// FILE: SelSortT.cpp // SORTS AN ARRAY (ASCENDING ORDER) USING SELECTION SORT ALGORITHM // USES exchange AND find_min // Functions used ... #include "FindMinT.h" // find_min template prototype #include "ExchngT.h" // exchange template prototype template void sel_sort (apvector & items, // INOUT: array to be sorted int n) // IN: number of items to be sorted (n >= 0) // Sorts the data in array items (items[0] through items[n-1]). // Pre: items is defined and n <= declared size of actual argument array. // Post: The values in items[0] through items items[n-1] are in increasing // order. { // Local data ... int min_sub; // subscript of each smallest item located by // find_min for (int i = 0; i <= n-1; i++) { // Invariant: The elements in items[0] through items[i-1] are in their // proper place and i < n-1. // Find index of smallest element in unsorted section of items. min_sub = find_index_of_min (items, i, n-1); // Exchange items at position min_sub and i if different if (i != min_sub) exchange (items[min_sub], items[i]); } // end for return; } // end sel_sort