Divide and conquer
Split a problem into independent smaller parts, solve each recursively, and combine the answers: the idea behind merge sort, quicksort and binary search.
Description
Divide and conquer has three steps. Divide the problem into smaller subproblems of the same kind. Conquer each one, recursively, until they're small enough to solve directly. Combine the sub-answers into the answer for the whole.
The technique pays off when combining is cheap compared with solving the whole problem directly. Merge sort splits an array in half, sorts both halves, and merges them in linear time, giving O(n log n) instead of O(n²). Binary search divides and then only needs to conquer one half. Quicksort does its work in the divide step (partitioning), so combining is free.
The running time follows a recurrence, such as T(n) = 2T(n/2) + O(n) for merge sort, which the master theorem solves: here O(n log n). Halving the input at each level is what produces the log n factor.
Because subproblems are independent, divide-and-conquer algorithms parallelize naturally, which is the idea behind MapReduce and parallel sorting. When subproblems overlap instead, the same work is repeated and dynamic programming is the better fit.
How it works
Merge sort's recursion: split down to single values, then merge back up
- 38 27 43 3 → 38 27, 43 3
- 38 27 → 38, 27
- 43 3 → 43, 3
- 38
- 27
- 43
- 3
Step through: merge sort, divide and conquer in action
Step 1 of 25Start with 8 values.
Complexity
| Operation | Average | Worst |
|---|---|---|
| T(n) = 2T(n/2) + O(n): merge sort | O(n log n) | O(n log n) |
| T(n) = T(n/2) + O(1): binary search | O(log n) | O(log n) |
| T(n) = 2T(n/2) + O(1): tree traversal | O(n) | O(n) |
Space: O(log n) recursion depth for balanced splits, plus any combine buffers
TypeScript implementation
Fast exponentiation and maximum subarray sum, two divide-and-conquer algorithms beyond sorting.
// Two divide-and-conquer algorithms beyond sorting.
/** base^exp in O(log exp) multiplications, by squaring. */
export function power(base: bigint, exp: number): { value: bigint; multiplications: number } {
if (exp === 0) return { value: 1n, multiplications: 0 };
const half = power(base, Math.floor(exp / 2)); // divide
let value = half.value * half.value; // combine
let m = half.multiplications + 1;
if (exp % 2) {
value *= base;
m++;
}
return { value, multiplications: m };
}
/** Largest sum of a contiguous subarray: the best is in the left half, the right half, or crosses the middle. */
export function maxSubarray(a: number[], lo = 0, hi = a.length - 1): number {
if (lo === hi) return a[lo];
const mid = (lo + hi) >> 1;
let left = -Infinity, sum = 0;
for (let i = mid; i >= lo; i--) left = Math.max(left, (sum += a[i]));
let right = -Infinity;
sum = 0;
for (let i = mid + 1; i <= hi; i++) right = Math.max(right, (sum += a[i]));
return Math.max(maxSubarray(a, lo, mid), maxSubarray(a, mid + 1, hi), left + right);
}
export function demo(): string[] {
const p = power(3n, 100);
return [
`3^100 = ${p.value.toString().slice(0, 12)}… in ${p.multiplications} multiplications instead of 99`,
`best run of daily profit in [3, -7, 4, -1, 5, -2, -6, 8]: ${maxSubarray([3, -7, 4, -1, 5, -2, -6, 8])}`,
];
}When to use it
Use it when
- The problem splits into independent parts of the same kind.
- Combining partial answers is cheaper than solving the whole problem directly.
- Work can be parallelized across cores or machines.
Avoid it when
- Subproblems overlap: use dynamic programming.
- The input is small: the recursion overhead outweighs the benefit; switch to a simple method below a threshold.
Pitfalls
- Unbalanced splits, which lose the log n benefit (quicksort with bad pivots).
- Expensive combine steps that cancel out the gains.
- Recursing all the way down to size one instead of switching to a simpler algorithm for small inputs.
Alternatives
Dynamic programming
Choose it when subproblems overlap and their answers can be reused.
Greedy algorithms
Choose it when one local decision at a time leads to the optimum.