Count halving steps, distinguish them from midpoint inspections, and compare the work per level in binary search and merge sort.
A logarithm is the mathematical inverse of exponentiation.
If:
bx=nthen:
logb(n)=xIn plain English, a logarithm asks: what power do we need to raise b to in order to get n?
Exponent: 2⁴ = 16 (2 multiplied by itself 4 times equals 16)
Logarithm: log₂(16) = 4 (How many times do we multiply 2 to get 16? Answer: 4)Halving a problem naturally gives a base-2 logarithm. If a search kept one third of the candidates each time, base 3 would describe the number of reductions. Changing between fixed bases greater than 1 multiplies the result by a constant, so either process has logarithmic growth.
| Value (n) | Base 2 Logarithm (log2n) | Exponent Equivalent (2x=n) |
|---|---|---|
| 1 | 0 | 20=1 |
| 2 | 1 | 21=2 |
| 4 | 2 | 22=4 |
| 8 | 3 | 23=8 |
| 16 | 4 | 24=16 |
| 32 | 5 | 25=32 |
| 64 | 6 | 26=64 |
| 1,024 | 10 | 210=1,024 |
| 1,048,576 (~1M) | 20 | 220=1,048,576 |
| 1,073,741,824 (~1B) | 30 | 230=1,073,741,824 |
The Halving Intuition
A great way to think about log2(n) in programming is: how many times can you cut n in half until you reach 1?
For example, starting with n=16: 16÷28÷24÷22÷21 It takes 4 cuts to bring 16 down to 1, so log2(16)=4.
For a process that repeatedly halves its input, doubling the starting size adds one halving. This is the pattern behind logarithmic growth.
In a sorted list, Binary Search looks right at the middle item. If the number we want is smaller than the middle item, we throw away the whole right half. If it's bigger, we throw away the left half. We repeat this until we find the item or run out of numbers.
For the target 8 in [1, 2, 3, 4, 5, 6, 7, 8], round the midpoint down. The successive midpoint values are 4 → 6 → 7 → 8. Three halvings reduce eight candidates to one. You still need a fourth inspection to test that final candidate.
With this inclusive interval and a lower midpoint, the worst case for a nonempty array takes ⌊log2n⌋+1 midpoint inspections. The chart counts one inspected array item per step. From 8 through 15 items, binary search needs at most four inspections. At 16 items, its maximum increases to five.
| Items in List (n) | Linear Search O(n) Max Steps | Binary Search O(logn) Max Steps |
|---|---|---|
| 8 | 8 | 4 |
| 64 | 64 | 7 |
| 1,024 | 1,024 | 11 |
| 1,000,000 (1 Million) | 1,000,000 | 20 |
| 1,000,000,000 (1 Billion) | 1,000,000,000 | 30 |
If you double a list from 1 million to 2 million items, a worst-case linear search checks an extra 1,000,000 items. The worst-case binary search needs one extra midpoint inspection.
An O(nlogn) algorithm (sometimes called linearithmic) combines two parts:
| Eight items, sorted in runs of | Merges on this level | Items processed across the level |
|---|---|---|
| 1 item | 4 merges of two singletons | 8 |
| 2 items | 2 merges of two pairs | 8 |
| 4 items | 1 merge of two runs of four | 8 |
Merge sort is one example of this growth rate.
Merge Sort splits a list in half over and over until each piece has only 1 item (which takes logn levels of splitting). It then merges those pieces back together in sorted order.
For eight items, the runs after each merge level have lengths 2, 4, and 8. Each level processes all eight items across its merges. Doubling to sixteen items adds one level and doubles the number of items at each level.
| Complexity | Growth Speed | Typical Use Case |
|---|---|---|
| O(logn) | One additional halving per doubling | Searching (like Binary Search) |
| O(nlogn) | A linear amount of work at each of logarithmically many levels | Fast Sorting (like Merge Sort) |
A recursion tree has logarithmic depth when each child is at most a fixed fraction of its parent. Subtracting one at each call instead creates a linear-depth chain.
For a power-of-two input, the following tree has log2n edges along each path from the root to a leaf. Level zero contains the root:
Level 0: [Size n] --> 1 call
/ \
Level 1: [Size n/2] [Size n/2] --> 2 calls
/ \ / \
Level 2: [n/4] [n/4] [n/4] [n/4] --> 4 calls
... ... ... ...
Level log₂(n): [ 1 ] [ 1 ] [ 1 ] [ 1 ] --> n callsThe total time depends on how many branches the function explores and how much work it does at each level:
Checking only one half →O(logn) Time: If the code only follows one branch down (like recursive binary search), the total work is simply the height of the tree: Total Work=O(1) per level×log2n levels=O(logn)
Working on both halves →O(nlogn) Time: If the code visits both branches and does O(n) work merging them (like merge sort), every level adds up to n work: Total Work=n work per level×log2n levels=O(nlogn)
O(n log n) time.