Skip to content
Chethan Yadav_
Data Structures & Algorithms

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 toAcceptable complexity
10O(n!), O(2^n * n)
20O(2^n)
500O(n^3)
5,000O(n^2)
10^6O(n log n)
10^8O(n)
HugeO(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 n inside loop of m is O(n * m).
  • Drop constants: O(2n) is O(n).
  • Different inputs, different variables: iterating two arrays a and b is O(a + b), not O(n).
  • A loop where i doubles (i *= 2) runs O(log n) times.

Best, average and worst case

AlgorithmBestAverageWorst
Linear searchO(1)O(n)O(n)
Binary searchO(1)O(log n)O(log n)
Quick sortO(n log n)O(n log n)O(n^2)
Hash table lookupO(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 d costs O(d) space. Recursive binary search is O(log n) space; naive recursive Fibonacci is O(n) space (depth), even though time is O(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 < 2n

So 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:

AlgorithmRecurrenceResult
Binary searchT(n) = T(n/2) + O(1)O(log n)
Merge sortT(n) = 2T(n/2) + O(n)O(n log n)
Naive FibonacciT(n) = T(n-1) + T(n-2) + O(1)O(2^n)
Linear recursionT(n) = T(n-1) + O(1)O(n)
Tree traversalT(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):

  1. If f(n) = O(n^(log_b a - e)) for some e > 0: T(n) = Theta(n^(log_b a)). Leaves dominate.
  2. If f(n) = Theta(n^(log_b a)): T(n) = Theta(n^(log_b a) * log n). Balanced.
  3. 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, so Theta(n log n).
  • Binary search: a = 1, b = 2, n^0 = 1, f(n) = 1. Case 2, so Theta(log n).
  • Strassen: a = 7, b = 2, n^2.81 vs n^2. Case 1, so Theta(n^2.81).
The Master Theorem does not apply when subproblems are unequal, like T(n) = T(n-1) + n. Use the recursion tree or substitution method instead.

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) with n^(log_b a); three cases.
  • About 10^8 operations per second is the usual budget.