This assignment makes use of files contained in this zip file. In this assignment you will implement and test three improvements to the Mergesort algorithm.
This assignment is due Tuesday, September 27.
In the zip file is the LaTeX file CS332Hw2.tex . Change the name of this file to CS332Hw2Surname.tex where Surname is your last name (and also put your name in the appropriate place in the document).
There are four parts to this assignment.
Even though Mergesort is an O(n ln(n)) algorithm and Insertion Sort is an O(n^2) algorithm, that does not mean that Mergesort is always faster than Insertion Sort. When n is small, it very well might be that Insertion Sort is faster than Mergesort. For the first part of this assignment, you are to gather data about the running time of Mergesort and Insertion Sort for "small" values of n. Use the three files Sort.c , Mergesort.h , and InsertionSort.h contained in the zip file. Report your findings by answering Question 1 in the file CS332Hw2Surname.tex .
Since we know that for "small" values of n, Insertion Sort is faster than Mergesort, one way to improve Mergesort is to have the mergeSort() function switch to Insertion Sort for a certain value of n instead of recursively calling itself. That is, the recursive function mergeSort() should terminate its recursion well before it gets to n=1, and it should terminate the recursion by calling the insertion sort function. Implement this strategy for the function mergeSort2() in the header file called MergeSort2.h from the zip file. Do some experimentation to find an optimal (or near optimal) value at which merge sort should stop recursing and call insertion sort. Write a program Sort2.c that you can use to do your experimentation with. For your "optimal value," how much of an improvement is there in MergeSort2.h over the implementation in MergeSort.h ? Report your findings by answering Question 2 in the file CS332Hw2Surname.tex .
If you look at the code for the Merge operation on page 31 of our textbook, you see that line 3 of the code creates two new arrays every time Merge is called. But allocating dynamic arrays is a fairly expensive operation. Since we know ahead of time that Merge never needs more than n extra storage spaces (where n is the size of the original array that is being sorted) one strategy for speeding up Mergesort is to allocate one "temporary" array of size n as soon as mergeSort() is first called and then passing this array as an extra parameter to all subsequent calls to mergeSort() and merge() . Implement this improvement in the mergeSort3() and merge3() functions in the file MergeSort3.h (use your code from mergeSort2() as a starting point for mergeSort3() ). Write a program Sort3.c that you can use to test and time your new implementation. How much of an improvement is there in your MergeSort3.h over your implementation in MergeSort2.h ? Report your findings by answering Question 3 in the file CS332Hw2Surname.tex .
If you look at the code for the Merge operation on page 31 of our textbook, you see that lines 4-7 copy the contents of the A[] array into the temporary arrays, and then lines 12-17 copy the contents of the temporary arrays back into the A[] array. This double copying is wasteful of time. We can arrange it so that each call to Merge copies in only one direction, but the calls to Merge "alternate" the direction. We do this by blurring the distinction between the "original" array and the "temporary" array that we used in the previous improvement. Implement this strategy by following the hints given in the file MergeSort4.h (use your code from mergeSort3() as a starting point for mergeSort4() ). Write a program Sort4.c that you can use to test and time your new implementation. How much of an improvement is there in your MergeSort4.h over your implementation in MergeSort3.h ? How much of an improvement is there in your MergeSort4.h over the original implementation in MergeSort.h ? Report your findings by answering Question 4 in the file CS332Hw2Surname.tex .
Note: In this last improvement, you will no longer have room in the arrays for the "sentinel" values that Merge uses. So you will need to rewrite Merge a bit so that it does not rely on sentinels (the loops need extra checks for when the they get to the ends of the arrays). This actually slows Merge down a bit, but hopefully removing the extra array copy will make up for this bit of slowing down.
Turn in a zip file called CS332Hw2Surname.zip (where Surname is your last name) containing your final version of the LaTeX file CS332Hw2Surname.tex , a CS332Hw2Surname.pdf version of your LaTeX file, and your versions of the C files MergeSort2.h , Sort2.c , MergeSort3.h , Sort3.h , MergeSort4.h and Sort4.h .
This assignment is due Tuesday, September 27.
|