This is a pencil and paper exercise. You may write your code in "pseudo-C++", which means that it does not have to be syntactically correct in every detail. However, the intended computation should be made clear. Write an explanation why your method uses only 3n/2 comparisions of entries.
Write sort5 as a C++ template function and test it using the sort5_test program that will be provided on the composers.
Grid data involves entries which are elements of
class GridNode { double val; int row; int col; public: // constructors GridNode(double v, int r, int c) : val(v), row(r), col(c){} GridNode() {} GridNode(const GridNode& b) : val(b.val), row(b.row), col(b.col){} // comparator for sorting in row order bool operator<=(GridNode& b) { return ( row < b.row ) || ( row == b.row && col <= b.col ); } };Here is an example of a data set of grid data in column order. It makes somewhat randomized data. Only about 50% of the n*n possible grid points will have data.
vector<GridNode> A; for (int j = 0; j < n; ++j) for (int i = 0; i < n; ++i ) if ( rand(2) ) A.push_back( GridNode( 1.0/(i+j+1), i, j) );Choose sensible sizes of data sets so that quickSort and randomizedQuickSort take time which is measurable but excessively long. Sensible sizes will be different for the four different categories of data.
This is an online exercise in which you write code to generate test cases and time the execution of sort as defined in ~saunders/320/sort/quickSort.h and randomizedQuickSort.h. Submit your test code so that we can see how your test data was constructed. Submit the results of the timings. Analyze/discuss/explain the timings. Do they come out as expected? What do you conclude from these experiments.
template<class Iterator> /* We require that class Iterator has pointer arithmetic operations and that the unknown class pointed to by dereferencing iterators has operator <= defined. */ void mergeSort(Iterator b, Iterator e, Iterator w) // w is for workspace { // Permute the items [b, (b+1) .. e) into sorted order. // Assume e-b is a power of 2. for (int k = 1; k < e-b; k = 2*k) // merge pairs of sections of length k for (Iterator i = b; i < e; i += 2*k) { merge(i, i+k, i+k, i+2*k, w); // copy back for (Iterator j = i; j < i+2*k; ++j) *j = *(w + (j - i)); } }Assume merge is as in ~saunders/320/sort/mergeSort.h on the composers.
Suppose that the data is a vector of 16 chars in reverse order such as this, for instance:
main() { vector<char> A(16), w(16); // assume A is initialized to the first 16 letters of the alphabet in // reverse order: A = {p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a} mergeSort(A.begin(), A.end(), w.begin()); }Write the value of k and the contents of A after each iteration of the outer for-loop in the bottom-up mergeSort function.