/***********************************************************************
 * selectionSort.c
 *
 * Selection sort works by finding the smallest unsorted item and
 * moving it into its final position in the array.
 ***********************************************************************/

#include <stdio.h>

int sdata[10] = { 10, 45, 37, 5, 9, 23, 53, 24, 33, 19 };

int main()
{
   int i;
   int j;
   int min;     /* Index of smallest element so far. */
   int temp;    /* Temp location for swapping elements. */

   /* Find and position n-1 smallest elements. */

   for (i = 0; i < 10 - 1; i++)
   {
      min = i;   /* Set min position to beginning of search space. */

      /* Find index of smallest unsorted item. */

      for (j = i + 1; j < 10; j++)
         if (sdata[j] < sdata[min])
            min = j;

      /* Swap elements. */

      temp = sdata[i];
      sdata[i] = sdata[min];
      sdata[min] = temp;
   }

   /* Print sorted array. */

   for (i = 0; i < 10; i++)
      printf("%d\n", sdata[i]);

   return 0;
}
