Merge sort
Merge two sorted lists in linear time, sort by splitting and merging, and give merge sort's cost, extra space and stability.
It opens on
Two sorted lists of 4 numbers each are merged into one sorted list of 8. Does that take about 16 comparisons, one for each pair, or far fewer?
Why it matters
Merge sort is the first sort whose running time is n log n for every input, which is why the sorts built into languages such as Python are merge based, and it is the standard example of divide and conquer.
Good to know first
- Selection and insertion sort: insertion sort makes n(n − 1)/2 comparisons at worst and a stable sort keeps equal items in order
- Recurrences and divide and conquer: T(n) = 2T(n/2) + n with T(1) = 0 has log₂ n levels that cost n each
- Recursion and the call stack: a call on a half-size list is a frame, and the depth is how many frames wait at once
- Lists: indexing and mutation: a[:mid] and a copy the two halves
Step by step
Merging two sorted lists
Merge two sorted lists by comparing their front items, trace it, and show that it makes at most n − 1 comparisons for n items.
We compare 3 and 1, and the 1 is smaller, so out is 1 and j moves on. Then we compare 3 and 2, and the 2 is smaller, so out is 1, 2.
A common mix-up
Merging compares every item of one list with every item of the other.
In fact: Each list is sorted, so only the two fronts are compared, and the merge needs at most n − 1 comparisons.
Split, sort, merge
Trace merge sort on a list of 8 by its tree of splits and its merges, and count the comparisons it makes.
Next the pair 8 and 3 is merged to 3, 8. Then the merge of 3, 6 with 3, 8 compares 3 with 3 and takes the left one, then 6 with 3, then 6 with 8, and then the 8 is copied, which is 3 comparisons and gives 3, 3, 6, 8.
A common mix-up
Merge sort sorts the halves by some other method first.
In fact: The halves are sorted by the same function calling itself, down to lists of one item, which need no sorting.
Cost, space and stability
State merge sort's comparisons, time, extra space and stability, and say how it compares with insertion sort.
Sort 6, 3a, 8, 3b, 1, 7, 2, 5, where 3a and 3b are equal numbers. With <= the merge of 3a, 6 with 3b, 8 takes 3a first, and the final result is 1, 2, 3a, 3b, 5, 6, 7, 8, so merge sort is stable. If the test were a strict < the 3b would be taken first and the order of equal items would be reversed.
the two results
A common mix-up
Merge sort is better than insertion sort on every list.
In fact: On a sorted list insertion sort makes n − 1 comparisons and merge sort about (n/2) log₂ n, so merge sort wins only on the worst and typical lists, and at the cost of Θ(n) space.
Try it
Merge 2, 9, 10 with 4, 5, 11, 12. What is the output, and how many comparisons were made?Answer
2, 4, 5, 9, 10, 11, 12; 5 comparisons
Hint: After the 10 is taken the first list is empty, and the rest is copied
Merge sort on 4, 1, 3, 2, cutting at len // 2. How many comparisons in all?Answer
5
Hint: 4 with 1 and 3 with 2 make one comparison each, then the final merge makes 3
Merge sort runs on 64 items. How many levels, and how many comparisons at worst?Answer
6 levels; 64 × 6 − 64 + 1 = 321 comparisons
Hint: n log₂ n − n + 1
Now i is at the first 3 and j is at the 5. Which item is taken next, and what is out afterwards?Answer
The 3 from the first list, and out is 1, 2, 3.
What does that first merge produce, and how many comparisons does it make?Answer
3, 6, with 1 comparison.
