/*
Lauren Dukes
CPSC 1110 Example

Selection sort implementation.
Also demonstrates use of using scanf to put directly into the array, and use of a constant.
*/

#include <stdio.h>

//constant to limit the number of items in the array
#define LIMIT 10

int main(void){

	//count of current number of elements entered
	int currCt = 0;

	//holds the items entered in
	int numbers[LIMIT];

	for (currCt = 0; currCt < LIMIT; currCt++){	
		printf("Enter values (%d of %d): ",currCt, LIMIT);
		scanf("%d",&numbers[currCt]);
	}
	
	//selection sort begins here

	//declare i and j as counter variables to iterate through the loop
	int i = 0;
	int j = 0;
	//for each position in the array
	for (i = 0; i < LIMIT; i++){
		//for starters, assume that the smallest item is in the current position we're looking at
		int smallestIdx = i;
		//go through the rest of the array (indices i + 1 through the end)
		for (j = i + 1; j < LIMIT; j++){
			//if we see a number that's smaller than the smallest number we've seen so far
			if (numbers[j] < numbers[smallestIdx]){
				//then set the smallest index to the place where we saw that smallest number
				smallestIdx = j;
			}
		}
		
		//swap the entries at the current position we're looking at and the position where we saw the smallest value
		int temp;
		temp = numbers[i];
		numbers[i] = numbers[smallestIdx];
		numbers[smallestIdx] = temp;
	}	
	
	//print out the sorted array
	for (i = 0; i < LIMIT; i++){
		printf("%d ",numbers[i]);
	}
	printf("\n");

	return 0;
}
