Algorithm Analysis: Time and Space Complexity
Core Titles
Key headlines and terms for quick recall- Algorithm analysis — predict resource use without running
- Time complexity — # of steps as function of input
- Space complexity — memory used
- Asymptotic notation: (upper), (lower), (tight)
- Common classes:
- Best, Average, Worst cases
- Amortised analysis
- Master theorem for divide-and-conquer recurrences
Basic Idea
What it is, why it matters, how it worksWhy analyse?
Two algorithms solving the same problem can differ by a factor of millions on real data. Algorithm analysis predicts resource use mathematically, without needing to run on every input.
We care about:
- Time — how many basic operations.
- Space — how much memory.
Asymptotic notation
We describe growth as , ignoring constants and lower-order terms.
- Big-O — upper bound. for large .
- Big-Omega — lower bound.
- Big-Theta — tight bound (upper + lower).
We usually quote worst-case in interviews and texts.
Common growth classes
| Notation | Name | Example |
|---|---|---|
| Constant | array index | |
| Logarithmic | binary search | |
| Linear | one pass | |
| Linearithmic | merge sort, FFT | |
| Quadratic | bubble sort | |
| Cubic | naive matrix multiplication | |
| Exponential | brute-force subset enumeration | |
| Factorial | brute-force permutations / TSP |
For : is ops — too slow for one second. is — fine.
Three case analyses
- Best case — luckiest input (rarely useful).
- Average case — expected behaviour on random input.
- Worst case — guaranteed upper bound (most useful for safety).
For quicksort: best , average , worst (already-sorted).
Amortised analysis
Some operations are sometimes expensive but on average cheap. Amortised cost averages over a sequence:
- Dynamic array (Python list) — append is amortised because doubling-and-copying happens rarely.
How to analyse
- Identify the "basic operation" (comparison, arithmetic).
- Count how many times it runs as a function of input size.
- Drop constants and lower-order terms.
def linear_search(arr, x):
for i in range(len(arr)): # n iterations
if arr[i] == x: # 1 op
return i
return -1
# Worst case T(n) = n → O(n)
Recurrences and the Master Theorem
For divide-and-conquer :
- If → .
- If → .
- If (and regularity) → .
Merge sort: .
Space complexity
Sometimes the bottleneck. Recursive algorithms use stack space; sorting in-place vs out-of-place differs.
Why it matters in Data Science
- Knowing matrix ops scale poorly tells you to switch to mini-batches or specialised libraries.
- ML training and inference latency depends on this analysis.
- Choosing the right data structure (hash table vs list) flips to .
Mind Map
Visual structure of the conceptALGORITHM ANALYSIS
├── Time vs Space complexity
├── Asymptotic notation
│ ├── O — upper bound
│ ├── Ω — lower bound
│ └── Θ — tight
├── Growth classes
│ └── O(1) < log n < n < n log n < n² < 2ⁿ < n!
├── Three cases
│ ├── Best
│ ├── Average
│ └── Worst
├── Amortised analysis (e.g., dynamic array append)
├── Master theorem for T(n) = aT(n/b) + f(n)
└── Practical impact (n = 10⁶, op limits)
Exam Q&A
Part A (2 marks) and Part B (20 marks) style questionsPart A (2 marks each)
Q1. Define time and space complexity.
- Time complexity — number of basic operations an algorithm performs as a function of input size .
- Space complexity — amount of memory required as a function of , including input, auxiliary and output.
Q2. What is Big-O notation? A mathematical notation that describes the asymptotic upper bound on an algorithm's growth: iff for some constant and all .
Q3. Order the following from slowest to fastest: , , , , . .
Part B (20 marks)
Q. Explain algorithm analysis. Discuss time and space complexity, asymptotic notations and common complexity classes. Why is the analysis of algorithms important?
Why analyse?
Two algorithms solving the same problem can differ by a factor of millions on real input. Empirical timing is brittle — performance changes with machine, compiler, data. Asymptotic analysis predicts growth mathematically and lets us reason about scalability.
Time complexity — number of basic operations as a function of input size . Space complexity — memory required as a function of .
Asymptotic notation.
| Notation | Meaning | Use |
|---|---|---|
| — upper bound | Worst-case guarantees | |
| — lower bound | Best-case / lower-bound proofs | |
| Both — tight bound | Exact growth class |
We drop constants and lower-order terms — is .
Common complexity classes.
| Class | Name | Example |
|---|---|---|
| Constant | Array index, hash lookup | |
| Logarithmic | Binary search | |
| Linear | Single pass through array | |
| Linearithmic | Merge sort, heap sort, FFT | |
| Quadratic | Bubble / selection sort | |
| Cubic | Naive matrix multiply | |
| Exponential | Brute-force subset enumeration | |
| Factorial | Brute-force TSP |
Concrete impact at (assume ops/sec):
| Class | Operations | Time |
|---|---|---|
| 1 ms | ||
| 20 ms | ||
| ~17 min | ||
| astronomical | universe-age |
Three cases.
- Best case — luckiest input.
- Average case — expected over random input.
- Worst case — guaranteed upper bound (used in safety-critical systems).
Quicksort: best , average , worst (already-sorted, bad pivot).
Amortised analysis.
Sometimes individual operations are expensive but on average cheap. Dynamic array append (Python list, C++ vector) is amortised — though resizing copies the whole array, it happens rarely.
Recurrences (Master Theorem). For divide-and-conquer :
| Case | Condition | Solution |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | + regularity |
Example (merge sort): (Case 2).
Why this matters.
- Predict whether an algorithm will scale to production data.
- Compare candidate algorithms without coding both.
- Justify infrastructure decisions — "your algorithm will take 15 hours on 10⁶ rows; switch to ."
- In ML, the same idea drives matrix-multiplication cost ( generally) and explains why batching, GPUs and approximate algorithms (e.g., random projections) are crucial.
Worked example.
Sorting 10 million numbers:
- Bubble sort = ops — infeasible.
- Merge sort ops — 0.5 s.
The choice of algorithm is the difference between completing and timing out.