AQA 7517, section 4.3.5.2
Merge sort is a divide and conquer algorithm. It solves a big problem by splitting it into smaller copies of the same problem, solving those, and then combining the answers.
For sorting, that means:
- Divide: split the list in half, and keep splitting until every piece has only one item. A list of one item is sorted by definition.
- Conquer (merge): join pieces back together in pairs, in order, until there's one sorted list.
Merging two sorted lists
The important step is merging two lists that are each already sorted. Compare the front items of the two lists, take the smaller one, and repeat. When one list runs out, add what's left of the other.
Merge and :
| Compare | Take | Result so far |
|---|---|---|
| 2 and 1 | 1 | 1 |
| 2 and 7 | 2 | 1, 2 |
| 6 and 7 | 6 | 1, 2, 6 |
| 9 and 7 | 7 | 1, 2, 6, 7 |
| 9 and (empty) | 9 | 1, 2, 6, 7, 9 |
Trace
Sort :
- Split into and , then into , , , .
- Merge and to get . Merge and to get .
- Merge with . Compare the fronts: 3 goes first, then 27, then 38, then 43.
The result is .
In Python
def merge_sort(items):
if len(items) <= 1:
return items
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
print(merge_sort([38, 27, 43, 3])) # [3, 27, 38, 43]
Merge sort is recursive. merge_sort calls itself on each half, and the base case is a list of 0 or 1 items, which is returned as it is.
Time complexity
- The list is halved repeatedly, so there are about levels of splitting.
- At each level, merging touches every item once, which is about steps.
- So the total is about steps, giving .
This is the same in the best, average and worst cases. There's no lucky ordering that makes it faster.
| Items () | Bubble sort, about | Merge sort, about |
|---|---|---|
| 1 000 | 1 000 000 | about 10 000 |
| 1 000 000 | 1 000 000 000 000 | about 20 000 000 |
For a list of a million items, merge sort is around 50 000 times quicker than bubble sort.
- Memory: it needs extra space, about , to hold the merged lists. Bubble sort needs almost none.
Worked example
Task: sort using merge sort, showing the splits and merges.
Divide: splits into and . Then splits into and , and splits into and . Likewise splits into and , and then and .
Merge:
- and give . and give .
- and give . and give .
- and : take 1, 2, 4, 7, 8, then 9.
The result is .
Exam tip: a trace should show both phases: the repeated splitting down to single items, and the merging back up. Quote for the time, and explain it: the list is halved about times, with about work at each level. Describing it as "divide and conquer" earns a mark too.
Compared with bubble sort
| Bubble sort | Merge sort | |
|---|---|---|
| Worst-case time | ||
| Extra memory | Very little | About |
| Approach | Repeated swaps of neighbours | Divide and conquer |
| Best for | Very small or nearly sorted lists | Large lists |
Check yourself
Merge [2, 6, 9] and [1, 7] into one sorted list
Compare the fronts each time: 1, then 2, then 6, then 7, then the remaining 9. The result is .
What is the time complexity of merge sort?
.
Why is merge sort described as divide and conquer?
It divides the problem (the list) into smaller parts, solves each part, and combines the results. Here it splits the list in half repeatedly, and merges the sorted halves.
Give one disadvantage of merge sort compared with bubble sort
It needs extra memory (about ) for the merged lists, whereas bubble sort sorts in place.