Skip to content
ByteStepStart with recursion

Divide and conquer

Free, no account

Three moves, always the same three. Divide the problem into smaller versions of itself. Conquer each one the same way. Combine the answers into an answer for the whole thing.

Below, the same three moves applied to three problems that look nothing alike: adding up an array, raising a number to a power, and sorting. The trees are almost identical. What differs is what the combining costs, and that is what decides whether any of it was worth doing.

Start with total, which is the honest case where splitting gains nothing at all. Then power, where it turns twenty multiplications into 6. Then sort, which is merge sort from the previous lesson, seen as the shape it is.

1function total(a, lo, hi) {
2 if (hi - lo === 1) return a[lo] // one item
3 const mid = (lo + hi) >> 1
4 const left = total(a, lo, mid) // divide
5 const right = total(a, mid, hi)
6 return left + right // combine
7}

The split

each node divides until a piece answers itself

One problem, about to be cut in half. Then each half in half, until a piece is small enough to answer without thinking.

1/24
Detail
24 steps

The three moves, why the tree stays shallow, and when splitting buys nothing.

The three moves

Watch any of the three run and the pattern is the same. A node appears and immediately splits, without solving anything. Its children split. That continues until a piece is small enough to answer outright, which is the base case from the recursion lesson wearing a different hat.

Then the answers come back up, and this is where the work happens. Nothing was computed on the way down. Every node is waiting for both its children so it can combine them, which is the same way-back-up shape the very first lesson showed with factorial.

Why the tree stays shallow

Splitting in half is what keeps these trees short. Eight items reach single items in 4 levels; a thousand items would take ten; a million, twenty. That count is log n, and it is the same log that appeared in binary search and in merge sort, for the same reason: you can only halve a number so many times.

So the cost of a divide and conquer algorithm is almost always how many levels multiplied by what one level costs. The levels are nearly always log n. The interesting part is the other factor.

When it buys nothing

Run total and count. Adding eight numbers by splitting costs eight additions, which is exactly what adding them left to right costs. The tree is prettier and the work is identical.

This matters more than the success stories. Divide and conquer is not a speed-up; it is a way of organising a problem. It only becomes a speed-up when combining two solved halves is genuinely cheaper than solving the whole from scratch.

When it buys everything

Now power. Computing 2 to the twentieth by multiplying twenty times costs twenty multiplications. By splitting, it costs 6, because 2 to the twentieth is simply the square of 2 to the tenth. Every level halves what is left to do.

And sort. Sorting a whole array by comparing every pair is quadratic. Merging two already-sorted halves is linear, because you only ever look at their two front values. Linear work at each of log n levels is O(n log n), and that is the entire reason merge sort beats bubble sort.

Know someone stuck on this? Send it to them.

A classmate, a study group, someone learning this on their own at midnight. The link opens the lesson set up exactly as you have it, the same function and the same number, and runs from the first step so they watch the whole thing build rather than landing in the middle of it.

Also in this course

Sorting: why O(n log n) beats O(n²)

Merge sort counted comparison by comparison. This lesson shows the shape underneath it.

Start there →