#include #include "apvector.h" #include "apstring.h" #include "sortall.h" // ******************************** // // illustrates use of templated sorts // and function objects for comparing the templated sorts // author: Owen Astrachan // 7/20/98 // // see also: sortall.h, sortall.cpp, comparer.h // // ******************************** struct Reverse // for sorting in reverse alphabetical order { int compare(const apstring & lhs, const apstring & rhs) const { if (lhs < rhs) return 1; // reverse: signals that lhs > rhs on return if (lhs > rhs) return -1; // reverse: signals that lhs < rhs on return return 0; } }; void print(const apvector & a, int size) // postcondition: all vector elements printed, one per line { int k; for(k=0; k < size; k++) { cout << a[k] << endl; } } void add(apvector & list,const apstring & value, int & n) // precondition: n = # elements in list // postcondition: value added to end of list, n = # elements in list { if (n >= list.length()) { list.resize(list.length()*2); } list[n] = value; n++; } int main() { apvector list(10); int count = 0; apstring pause; add(list,"orange",count); add(list,"apple",count); add(list,"canteloupe",count); add(list,"tangerine",count); add(list,"cherry",count); add(list,"watermelon",count); print(list,count); cout << endl << "insertion sort" << endl << "-----" << endl; InsertSort(list,count); print(list,count); cout << "return"; getline(cin,pause); cout << endl << "insertion sorting in reverse" << endl << "-----" << endl; Reverse rev; InsertSort(list,count,rev); print(list,count); cout << "return"; getline(cin,pause); cout << endl << "insertion sort with comparer" << endl << "-----" << endl; InsertSort(list,count,Comparer()); print(list,count); cout << "return"; getline(cin,pause); cout << endl << "selection sort" << endl << "-----" << endl; SelectSort(list,count); print(list,count); cout << "return"; getline(cin,pause); cout << endl << "mergesort reverse" << endl << "-----" << endl; MergeSort(list,count,Reverse()); print(list,count); return 0; }