// Recursive Version

#include <stdio.h>

void selectionSort(int[], int);
void putMinimalElementInPlace(int[], int);
void swap(int*, int*);
void printArrayContents(int[], int);

void main()
{
    int data[] = {12, 3, 37, 8, 24, 15, 5, 33};
    int ndata = sizeof(data) / sizeof(int);

    selectionSort(data, ndata);    
    printArrayContents(data, ndata);
}

void selectionSort(int data[], int ndata)
{
    putMinimalElementInPlace(data, ndata);
    if (ndata>2)
        selectionSort(&data[1], ndata-1);
}

void putMinimalElementInPlace(int data[], int ndata)
{
    int i, min;

    min = 0;
    for (i=1; i<ndata; i++)
    {
        if (data[i]<data[min]) min = i;
    }
    swap(&data[0], &data[min]);
}

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

void printArrayContents(int data[], int ndata)
{
    int i;
    printf("Sorted data:\n");
    for (i=0; i<ndata; i++)
        printf(" %d", data[i]);
    printf("\n");
}