Math Fundamentals18 sections · 814 units
Open in Course

The Halving Pattern (Where log n comes from)

Divide and conquer

O(logn)O(\log n) appears whenever you repeatedly divide the problem size by a constant factor. The most common pattern is halving.

Start with nn items. After one step, you have n2\frac{n}{2}. After two steps, n4\frac{n}{4}. After three steps, n8\frac{n}{8}. After kk steps, you have n2k\frac{n}{2^k} items left.

You stop when you reach 11 item, meaning n2k=1\frac{n}{2^k} = 1. Solving for kk: 2k=n2^k = n, so k=log2(n)k = \log_2(n). That's where the logarithm comes from.