Computer Science I / Divide-and-Conquer Reasoning
Practice question · Multiple choice

Merge sort and binary search are both divide-and-conquer, yet one is O(n log n) and the other O(log n). Why does the same strategy give such different costs?

Hints
  1. How many of the two halves does each algorithm actually visit?
  2. Count the total work at one level of the recursion for each.
Show the answer

A. Because merge sort recurses into both halves and merges O(n) per level.

Why

Both halve the problem, so both have about log n levels; what differs is the work per level. Binary search discards half and does constant work; merge sort recurses into both halves and touches every element merging, giving O(n) per level. That is what the Master Theorem formalises — how many subproblems, how much smaller, how much combining — and it lets you predict a design change.

Read the lesson: Divide-and-Conquer Reasoning →

Practise Divide-and-Conquer Reasoning

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Divide-and-Conquer Reasoning