When to use divide and conquer
Use divide and conquer when:
- A problem can be split into smaller copies of itself, and the answers can be combined: sorting, searching, counting, multiplying big numbers.
- The pieces are independent, so they can be solved separately, or even in parallel on different machines or cores.
- You want to sort in O(n log n) in the worst case (merge sort), or find the k-th smallest or the median without sorting everything (quickselect).
- You need to count something across a split, such as pairs that are out of order (inversions).
Use something else when:
- The pieces overlap: the same smaller problem appears again and again, as in Fibonacci. Use dynamic programming to remember answers.
- You only need the top k of a stream: a heap is simpler.
- A simple loop does the job. Recursion adds overhead, so do not split a problem that one pass already solves.
Do not write your own sort in production. Every language ships an excellent one (see the real-world section). Write merge sort and quickselect to learn the pattern and for interviews.
How divide and conquer works
Divide and conquer has three steps:
- Divide. Split the problem into smaller copies of the same problem.
- Conquer. Solve each copy by recursion. A tiny copy (a base case) is solved directly.
- Combine. Put the answers together.
The recursion tree is the whole story: count the work at each level, then add the levels up.
Two examples show the range:
- Merge sort divides the list in half, sorts both halves, and combines by merging two sorted lists, which takes about
nsteps. There arelog nlevels and each level costsn, so the total is O(n log n) for any input. - Quickselect finds the k-th smallest by partitioning around a pivot, which puts the pivot in its final place. It then keeps only one side, the one that holds position
k. The work shrinks asn + n/2 + n/4 + ..., which is O(n) on average.
To find the running time, write a recurrence: T(n) = a T(n / b) + n^d means "a subproblems, each 1/b the size, plus n^d work to split and combine". The Master theorem solves it by comparing a with b^d:
aequalsb^d: every level does about the same work, so the total is Θ(n^d log n). Merge sort is2, 2, 1, giving Θ(n log n).ais bigger: the work grows towards the leaves, so the bottom level dominates and the total is Θ(n^(log_b a)). Karatsuba multiplication is3, 2, 1, giving about Θ(n^1.58).ais smaller: the work shrinks towards the leaves, so the top level dominates and the total is Θ(n^d). Quickselect on average is1, 2, 1, giving Θ(n).
Try it
Pick one of the three techniques, then press Run and step through.
Divide and conquer
Step 1 of 15 · Press Space to play, arrow keys to stepNumbers to sort
Numbers
Merge sort: split the list in half, sort each half the same way, then merge the two sorted halves. Splitting is free, and merging n items takes about n steps.
- comparisons:
- 0
- merges:
- 0
- n × log2 n (upper bound):
- 24
Divide and conquer splits a problem, solves the pieces, and combines the answers. The counters show the work compared with sorting or checking everything.
Things to try:
- Merge sort, "Eight numbers":
5 2 9 1 7 3 8 4is sorted with 17 comparisons, below the bound of8 × 3 = 24. Try "Already sorted": it still splits and merges every level, using 12 comparisons. - Quickselect, "3rd smallest":
7 10 4 3 20 15finds 7 after 2 partitions and looking at 10 items, where sorting would need about 18. For "Median of nine" it takes 2 partitions and looks at 15 items, against about 36 for a sort. - Master theorem, "Merge sort: 2 2 1" with
n = 32: all 6 levels do 32 work, 192 in total. Switch to "Karatsuba: 3 2 1": the levels grow from 32 up to 243 and the bottom dominates. "Quickselect average: 1 2 1" shrinks from 32 to 1, a total of 63, about2n.
Divide and conquer operations
Merge sort.
- If the range has one item or none, it is sorted. Stop.
- Find the middle. Sort the left half and the right half recursively.
- Merge: compare the front items of both halves, take the smaller, and repeat. Copy what is left.
Quickselect (k-th smallest).
- Pick a pivot, ideally random or the middle item, and partition: smaller items to the left, the rest to the right.
- The pivot is now at its final position
p. Ifpequalsk - 1, return it. - If
pis smaller thank - 1, repeat on the right side. Otherwise repeat on the left side.
Solve a recurrence.
- Read off
a(how many subproblems),b(how much smaller each is) andd(the power ofnin the extra work). - Compare
awithb^dand pick the case above.
Interview fact. Counting inversions is merge sort with one extra line: when an item from the right half is placed before the items still waiting in the left half, add the number of those left items to the count. It runs in O(n log n) instead of O(n²).
Divide and conquer time complexity
| Algorithm | Best | Average | Worst | Extra memory |
|---|---|---|---|---|
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| Quicksort | O(n log n) | O(n log n) | O(n²) | O(log n) for the recursion |
| Quickselect | O(n) | O(n) | O(n²) | O(1) after a copy |
| Binary search | O(1) | O(log n) | O(log n) | O(1) loop, O(log n) recursive |
The worst case of quicksort and quickselect happens when the pivot is always the smallest or biggest item, for example the first item of already sorted data. A random or middle pivot makes that very unlikely.
Divide and conquer vs other approaches
n is the number of items and k is the position you want.
| What you need | Divide and conquer | Sort first | Heap | Dynamic programming |
|---|---|---|---|---|
| Sort with a guaranteed O(n log n) | Merge sort | Not applicable | Heap sort, O(n log n) | Not applicable |
| The k-th smallest of an array | Quickselect, O(n) average | O(n log n) | O(n log k) | Not applicable |
| The top k of a stream | Not suitable | Not suitable | O(n log k) | Not applicable |
| Subproblems that overlap | Repeats work | Not applicable | Not applicable | Remembers answers |
| Work that can run in parallel | Pieces are independent | Hard to parallelise | Hard to parallelise | Often dependent |
The bold cells show where each wins. Divide and conquer wins when the pieces are independent. It loses when the pieces overlap.
How to explain divide and conquer in an interview
- Name the three steps out loud: divide, conquer, combine. Say what the combine step does.
- Write the recurrence and say what each part means: "two halves, plus a linear merge".
- Solve it with a recursion tree: "log n levels, n work per level".
- For quickselect and quicksort, say the worst case and the fix: "a bad pivot gives O(n²), so I use a random pivot".
- State both costs: merge sort is O(n log n) time and O(n) extra memory. Quickselect is O(n) average time and O(1) extra memory.