📘 CodingMarble Learn

Divide and Conquer

Divide and conquer solves a big problem in three moves: divide it into smaller problems of the same kind, conquer each small problem (often by recursion, down to a trivial case), and combine the answers. Merge sort, binary search, quicksort and fast power all use it.

🎬 Step-by-step story

  1. The problem: 8 bars of different heights. Put them in order from short to tall. Sorting everything at once is heavy work.
  2. Divide: break the whole row in the middle into two halves, 4 and 4.
  3. Divide again: each 4 becomes 2 and 2, then each 2 becomes 1 and 1. A single bar is already in order. This is the easiest problem.
  4. Conquer and combine: join neighbours into pairs, putting each pair in the right order.
  5. Merge the pairs into sorted groups of four, and at last all eight in one sorted row.
  6. Free play: use the slider to move through the states, and press "New row" for a fresh mixed-up row.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why split at all?

Small problems are easy. A single bar is already sorted, and merging two small sorted rows is simple. See the split steps.

Where does splitting stop?

At single items, the base case. Step 2 shows each bar alone.

Does any sorting happen while splitting?

No. The bars only move apart. The order is fixed while merging in step 3.

How is a merge done?

Look at the first bar of each group, take the shorter one, repeat. Watch the pairs form.

How many levels are there for 8 bars?

Three (8 to 4 to 2 to 1), because 8 = 2×2×2.

Can I mix the bars up again?

Yes, press New row and move the slider through the states.

The general idea

Divide and conquer (in Romanian, Divide et Impera, Latin for "divide and rule") has three parts:

  1. Divide the problem into smaller problems of the same kind.
  2. Conquer: solve each small problem. If it is tiny (the base case), solve it directly. Otherwise do the same trick again (recursion).
  3. Combine the small answers into the answer to the big problem.

It works when the small problems are independent and when combining them is cheap. See Recursion for how a function can call itself.

Merge sort: the classic example

Sort a list of n numbers:

mergeSort(list):
  if list has 1 item: return list
  left  = mergeSort(first half)
  right = mergeSort(second half)
  return merge(left, right)

Merge two sorted lists: look at the first item of each list, take the smaller one, and repeat until both are empty. Merging n items takes about n steps.

There are about log₂ n levels of splitting (8 items: 3 levels) and each level does about n work, so the total is about n log₂ n steps. This is much faster than the simple method (bubble sort), which needs about n² steps.

Binary search: halving

To find a number in a sorted list, compare it with the middle item. If it is smaller, look only at the left half; if bigger, only the right half. Each check throws away half of the items.

For n items you need at most about log₂ n checks: 16 items need 4, 1000 items need 10, and 1 000 000 items need only 20.

Other applications

When not to use it

If the small problems overlap (the same sub-problem appears again and again, like in Fibonacci), plain divide and conquer repeats work. Use dynamic programming instead. If you must try many choices and undo them, use backtracking.

Try it

Take 8 playing cards. Deal them into two piles of 4, then 2 and 2, then single cards. Merge pairs back, always taking the smaller top card. Count how many levels you made. In the 3D, move the state slider and predict the next state before you move it.

Key formulas and definitions

Worked examples

1. Split 8 items down to single items. How many levels of splitting?

8 → 4 → 2 → 1. That is 3 levels (log₂ 8 = 3).

2. Merge the sorted lists [2, 5, 9] and [3, 4, 10].

Take the smaller front each time: 2, 3, 4, 5, 9, 10. Result: [2, 3, 4, 5, 9, 10].

3. Sort [5, 2, 7, 1] with merge sort. Show the steps.

Split: [5, 2] and [7, 1]. Split again: [5] [2] [7] [1]. Merge pairs: [2, 5] and [1, 7]. Merge: [1, 2, 5, 7].

4. How many checks does binary search need, at most, for 1000 sorted numbers?

2¹⁰ = 1024 ≥ 1000, so at most 10 checks.

5. Find 2¹⁰ using fast power. How many multiplications?

2¹⁰ = (2⁵)². 2⁵ = 2 × (2²)². 2² needs 1 multiplication, (2²)² needs 1 more, × 2 needs 1 more, and the final square needs 1 more: 4 multiplications instead of 9.

6. Search 23 in [3, 8, 15, 23, 31, 42, 57]. Show binary search.

Middle is 23 (position 4 of 7). Found in 1 check.

Common mistakes

Practice quiz

1. The three parts of divide and conquer are:
2. Merge sort on 16 items has about how many levels of splitting?
3. Binary search needs the list to be:
4. When does the real sorting work happen in merge sort?
5. Fast power computes a⁸ with how many multiplications?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between divide and conquer and recursion?

Recursion is a tool where a function calls itself. Divide and conquer is a plan (split, solve, combine) that is usually written with recursion.

Why is merge sort faster than bubble sort?

Bubble sort takes about n² steps. Merge sort takes about n × log₂ n. For 1000 items, that is about 10 000 steps instead of 1 000 000.

Is binary search divide and conquer?

Yes, a simple form. It divides the list in half, but it conquers only one half, so there is nothing to combine.

Learn first

Learn next

Related lessons

All Computer Science lessons