Why it matters

Choosing the right algorithm for the problem size is what separates code that scales from code that dies at 10x growth. Big-O is the tool for reasoning about that choice before writing any code.

Advertisement

The architecture

Big-O is an upper bound on growth rate. O(f(n)) means the algorithm's cost grows at most proportionally to f(n) as n grows large. Constants and lower-order terms don't matter — 3n² + 5n + 7 is O(n²).

Common classes: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n) linearithmic, O(n²) quadratic, O(2^n) exponential.

Complexity analysis dimensionsBest caselower boundAverage casetypical inputWorst caseupper bound Big-OBig-O is worst-case; Big-Theta is tight; Big-Omega is lower bound
Complexity notations.
Advertisement

How it works end to end

Analyzing a loop: outer loop N times, inner loop N times = O(n²). Nested loops of different sizes multiply. Sequential loops add.

Recursive analysis: use the master theorem for divide-and-conquer, or draw the recursion tree.

Amortized analysis: some operations cost more but average out. Dynamic array append is amortized O(1) despite occasional O(n) resize.