/* P. Conrad, program to sort an array    */

#include <stdio.h>

#define ARRAYSIZE 5

int main (void) 
{

  int scores[ARRAYSIZE]; /* array to store test scores */
  
  int i,j; /* index variables */
  int minIndex; /* index of smallest array element */
  int temp; /* temporary location for swapping array elements */

  /* populate my array */

  for (i=0; i<ARRAYSIZE; i++)
    {
      printf("Enter score %d >", i);
      scanf("%d", &scores[i]);
    }

  /* first step towards sorting */
  /* find index of minimum element */

  for (j=0; j < ARRAYSIZE - 1; j++)
    {
      /* find the smallest element between position j and the end of the
	 array, and then put that element into position j */

      minIndex = j; /* initial guess at minIndex */
      
      for (i=j+1; i<ARRAYSIZE; i++)
	{
	  /* consider element at position i */
	  /* if it is smaller than the one at minIndex, update minIndex */
	  
	  if ( scores[i]  < scores[ minIndex]  )
	    {
	      minIndex = i;
	    }
	} /* for i = ... */
      
      /* now minIndex is the index of the smallest element in position
         j though ARRAYSIZE - 1, so swap element at position j with that at
         position minIndex */

      temp = scores [j];
      scores [j] = scores[minIndex];
      scores[minIndex] = temp;

      /* now the array is sorted from position 0 through position j;
         all elements in those positions are in their final resting place */

    } /* for j = ... */


  /* print out final sorted array */

  for (i = 0; i< ARRAYSIZE; i++)
    printf("scores[%2d] = %3d\n",i,scores[i]);

  return 0;
}

