/* sort_example.cc illustrates sorting with any of several sorting methods. Known to compile with "/opt/gcc-3.2/bin/g++ -static sortExample.cc" -bds 9/02 Known to compile with CC. -bds 9/06 */ #include "insertionSort.h" //#include "mergeSort.h" #include "quickSort.h" #include "heapSort.h" #include "introspectiveSort.h" #include #include using namespace std; #include "gridNode.h" // defines a class of keys #include "stopwatch.h" template void printSome(const Iterator b, const Iterator e, int c) // print first few and last few of items in range [b..e) { int chunk = c/sizeof(*b); // first chunk Iterator firstend = ((e - b) < chunk ? e : b + chunk); for (Iterator i = b; i < firstend; ++i) cout << *i << ", "; cout << endl; // last chunk Iterator secondbegin = (e - b < chunk ? e : ( e - b < 2*chunk ? b + chunk : e - chunk ) ); for (Iterator i = secondbegin; i < e; ++i) cout << *i << ", "; cout << endl; } int main(int argc, char* argv[] ) { // display beginning and end of vector using this many chars. int printChunk = 80; //////////////////////////////////////////////////////////////////// //////////// begin of data definition section ///////////////////// //////////////////////////////////////////////////////////////////// // modify this section (only) to experiment with different kinds of data // set up some data - kind 3, random int n = 30000; if (argc > 1 ) n = atoi(argv[1]); /* vector A(n); //for (int i = 0; i < n; ++i) A[i] = n-i; for (int i = 0; i < n; ++i) A[i] = rand(); typedef double Data; */ // set up some data - kind 4, grid data int m = 1000; if (argc > 1 ) m = atoi(argv[1]); // note data size will be larger vector A; for (int j = 0; j < m; ++j) for (int i = 0; i < m; ++i ) if ( rand()%2 ) A.push_back( GridNode( 1.0/(i+j+1), i, j) ); typedef GridNode Data; //////////////////////////////////////////////////////////////////// //////////// end of data definition section ///////////////////// //////////////////////////////////////////////////////////////////// // show initial values printSome(A.begin(), A.end(), printChunk); // do the sorting vector B(A.size()); StopWatch w; w.start(); copy ( A.begin(), A.end(), B.begin() ); cout << "using heapSort on " << B.size() << " items." << endl; w.lap("copy data"); heapSort(B.begin(), B.end()); w.lap("heapSort time"); printSome(B.begin(), B.end(), printChunk); copy ( A.begin(), A.end(), B.begin() ); cout << "using randomized quickSort on " << B.size() << " items." << endl; w.lap("copy data"); quickSort(B.begin(), B.end(), true); w.lap("randomized quickSort time"); printSome(B.begin(), B.end(), printChunk); copy ( A.begin(), A.end(), B.begin() ); cout << "using quickSort on " << B.size() << " items." << endl; w.lap("copy data"); quickSort(B.begin(), B.end()); w.lap("quickSort time"); printSome(B.begin(), B.end(), printChunk); copy ( A.begin(), A.end(), B.begin() ); cout << "using introspectiveSort on " << B.size() << " items." << endl; w.lap("copy data"); introspectiveSort(B.begin(), B.end()); w.lap("introspectiveSort time"); printSome(B.begin(), B.end(), printChunk); copy ( A.begin(), A.end(), B.begin() ); cout << "using randomized introspectiveSort on " << B.size() << " items." << endl; w.lap("copy data"); introspectiveSort(B.begin(), B.end(), true); w.lap("randomized introspectiveSort time"); printSome(B.begin(), B.end(), printChunk); copy ( A.begin(), A.end(), B.begin() ); if (B.size() > 50000) { cout << "Too many keys for O(n^2) insertionSort, " << B.size() << " items." << endl; } else { cout << "using insertionSort on " << B.size() << " items." << endl; w.lap("copy data"); insertionSort(B.begin(), B.end()); w.lap("insertionSort time"); printSome(B.begin(), B.end(), printChunk); } w.stop("all done"); }