How does merge sort put a list in order by splitting and joining?
A lesson on sorting. Computing, secondary school level.
Merge sort orders a list by splitting it in half again and again until every piece holds one item, which is already sorted.
Then it joins pieces in pairs.
To merge two sorted pieces, it compares their first items, takes the smaller, and repeats until both are empty.
Take the list 5, 2, 4, 1.
It splits into 5 and 2, and 4 and 1, which merge into 2, 5 and 1, 4.
Comparing their fronts gives 1, then 2, then 4, then 5.
Each round of merging touches every item once, so sorting stays fast even for long lists.
Questions in this lesson
- You have a huge pile of exam papers to put in order by name, and you want friends to help. Which plan do you think works best?
- In your own words, how does merge sort put a list in order, and why does merging stay fast?