/** * selection_sort.c * * Andi Peng * Section, Week 3 * * Sorts array of n values. * Implemented using selection sort. */ #include int main (void) { int array[] = {4, 8, 2, 1, 6, 9, 7, 5, 3}; int n = 9; for (int i = 0; i < n; i++) { printf("%i", array[i]); } printf("\n"); // iterate through array for (int i = 0; i < n - 1; i++) { // create variable for keeping track of the smallest value int min = i; int j; // iterate through unsorted values of the array for (j = i; j < n - 1; j++) { // find the next smallest element if (array[j + 1] < array[min]) { min = j + 1; } } // swap if (min != i) { int temp = array[i]; array[i] = array[min]; array[min] = temp; } } for (int i = 0; i < n; i++) { printf("%i", array[i]); } printf("\n"); }