

#include <stdio.h>

int cmp(int x, int y)  // compare x with y
{
    return x < y;      // 1 if x < y, 0 otherwise
}

void swap(int *x, int *y)
{
    int tmp = *x;

    *x = *y;
    *y = tmp;
}

int *select(int p[], size_t len)
{
    int *pos = p;    // pointer to the highest-precedence element

    for (size_t i = 0; i < len; ++i) {
        if (cmp(p[i], *pos)) {
            pos = &p[i];
        }
    }

    return pos;
}

void disp(int p[], size_t len)
{
    for (size_t i = 0; i < len; ++i) {
        printf("%d ", p[i]);
    }
    printf("\n");
}

int main(void)
{
    int a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
    size_t n = sizeof a / sizeof a[0];

    for (size_t i = 0; i < n - 1; ++i) {

        int *q = select(&a[i+1], n-i-1);

        if (cmp(*q, a[i])) {
            swap(&a[i], q);
        }

    }

    disp(a, n);

    return 0;
}
