SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm PerformanceBig O & Time ComplexitySpace ComplexityLogarithms and ComplexityProblem Solving ApproachAlgorithm Efficiency and Problem Solving
Big O Notation

Logarithms and Complexity

Count halving steps, distinguish them from midpoint inspections, and compare the work per level in binary search and merge sort.

Logarithms and Complexity Growth

A logarithm is the mathematical inverse of exponentiation.

If:

bx=nb^x = nbx=n

then:

log⁡b(n)=x\log_b(n) = xlogb​(n)=x

In plain English, a logarithm asks: what power do we need to raise bbb to in order to get nnn?

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 (nnn)Base 2 Logarithm (log⁡2n\log_2 nlog2​n)Exponent Equivalent (2x=n2^x = n2x=n)
11100020=12^0 = 120=1
22211121=22^1 = 221=2
44422222=42^2 = 422=4
88833323=82^3 = 823=8
16161644424=162^4 = 1624=16
32323255525=322^5 = 3225=32
64646466626=642^6 = 6426=64
1,0241{,}0241,024101010210=1,0242^{10} = 1{,}024210=1,024
1,048,5761{,}048{,}5761,048,576 (~1M)202020220=1,048,5762^{20} = 1{,}048{,}576220=1,048,576
1,073,741,8241{,}073{,}741{,}8241,073,741,824 (~1B)303030230=1,073,741,8242^{30} = 1{,}073{,}741{,}824230=1,073,741,824

The Halving Intuition

A great way to think about log⁡2(n)\log_2(n)log2​(n) in programming is: how many times can you cut nnn in half until you reach 1?

For example, starting with n=16n = 16n=16: 16→÷28→÷24→÷22→÷2116 \xrightarrow{\div 2} 8 \xrightarrow{\div 2} 4 \xrightarrow{\div 2} 2 \xrightarrow{\div 2} 116÷2​8÷2​4÷2​2÷2​1 It takes 4 cuts to bring 161616 down to 111, so log⁡2(16)=4\log_2(16) = 4log2​(16)=4.


O(log⁡n)O(\log n)O(logn) Growth

For a process that repeatedly halves its input, doubling the starting size adds one halving. This is the pattern behind logarithmic growth.

Example: Binary Search

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 ⌊log⁡2n⌋+1\lfloor\log_2 n\rfloor + 1⌊log2​n⌋+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.

Worst-case linear search inspects n items. For 1 through 16 items, binary search increases to 2, 3, 4, and 5 inspections at lengths 2, 4, 8, and 16 respectively.

Items in List (nnn)Linear Search O(n)O(n)O(n) Max StepsBinary Search O(log⁡n)O(\log n)O(logn) Max Steps
888888444
646464646464777
1,0241{,}0241,0241,0241{,}0241,024111111
1,000,0001{,}000{,}0001,000,000 (1 Million)1,000,0001{,}000{,}0001,000,000202020
1,000,000,0001{,}000{,}000{,}0001,000,000,000 (1 Billion)1,000,000,0001{,}000{,}000{,}0001,000,000,000303030

If you double a list from 111 million to 222 million items, a worst-case linear search checks an extra 1,000,0001{,}000{,}0001,000,000 items. The worst-case binary search needs one extra midpoint inspection.


O(nlog⁡n)O(n \log n)O(nlogn) Growth

An O(nlog⁡n)O(n \log n)O(nlogn) algorithm (sometimes called linearithmic) combines two parts:

  • nnn: doing work on all nnn items (like looking through or merging items at each step).
  • log⁡n\log nlogn: repeating that work across log⁡n\log nlogn split levels or stages.
Eight items, sorted in runs ofMerges on this levelItems processed across the level
1 item4 merges of two singletons8
2 items2 merges of two pairs8
4 items1 merge of two runs of four8

Merge sort is one example of this growth rate.

Example: Merge Sort

Merge Sort splits a list in half over and over until each piece has only 1 item (which takes log⁡n\log nlogn 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.

Comparing O(log⁡n)O(\log n)O(logn) and O(nlog⁡n)O(n \log n)O(nlogn)

ComplexityGrowth SpeedTypical Use Case
O(log⁡n)O(\log n)O(logn)One additional halving per doublingSearching (like Binary Search)
O(nlog⁡n)O(n \log n)O(nlogn)A linear amount of work at each of logarithmically many levelsFast Sorting (like Merge Sort)

Logarithms in Recursion

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 log⁡2n\log_2 nlog2​n 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 calls

The total time depends on how many branches the function explores and how much work it does at each level:

  1. Checking only one half →O(log⁡n)\to O(\log n)→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×log⁡2n levels=O(log⁡n)\text{Total Work} = O(1) \text{ per level} \times \log_2 n \text{ levels} = O(\log n)Total Work=O(1) per level×log2​n levels=O(logn)

  2. Working on both halves →O(nlog⁡n)\to O(n \log n)→O(nlogn) Time: If the code visits both branches and does O(n)O(n)O(n) work merging them (like merge sort), every level adds up to nnn work: Total Work=n work per level×log⁡2n levels=O(nlog⁡n)\text{Total Work} = n \text{ work per level} \times \log_2 n \text{ levels} = O(n \log n)Total Work=n work per level×log2​n levels=O(nlogn)


Key Takeaways

  • A base-2 logarithm counts the exact number of halvings from a power of two to one.
  • Binary search follows one remaining interval and uses one midpoint inspection per iteration. The last candidate still needs checking.
  • Merge sort processes all items at each of its logarithmically many merge levels, giving O(n log n) time.
  • Tree depth describes one path. To estimate total work, also count the work across every branch at each level.

How is this lesson?

Previous lessonSpace ComplexityNext lessonProblem Solving Approach
Edit on GitHubReport on GitHub
On this page
%

On this page

Logarithms and Complexity GrowthO(log⁡n)O(\log n)O(logn) GrowthExample: Binary SearchO(nlog⁡n)O(n \log n)O(nlogn) GrowthExample: Merge SortComparing O(log⁡n)O(\log n)O(logn) and O(nlog⁡n)O(n \log n)O(nlogn)Logarithms in RecursionKey Takeaways