/* Test driver for sorting. Written by: G & F Date: 2/98 Brooks/Cole Publishing Company An International Thomson Publishing Company Copyright 1998. All Rights Reserved */ #include #define MAX_ARY_SIZE 15 int main (void) { /* Prototype Declarations */ void selectionSort (int list[ ], int last) ; /* Local Declarations */ int i ; int ary[ MAX_ARY_SIZE ] = { 89, 72, 3, 15, 21, 57, 61, 44, 19, 98, 5, 77, 39, 59, 61 } ; /* Statements */ printf("Unsorted array: ") ; for (i = 0; i < MAX_ARY_SIZE; i++) printf("%3d", ary[ i ]) ; selectionSort (ary, MAX_ARY_SIZE - 1) ; printf("\nSorted array: ") ; for (i = 0; i < MAX_ARY_SIZE; i++) printf("%3d", ary[ i ]) ; printf("\n") ; return 0 ; } /* main */ /* =================== selectionSort ==================== */ /* Sorts list[1Élast] by selecting smallest element in unsorted portion of array and exchanging it with element at the beginning of the unsorted list. Pre list must contain at least one item. last contains index to last element in the list Post The list has been rearranged smallest to largest */ void selectionSort (int list[ ], int last ) { /* Local Declarations */ int current; int smallest; int holdData; int walker; /* Statements */ for ( current = 0; current < last; current++ ) { smallest = current; for (walker = current + 1; walker <= last; walker++) if ( list[ walker ] < list[ smallest ] ) smallest = walker; /* Smallest selected: exchange with current */ holdData = list[ current ]; list[current] = list[ smallest ]; list[smallest] = holdData; } /* for current */ return; } /* selectionSort */ /* Results: Unsorted array: 89 72 3 15 21 57 61 44 19 98 5 77 39 59 61 Sorted array: 3 5 15 19 21 39 44 57 59 61 61 72 77 89 98 */