Algorithms / Divide and conquer

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.

Intermediate7 steps~7 min
Problem — a list still to be sorted
Solved piece — a list that is already in order
Divide — splits a problem into two halves
Combine — merges two answers into one
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 One problem, three moves
THE MATH
1 divide 2 conquer 3 combine
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
VALUES SO FAR
input5 2 7 1 8 3 6 4n = 8
depth····divide
merge 8····combine
sorted····result
work····n log n
T(n)····sort
T(n)····search

Spotted a mistake on this page?