Algorithm Analysis and Complexity

Lesson 2 of 20

Count loop steps by hand, fill a triangular pair census, then watch O(n squared) explode as n keeps doubling.

The big picture

Counting cost

Big-O is a vocabulary for cost: it counts operations instead of seconds, and keeps only the term that dominates as n grows.

Operations, not seconds

Never time code with a stopwatch — machines lie. Count steps: a loop over n does n units of work.

Constants get dropped

3n + 20 steps is just O(n). As n grows, only the fastest-growing term survives.

The growth ladder

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ). Each rung dwarfs the one below it.

Best vs worst

Linear search finds item 1 instantly (best) or scans everything (worst). Big-O quotes the worst case.

Amortized push_back

vector doubling looks expensive, but averaged over many pushes each push costs O(1).