Script started on Mon Mar 07 22:29:49 2005 strauss[10:29pm] [~/Class/cisc181/examples/]> cat selection-sort.cc // Program sorts an integer array using the selection sort technique // Exercise 4.31 from D&D #include #include void selectionSort(int array[], int start_index, int end_index); // sorts the array via the selection sort method int findmin(int array[], int start_index, int end_index); // finds the index of the minimum element in the array void printArray(int array[], int size); // prints the array main () { const int size = 15; int numbs[size] = {6,3,8,2,0,12,13,14,1,4,5,11,10,7,8}; printArray(numbs, size); selectionSort(numbs, 0, size-1); printArray(numbs, size); return 0; } // sorts the array via the selection sort method // takes an integer array to be sorted and two indices // indicating the positions within the array to be sorted void selectionSort(int array[], int start_index, int end_index) { int temp, small_element_index; cout << "Entering selectionSort with start_index = " << start_index << endl; if (start_index < end_index) { small_element_index = findmin(array, start_index, end_index); cout << "\t Switching element at position " << small_element_index << " with element at position " << start_index << endl; // switch the smallest element into the lowest position in array temp = array[small_element_index]; array[small_element_index] = array[start_index]; array[start_index] = temp; selectionSort(array, start_index + 1, end_index); } } // finds the index of the minimum element in the array int findmin(int array[], int start_index, int end_index) { int imin = start_index, min_element = array[start_index], i; for (i = start_index + 1; i <= end_index; i++) if (array[i] < min_element) { min_element = array[i]; imin = i; } return imin; } // prints the array void printArray(int array[], int size) { int i; cout << "The array contains the following elements:" << endl; for (i = 0; i < size; i++) { cout << "[" << i << "]" << " = " << array[i] << endl; } } strauss[10:29pm] [~/Class/cisc181/examples/]> CC selection-sort.cc strauss[10:30pm] [~/Class/cisc181/examples/]> a.out The array contains the following elements: [0] = 6 [1] = 3 [2] = 8 [3] = 2 [4] = 0 [5] = 12 [6] = 13 [7] = 14 [8] = 1 [9] = 4 [10] = 5 [11] = 11 [12] = 10 [13] = 7 [14] = 8 Entering selectionSort with start_index = 0 Switching element at position 4 with element at position 0 Entering selectionSort with start_index = 1 Switching element at position 8 with element at position 1 Entering selectionSort with start_index = 2 Switching element at position 3 with element at position 2 Entering selectionSort with start_index = 3 Switching element at position 8 with element at position 3 Entering selectionSort with start_index = 4 Switching element at position 9 with element at position 4 Entering selectionSort with start_index = 5 Switching element at position 10 with element at position 5 Entering selectionSort with start_index = 6 Switching element at position 9 with element at position 6 Entering selectionSort with start_index = 7 Switching element at position 13 with element at position 7 Entering selectionSort with start_index = 8 Switching element at position 8 with element at position 8 Entering selectionSort with start_index = 9 Switching element at position 14 with element at position 9 Entering selectionSort with start_index = 10 Switching element at position 12 with element at position 10 Entering selectionSort with start_index = 11 Switching element at position 11 with element at position 11 Entering selectionSort with start_index = 12 Switching element at position 12 with element at position 12 Entering selectionSort with start_index = 13 Switching element at position 14 with element at position 13 Entering selectionSort with start_index = 14 The array contains the following elements: [0] = 0 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5 [6] = 6 [7] = 7 [8] = 8 [9] = 8 [10] = 10 [11] = 11 [12] = 12 [13] = 13 [14] = 14 strauss[10:30pm] [~/Class/cisc181/examples/]> ^D Use "exit" to leave sh. strauss[10:30pm] [~/Class/cisc181/examples/]> exit exit script done on Mon Mar 07 22:30:12 2005