DSA/Lesson 1 of 11/5 min read
Complexity Analysis
Big O, Omega and Theta, time and space complexity, amortized analysis and the Master Theorem for recurrences.
On this page
Why analyze complexity
Wall-clock time depends on hardware, language and input. Complexity analysis instead counts how the number of basic operations grows with input size n. It lets you compare algorithms before writing them and predict whether a solution fits a time limit.
Rule of thumb for interviews and online judges: about 10^8 simple operations per second.
| n up to | Acceptable complexity |
|---|---|
| 10 | O(n!), O(2^n * n) |
| 20 | O(2^n) |
| 500 | O(n^3) |
| 5,000 | O(n^2) |
| 10^6 | O(n log n) |
| 10^8 | O(n) |
| Huge | O(log n), O(1) |
Asymptotic Notation
Asymptotic notation describes growth for large n, ignoring constants and lower-order terms.
Big O
Upper bound. f(n) = O(g(n)) if there exist constants c and n0 such that f(n) <= c * g(n) for all n >= n0. In practice we use it for the worst case.
Example: 3n^2 + 5n + 7 = O(n^2).
Big Omega
Lower bound. f(n) = Omega(g(n)) if f(n) >= c * g(n) for large n. Comparison-based sorting is Omega(n log n).
Big Theta
Tight bound. f(n) = Theta(g(n)) when it is both O(g(n)) and Omega(g(n)). Merge sort is Theta(n log n) in every case.
Common growth rates
From fastest to slowest:
O(1) < O(log n) < O(sqrt n) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n!)Time Complexity
Count how many times the dominant operation runs.
# O(n): one pass
for x in arr:
total += x
# O(n^2): nested loops over the same input
for i in range(n):
for j in range(n):
work(i, j)
# O(log n): input halves every step
while n > 1:
n //= 2
# O(n log n): log n levels, n work per level
def merge_sort(a): ...Rules of thumb
- Sequential blocks add:
O(n) + O(n^2) = O(n^2). - Nested loops multiply: loop of
ninside loop ofmisO(n * m). - Drop constants:
O(2n)isO(n). - Different inputs, different variables: iterating two arrays
aandbisO(a + b), notO(n). - A loop where
idoubles (i *= 2) runsO(log n)times.
Best, average and worst case
| Algorithm | Best | Average | Worst |
|---|---|---|---|
| Linear search | O(1) | O(n) | O(n) |
| Binary search | O(1) | O(log n) | O(log n) |
| Quick sort | O(n log n) | O(n log n) | O(n^2) |
| Hash table lookup | O(1) | O(1) | O(n) |
Space Complexity
Extra memory used as a function of n, usually auxiliary space (excluding the input).
- Iterative sum:
O(1). - Creating a copy of the array:
O(n). - Recursion uses the call stack: recursive depth
dcostsO(d)space. Recursive binary search isO(log n)space; naive recursive Fibonacci isO(n)space (depth), even though time isO(2^n).
Pitfall: forgetting that recursion is not free. A DFS on a skewed tree of 10^5 nodes can overflow the stack.
Amortized Analysis
Average cost per operation over a sequence of operations, even if a single operation is occasionally expensive. Not the same as average case (no probability involved).
Dynamic array example
A dynamic array (Python list, Java ArrayList, C++ vector) doubles capacity when full. A resize copies all n elements, which is O(n), but it happens rarely.
Inserts: 1 2 3 4 5 6 7 8 9
Copy cost: 0 1 2 0 4 0 0 0 8
Total copies for n inserts <= 1 + 2 + 4 + ... + n < 2nSo n appends cost O(n) total, which is O(1) amortized per append.
Methods: aggregate method (total / n), accounting method (prepay credits), potential method.
Recurrence Relation
Recursive algorithms are described by recurrences:
| Algorithm | Recurrence | Result |
|---|---|---|
| Binary search | T(n) = T(n/2) + O(1) | O(log n) |
| Merge sort | T(n) = 2T(n/2) + O(n) | O(n log n) |
| Naive Fibonacci | T(n) = T(n-1) + T(n-2) + O(1) | O(2^n) |
| Linear recursion | T(n) = T(n-1) + O(1) | O(n) |
| Tree traversal | T(n) = 2T(n/2) + O(1) | O(n) |
Master Theorem
For T(n) = a T(n/b) + f(n) with a >= 1, b > 1, compare f(n) with n^(log_b a):
- If
f(n) = O(n^(log_b a - e))for somee > 0:T(n) = Theta(n^(log_b a)). Leaves dominate. - If
f(n) = Theta(n^(log_b a)):T(n) = Theta(n^(log_b a) * log n). Balanced. - If
f(n) = Omega(n^(log_b a + e))and regularity holds:T(n) = Theta(f(n)). Root dominates.
Worked examples:
- Merge sort:
a = 2, b = 2,n^(log_2 2) = n,f(n) = n. Case 2, soTheta(n log n). - Binary search:
a = 1, b = 2,n^0 = 1,f(n) = 1. Case 2, soTheta(log n). - Strassen:
a = 7, b = 2,n^2.81vsn^2. Case 1, soTheta(n^2.81).
Quick revision
- Big O is the upper bound, Omega the lower bound, Theta the tight bound.
- Drop constants and lower-order terms; nested loops multiply, sequential blocks add.
- Recursion depth counts toward space complexity.
- Amortized cost spreads rare expensive operations across many cheap ones: dynamic array append is
O(1)amortized. - Master Theorem: compare
f(n)withn^(log_b a); three cases. - About
10^8operations per second is the usual budget.