// 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 using namespace std; // bubble_sort - sorts, in place, the items stored in the vector 'entries' // // notes: the type of the items, T, must support '<' template void bubble_sort(vector &entries) { bool swapped=true; for (int i = entries.size()-1; i >= 0 && swapped; i--) for (int j = 0, swapped=false; j < i; j++) { if (entries[j+1] < entries[j]) { T swap; swap = entries[j+1]; entries[j+1] = entries[j]; entries[j] = swap; swapped=true; } } } int main(int argc, char *argv[]) { if (argc != 2) { cerr<<"Usage:\n\t"< entries; for (float f; istrm>>f, istrm.good(); ) entries.push_back(f); istrm.close(); // sort them bubble_sort(entries); // display the results for (vector::iterator iter=entries.begin(); iter != entries.end(); iter++) cout<<*iter<