What's happening?
- Split the range at the middle and sort each half (by the same method).
- A piece with one value is already sorted — that is where the splitting stops.
- Merge two sorted halves by repeatedly taking the smaller of their front values into a buffer, then copy the buffer back.
Complexity
- Time
- O(n log n) always — best, average and worst
- Space
- O(n) for the merge buffer
Where you'll meet it
Sorting linked lists, external sorting of files too big for memory, and the basis of TimSort (Python, Java objects). Its guaranteed O(n log n) and stability matter.
Common mistake
Forgetting the extra memory. Merge sort needs a buffer as big as the list, unlike quick sort or heap sort.
FAQ
Why is merge sort O(n log n)?
Halving gives about log n levels, and each level merges all n values once.
Is merge sort stable?
Yes, as long as the merge takes from the left half when values are equal — as it does here.
Merge sort vs quick sort?
Merge sort is predictable and stable but needs extra memory; quick sort is usually faster in practice and sorts in place.