Phase 03 · DSA & Problem SolvingLesson 4 of 4
Merge Sort & Quick Sort
Two classic divide-and-conquer sorts, visualised and dry-run.
The problem
Sorting is one of the most studied problems in computer science, and two algorithms come up again and again in interviews: merge sort and quick sort. Both use the same powerful strategy, divide and conquer, but they make different trade-offs.
The concept
Divide and conquer#
- Divide the problem into smaller sub-problems.
- Conquer each sub-problem (usually with recursion).
- Combine the results.
Divide and conquer
- Dividesplit into smaller sub-problems
- Conquersolve each one, usually with recursion
- Combinejoin the results
Merge sort#
Split the list in half until each piece has one item (a single item is already sorted), then merge sorted halves back together.
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, 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
- Always O(n log n): the list is halved about log n times, and each level of merging touches all n items.
- Stable: equal items keep their original order.
- Needs O(n) extra memory for the merged lists.
Quick sort#
Pick a pivot, move smaller items to its left and larger items to its right (partition), then sort each side.
def quick_sort(items):
if len(items) <= 1:
return items
pivot = items[len(items) // 2]
smaller = [x for x in items if x < pivot]
equal = [x for x in items if x == pivot]
larger = [x for x in items if x > pivot]
return quick_sort(smaller) + equal + quick_sort(larger)
- Average O(n log n), and very fast in practice.
- Worst case O(n²) when pivots are consistently bad (for example always the smallest item), making partitions very uneven.
- The classic in-place version uses very little extra memory.
Side by side#
| Merge sort | Quick sort | |
|---|---|---|
| Time (average) | O(n log n) | O(n log n) |
| Time (worst) | O(n log n) | O(n²) |
| Extra memory | O(n) | O(log n) in-place |
| Stable? | Yes | Not usually |
Practice#
Dry-run both algorithms on paper with [5, 2, 8, 1, 9, 3]. Drawing the splits and merges by hand is the fastest way to make the idea click.
Try it
Your task · about 10 minutes
Implement merge sort, then compare its speed with sorted() on a list of 100,000 random numbers.
Recap & next
Key takeaways
- Both split the problem into smaller parts, solve them, then combine (divide and conquer).
- Merge sort is always O(n log n) and stable, but needs extra memory for merging.
- Quick sort averages O(n log n) and sorts in place, but degrades to O(n²) with consistently bad pivots.
- Python's built-in
sorted()uses Timsort, a hybrid that runs in O(n log n).
Quick check
2 questions · instant feedback
Go deeper
Finished this lesson?
Mark it complete to track your progress and keep your streak going.

