Merge sort
Computer Science · Algorithms
Question 1 of 10
0Two sorted lists are merged: L = [3, 19] and R = [5, 27, 31]. Using the merge step, which item is written first to the output?
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Merge sort explained
Merge sort sorts a list by splitting it in half again and again until every piece holds one item, then merging the pieces back together in order. A single item is already sorted, so the real work happens while merging. It is an example of the divide and conquer strategy.
The algorithm has two stages:
- Divide: split the list into two halves, and split each half in the same way until each piece has one item.
- Merge: join two sorted pieces into one sorted piece. Compare the front item of each piece, move the smaller one to the new list, and repeat until both pieces are empty.
Each level of splitting touches every item once, and there are about levels. The time is therefore in every case, which is much faster than on large lists. The cost is the extra memory needed for the merged lists.
A common mistake is to join the two sorted halves end to end. The merge step must compare the front items so that the smaller one is taken first.
Worked example
Sort [6, 3, 8, 1].
- Divide: [6, 3] and [8, 1], then [6], [3], [8] and [1]. Four items need levels of splitting.
- Merge [6] and [3]: compare 6 and 3, take 3, then 6. The result is [3, 6].
- Merge [8] and [1] in the same way to get [1, 8].
- Merge [3, 6] and [1, 8]: take 1, then 3, then 6, then 8. This takes 3 comparisons.
- Total: 1 + 1 + 3 = 5 comparisons, and the final list is [1, 3, 6, 8].
Key facts
The practice questions use these exact rules:
- A list of items is split at index . The left part gets items and the right part gets the rest, so with an odd the right part is bigger.
- In a merge, compare the front items of the two sorted lists and move the smaller to the output. One comparison is made for each item moved while both lists still have items.
- When one list runs empty, the rest of the other list is copied across with no more comparisons.
- Merging lists of and items therefore takes at most comparisons, and at least the smaller of and .
The longest chain of splits follows the larger part each time. For 11 items the sizes are 11, 6, 3, 2, 1, so there are 4 splits. This equals .
Worked example 2
Merge the sorted lists L = [4, 9, 15] and R = [2, 6, 7, 20, 23]. How many comparisons are made?
- 4 vs 2: output 2.
- 4 vs 6: output 4.
- 9 vs 6: output 6.
- 9 vs 7: output 7.
- 9 vs 20: output 9.
- 15 vs 20: output 15. L is now empty.
- Copy 20 and 23 with no comparisons.
The output is [2, 4, 6, 7, 9, 15, 20, 23], made with 6 comparisons. The most possible was .
Worked example 3
Sort [38, 12, 27, 5, 44, 19, 8] with top-down merge sort and count every comparison.
- Split 7 items at : [38, 12, 27] and [5, 44, 19, 8].
- Left half: split into [38] and [12, 27]. Merge [12] and [27]: 1 comparison, giving [12, 27]. Merge [38] with [12, 27]: 12 vs 38, then 27 vs 38, then copy 38. That is 2 comparisons, giving [12, 27, 38]. Left half total: 3.
- Right half: split into [5, 44] and [19, 8]. Merge to get [5, 44] (1 comparison) and [8, 19] (1 comparison). Merge those: 5 vs 8, 44 vs 8, 44 vs 19, then copy 44. That is 3 comparisons, giving [5, 8, 19, 44]. Right half total: .
- Final merge of [12, 27, 38] and [5, 8, 19, 44]: 12 vs 5, 12 vs 8, 12 vs 19, 27 vs 19, 27 vs 44, 38 vs 44, then copy 44. That is 6 comparisons.
- Total: comparisons. The sorted list is [5, 8, 12, 19, 27, 38, 44].
If a question asks only for the final merge, the answer is 6. Sort each half first, then count the last merge on its own.
Common mistakes
- Splitting an odd-length list with the bigger part on the left. Here the left part gets .
- Counting the copying of leftover items as comparisons.
- Counting only the last merge when the question asks for the total over all merges.
- Merging unsorted halves. Each half must be fully sorted before the merge.
- Joining two sorted halves end to end instead of comparing front items.
Exam tips
- Draw the split as a tree, then merge back up level by level, writing the comparisons beside each merge.
- Check each merge: the number of comparisons must lie between the smaller list's length and .
- Check your final list: it must be in order and contain every original item.
- Give the complexity as for every case, and mention the extra memory used by merging.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
Top-down merge sort splits a list at index floor(n/2): the left part gets floor(n/2) items and the right part gets the rest. A list of one item is not split. For a list of 12 items, how many splits lie on the longest chain from the full list down to a single item?
Medium
Merge the sorted lists L = [17, 30, 36] and R = [4, 11]. Merge: compare the front items of the two sorted lists, move the smaller one to the output, and repeat. When one list is empty, copy the rest of the other list without comparisons. What is the value at index 4 (counting from 0) of the merged output?
Hard
Top-down merge sort on [21, 6, 31, 17, 36, 11, 32] splits the list at index floor(n/2), sorts each half with merge sort, and then makes one final merge of the two sorted halves. Merge: compare the front items of the two sorted lists, move the smaller one to the output, and repeat. When one list is empty, copy the rest of the other list without comparisons. How many comparisons does that final merge make?