Begin Algo
Algorithms · 4 lessons

Divide & ConquerSplit, solve, combine

Divide and conquer is where O(n log n) comes from: halve the problem, recurse, merge the results. Merge sort and quicksort are its famous examples; here it is treated as a general method, with the Master Theorem for working out the cost.

Why learn Divide & Conquer

Where it shows up
Why halving makes it faster

Comparing n things pairwise takes n², but splitting in half, solving each and merging takes n log n. The Master Theorem saves you from deriving the recurrence every time.

Cryptography and big numbers

RSA raises a number to a several-hundred-digit power modulo another. Fast exponentiation halves the exponent each step, finishing in a few hundred multiplications instead of never.

→ Lesson: Fast Exponentiation
"How many pairs are out of order?"

Measuring how far apart two rankings are is really counting inversions. Count them during merge sort's merge step and it costs nothing extra.

→ Lesson: Count Inversions

Lessons

4 lessons