3.1.2Complexity Analysis

Common complexities — O(1), O(log n), O(n), O(n log n), O(n²), O(n³), O(2ⁿ), O(n!)

1,997 words9 min readdifficulty · medium

WHAT: The growth ladder

We rank algorithms by how their step count T(n) scales as n → ∞. From fastest (best) to slowest (worst):

O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)O(1) < O(\log n) < O(n) < O(n\log n) < O(n^2) < O(n^3) < O(2^n) < O(n!)

The < here means "grows strictly slower for large n". We drop constants and lower-order terms because for huge n only the dominant term matters (that's the whole point of asymptotics — see 3.1.01-Big-O-Notation).

Figure — Common complexities — O(1), O(log n), O(n), O(n log n), O(n²), O(n³), O(2ⁿ), O(n!)

HOW each class arises (derived from first principles)


Why the ordering is what it is


Feel the numbers (Forecast-then-Verify)


Common mistakes (Steel-man + fix)


Recall Feynman: explain to a 12-year-old

Imagine you have a giant pile of homework sheets n.

  • O(1): you only check the top sheet — same effort no matter how tall the pile.
  • O(log n): it's a sorted pile and you play "higher/lower", tearing the pile in half each guess — even a million sheets takes ~20 guesses.
  • O(n): you read every sheet once.
  • O(n²): for every sheet, you compare it to every other sheet (gossip: everyone talks to everyone).
  • O(2ⁿ): for each sheet you decide keep-or-toss, and you try every combination — the choices double each new sheet.
  • O(n!): you try every possible order to stack them — hopeless past a handful. The faster the pile of work explodes when sheets are added, the worse the algorithm.

Flashcards

Which complexity has step count independent of n?
O(1) — constant.
Why is binary search O(log n)?
Each step halves the search space; n/2k=1k=log2nn/2^k=1 \Rightarrow k=\log_2 n.
Two sequential O(n) loops give what complexity?
O(n) — sequential loops add, not multiply.
Why is the log base irrelevant in O(log n)?
Changing base multiplies by a constant (logbn=log2n/log2b\log_b n=\log_2 n/\log_2 b), and Big-O drops constants.
Derive the complexity of merge sort.
T(n)=2T(n/2)+cnT(n)=2T(n/2)+cn; recursion tree has log2n\log_2 n levels of cncn work each → O(nlogn)O(n\log n).
What is i=1ni\sum_{i=1}^{n} i and its Big-O?
n(n+1)2=O(n2)\frac{n(n+1)}{2}=O(n^2).
Where does O(2ⁿ) come from in subset problems?
Each element is take/skip → 2n2^n subsets; or recurrence T(n)=2T(n1)+cT(n)=2T(n-1)+c.
Which grows faster, 2ⁿ or n!, and why?
n! — it's a product of n factors most of which exceed 2; overtakes 2ⁿ from n=4.
At n=10, compare n² and 2ⁿ.
n²=100, 2ⁿ=1024; exponential already larger past n≈4.
Naive n×n matrix multiplication is what complexity?
O(n³) — three nested loops.
Order these: O(n!), O(1), O(n log n), O(2ⁿ), O(n²).
O(1) < O(n log n) < O(n²) < O(2ⁿ) < O(n!).
Signature of O(log n) in code?
A variable divided/multiplied by a constant factor each iteration (not subtracted).

Connections

Concept Map

basis for

no loop

halve input

one pass

split and merge

nested loops

triple loop

binary choice per item

all permutations

slower than

slower than

slower than

slower than

slower than

slower than

slower than

Big-O drops constants

Growth ladder by n

O 1 constant

O log n logarithmic

O n linear

O n log n linearithmic

O n2 quadratic

O n3 cubic

O 2n exponential

O n! factorial

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, complexity classes basically ek "growth ka vocabulary" hain. Sawaal yeh hai: jaise-jaise input n bada hota hai, tumhare code ka kaam kitni tezi se badhta hai? Agar kaam constant rehta hai (O(1)) to mast — input chahe ek ho ya ek crore, same time. Agar har step me problem aadhi ho jaati hai (jaise binary search), to woh O(log n) — bahut slow grow karta hai, kyunki n/2k=1n/2^k = 1 se k=log2nk = \log_2 n aata hai.

Phir O(n) matlab har element ko ek baar chhuo, O(n log n) matlab merge sort jaisa (log levels, har level pe linear kaam). Nested loops aate hi cheezein multiply hoti hain: O(n²), O(n³). Aur jab har element pe "take ya skip" jaisa choice hota hai to O(2ⁿ) — exponential, aur jab saare orderings try karne padein to O(n!) — factorial, sabse worst.

Sabse important practical baat: O(2ⁿ) aur O(n!) bade n ke liye "intractable" hote hain. n=20 pe n! ka matlab 77 saal lag sakte hain ek normal computer pe! Isiliye hum dynamic programming jaise tricks se exponential ko polynomial me convert karte hain. Yaad rakhna — sequential loops add hote hain (O(n)+O(n)=O(n)), sirf nested loops multiply karte hain. Aur log ka base matter nahi karta kyunki woh sirf ek constant se multiply hota hai jo Big-O drop kar deta hai.

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections