


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In earlier lessons you were introduced to two classic sorting algorithms, selection sort and insertion sort. While easy to explain, both these algorithms were O(n^2 ) worst case. There are many sorting algorithms that can do better. In this lesson we will explore one of these.
As with many algorithms, the intuition for merge sort is best understood if you start in the middle, and only after having seen the general situation do you examine the start and finish as special cases. For merge sort the key insight is that two already sorted lists can be very rapidly merged together to form a new list. All that is necessary is to walk down each of the original lists in order, selecting the smallest element in turn:
When you reach the end of one of the lists (and you cannot, in general, predict which list will end first), you then must copy the remainder of the elements from the other.
Based on this description you should now be able to complete the implementation of the merge method. Assume that a and b are input arrays, and that the array c is large enough to hold the result:
public void merge (double [] a, double [] b, double [] c) {
Let n represent the length of the result. At each step of the loop one new value is being added to the result. Hence the merge algorithm is O(n). A Recursive Sorting Algorithm So how do you take the observation that it is possible to quickly merge two ordered lists, and from this produce a fast sorting algorithm? The key is to think recursively. Imagine sorting as a three step process. In the first step an unordered array of length n is broken into two unordered arrays each containing approximately half the elements of the original. (Approximately, because if the size of the original is odd, one of the two will have one more element than the other). Next, each of these smaller lists is sorted. Finally, the two sorted lists are merged back to form the original:
int mid = data.length/2; double [] first = new double[mid]; System.arraycopy(data, 0, first, 0, mid); mergeSort(first); // first recursive call int secondLength = data.length โ mid; double [] second = new double[secondLength]; System.arraycopy(stordataage, mid, second, 0, secondLength); mergeSort(second); // second recursive call merge(first, second, data); // merge back together }
Algorithmic Analysis of Merge Sort Recall from Lesson x that the analysis of recursive algorithms involves defining the execution time as a recurrence relation. Merge sort is breaking an array of size n into two smaller arrays, and then sorting the smaller arrays. So the equation is roughly
T(N) = 2 * T(N / 2) + LOT
Recall that the solution to this relation is approximately n log n. You can verify this by substituting n log n into the equation and simplifying
N log N = 2 * ((N/2) log (N/2)) + LOT = N (Log N โ 1) + LOT = N log N + L.O.T.
An O(n log n) algorithm is a vast improvement over an O(n^2 ) one. To illustrate, you can try executing the merge sort algorithm on arrays of various sizes, and comparing the execution times to that of selection sort. The following table shows some of the values in milliseconds that you would typically discover:
size 5000 6000 7000 8000 9000 10000 selection sort 104 165 230 317 402 500 merge sort 11 12 15 15 19 21
Is it possible to do better? The answer is yes and no. It is possible to show, although we will not do so here, that any algorithm that depends upon a comparison of elements must have at least an O(n log n) worst case. In that sense merge sort is about as good as we can expect. On the other hand, merge sort does have one annoying feature, which is that it uses a lot of extra memory in order to allocate all those additional arrays. It is possible to discover algorithms that, while not asymptotically faster than merge sort, do not have this disadvantage. We will examine one of these in the next lesson.