Programming5 min read10 interview questionsNew · November
Merge sort: split, sort, merge
A clear way to sort that is always fast, explained step by step.
In one sentenceMerge sort splits a list in half again and again, sorts the small pieces and then merges them back together in order.
Think of it like thisIt is like sorting a pile of exam papers by dividing the pile between friends, then combining their sorted piles two at a time.
The big picture
Tap any step to see where to use it, how to use it and an example result.
The idea
A list with one item is already sorted. Merge sort uses recursion: sort the left half, sort the right half, then merge the two sorted halves. The real work happens in the merge step.
How merging works
Look at the first item of each sorted half. Take the smaller one, add it to the result and move on in that half. When one half is empty, add the rest of the other. Each item is touched once, so a merge costs about n steps.
Speed and memory
The list is halved about log n times and each level costs about n, so the time is O(n log n) in every case, best or worst. It needs extra memory of about n for the merged pieces. It is stable, which means equal items keep their original order.
In real life
Python's sorted() and list.sort() use Timsort, a mix of merge sort and insertion sort tuned for real data. You rarely write merge sort yourself, but it teaches divide and conquer, and it suits sorting data too big for memory and sorting linked lists.
Common mistakes
- Forgetting the base case, so the splitting never stops.
- Copying lists repeatedly and using far more memory than needed.
- Merging with the wrong comparison, which breaks stability.
- Believing it is always the best choice: for small lists, simpler sorts can be faster.
Quick recap
- Split in half, sort each half, then merge.
- Time is O(n log n) in all cases.
- It uses extra memory and is stable.
- Python's built-in sort is a merge-based hybrid.
Try it yourself
Sort the numbers 5, 2, 9, 1, 7, 3 on paper using merge sort. Draw the splits, then the merges, and count how many levels there are.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is merge sort?Basic
A sorting algorithm that divides the list into halves, sorts each half recursively and merges the two sorted halves into one sorted list.
What is its time complexity?Basic
O(n log n) in the best, average and worst cases.
What is the base case?Basic
A list with zero or one item, which is already sorted, so the recursion stops there.
What does merging two sorted lists mean?Basic
Compare the front items of both lists, take the smaller, repeat until one list runs out, then append what remains of the other.
What is its space complexity?Intermediate
O(n) extra space for the temporary lists used while merging, plus recursion stack space of O(log n).
What does it mean that merge sort is stable?Intermediate
Items that compare equal stay in the same relative order as in the input. That matters when sorting records by one field after another.
Merge sort versus quick sort: when would you pick each?Intermediate
Merge sort has guaranteed O(n log n), is stable and suits linked lists and external data. Quick sort is often faster in memory because it sorts in place, but its worst case is O(n²) without care.
Why is it O(n log n)?Intermediate
Halving the list gives about log n levels, and at each level merging touches every item once, which costs n. Multiply the two.
How would you sort a file that is larger than memory?Advanced
Use external merge sort: read chunks that fit in memory, sort each and write them to disk, then merge the sorted chunks in a streaming way.
What is Timsort and why does Python use it?Advanced
A hybrid that finds runs of already ordered data, sorts short runs with insertion sort and merges runs. It is stable and very fast on real-world data that is partly sorted, with O(n) best case.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.