// Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) // // This work is licensed under the Creative Commons Attribution-NonCommercial- // ShareAlike License. To view a copy of this license, visit // http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to // Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, // 94105, USA. #include #include #include #include using namespace std; template void selection_sort(vector &entries) { int entries_sz = entries.size(); for (int i = 0; i < entries_sz; i++) { // find minimum value in the range i..(N_entries-1) int min_idx = i; for (int j = i+1; j < entries_sz; j++) if (entries[j] < entries[min_idx]) min_idx = j; // if needed, swap i-th entry with min_idx entry if (i == min_idx) continue; T swap_tmp = entries[i]; entries[i] = entries[min_idx]; entries[min_idx] = swap_tmp; } } int main(int argc, char *argv[]) { if (argc != 2) { cerr<<"Usage:\n\t"< entries; // read in entries to sort for (float f; istrm>>f, istrm.good(); ) entries.push_back(f); istrm.close(); // sort them selection_sort(entries); // display the results for (vector::iterator iter=entries.begin(); iter != entries.end(); iter++) cout<<*iter<