Divide and conquer
Split a problem in half, solve the halves the same way, and join the results. Merge sort on eight numbers shows the three moves, and why the total is n log n.
In 30 seconds
Divide and conquer solves a big problem in three moves: divide it into smaller problems of the same kind, conquer those (by dividing again, until they are trivial), and combine the answers.
Merge sort splits a list in half until every piece is a single item, then merges the pieces back together in order.
Each level of merging handles all n items once, and halving allows only about log₂ n levels. That product, n log n, is why it beats simple sorts on large inputs.
Divide and conquer, step by step
Work is counted as items written by merges, with n a power of two. Real counts differ by small constant factors; the n log n growth does not.
- Step 1: One problem, three moves. The task: sort eight numbers. Divide and conquer never attacks the whole problem at once. It splits it into smaller problems of the same kind, solves those, and joins the answers. Merge sort is the classic example, and we follow it here.
- Step 2: Divide until it is trivial. Cut the list in half, then cut each half in half, and keep going until every piece holds one number. A list of one is already sorted, so there is nothing left to solve: that is the base case. Eight items take three rounds of halving, because 8 = 2 × 2 × 2.
- Step 3: Combine: merge two halves. Combining is where the real work happens. Suppose two halves have come back sorted. Look only at the front item of each, move the smaller one to the output, and repeat. Merging n items writes n items and needs at most n − 1 comparisons, so it costs about n steps.
- Step 4: Merge back up. Now climb back up the same tree. Single numbers merge into sorted pairs, pairs into sorted fours, and the two fours into the sorted list of eight. Every merge is the same small routine; the answer to the big problem appears at the top.
- Step 5: Why n log n. Count the merging level by level. Whatever the block size, each level moves all eight items exactly once. There are three levels, because eight can be halved three times. So the total is 8 × 3 = 24, or n log₂ n in general, far below the roughly n² steps of a simple sort.
- Step 6: Write it as a recurrence. A recurrence states the cost of a problem in terms of smaller ones. Sorting n items costs two sorts of n/2 items plus n steps of merging. Start from T(1) = 0 and build up: 2, 8, 24. It matches the level count, and solving it in general gives n log₂ n.
- Step 7: Binary search: one half. Binary search has the same shape. Looking for 6 in a sorted list, check the middle: 6 is larger than 4, so the left half is discarded. Check the middle of what is left: found. It conquers only one half and has nothing to combine, so the cost is about log₂ n looks.
Try it yourself
Learn this first
Learn next
- Fast Fourier transformFinding the waves inside a signal in n log n steps by splitting it into even and odd halves. Advanced
- BacktrackingTry a choice, check the rules, undo it when stuck: four queens on a small board. Intermediate
- Greedy algorithmsTake the best-looking step every time, and learn when that is enough. Beginner
Spotted a mistake on this page?