Let's explore every common Big O complexity with real code examples. Understanding these by heart is essential.
O(1) — Constant Time
The operation takes the same time regardless of input size.
O(log n) — Logarithmic
The input is halved each step. Binary search is the classic example.
O(n) — Linear
We visit each element once.
O(n²) — Quadratic
Nested loops. Common in brute-force solutions.
Speed ranking from fastest to slowest:
complexity_of("single operation")
O(1)
complexity_of("loop through array")
O(n)
complexity_of("nested loops")
O(n^2)
complexity_of("binary search")
O(log n)
complexity_of("sorting")
O(n log n)