Algorithmic Complexity
The lesson that an elegant algorithm beats any supercomputer — provided n is large enough.
Your computer processes billions of operations per second. Yet a simple nested loop can grind to a halt on 100,000 items. The bottleneck isn't your hardware — it's the growth pattern of your algorithm. Big-O notation gives you a tool to see this pattern at a glance and predict bottlenecks before they happen.
No formal math required — just a shift in how you think about "fast" and "slow."
Scaling — Why Hardware Cannot Rescue Bad Algorithms
Definition
Big-O Notation
Analogy
Definition
The library analogy hides that binary search requires pre-sorted data — sorting itself costs O(n log n) (“n times log n”: a little more than linear, and the typical class of good sorting algorithms). In a real library you can scan shelves visually and skip sections, making the search richer than the pure algorithmic model.
Note
Binary Search: Step by Step
- 1. Start: 1,000,000 books — check the middle
- 2. Eliminate half → 500,000 remaining
- 3. Halve again → 250,000, then 125,000 ...
- 4. After 10 steps: only ~1,000 books left
- 5. After ~20 steps: book foundout of 1 million!
Each time you double the number of books, you need only one additional search step. That is the power of logarithmic growth.
At n = 100,000: ~10 billion comparisons. Compares adjacent elements and swaps them. Simple, but extremely slow on large datasets.
At n = 100,000: ~1.7 million comparisons. Recursively splits, sorts, and merges. Thousands of times faster on large data.
Caution
Misconception: A Faster Processor Solves the Problem
Faster hardware is just a constant factor. With exponential growth, a computer that is twice as fast lets you process only one more unit of n. The growth rate dominates any constant.
Take a computer that processes 10 billion operations per second (today's supercomputers manage over 10¹⁸). With a linear algorithm O(n) at n = 1 billion: done in 0.1 seconds. With an exponential algorithm O(2ⁿ)? A completely different story.
n = 50: 2⁵⁰ ≈ 1.1 × 10¹⁵ operations → hours
n = 100: 2¹⁰⁰ ≈ 1.27 × 10³⁰ operations
→ Longer than the age of the universe (~4.3 × 10¹⁷ seconds)
→ Even at 10¹⁸ operations/s: around 40,000 yearsThis is why exponential algorithms become impractical at surprisingly small n — no matter how fast your hardware is.
O(n) vs. O(n²) — The Four Everyday Classes
Note
Potatoes vs. Handshakes
Imagine a dinner party. Peeling potatoes (O(n)): one per guest — double the guests, double the potatoes. Handshakes (O(n²)): every guest shakes hands with every other guest.
Guests Potatoes Handshakes
4 4 6
10 10 45
100 100 4,950
1,000 1,000 499,500Ten times more guests: 10× more potatoes, but ~100× more handshakes. That is the difference between linear and quadratic growth.
In reality, you can peel potatoes in parallel (more hands, just a constant factor) and greet guests in groups — the latter corresponds, in CS, to designing better algorithms that avoid the all-pairs pattern.
Caution
Misconception: O(n²) Is Always Bad
For small n (e.g. 20 items), a simple O(n²) algorithm is often faster than a complex O(n log n) algorithm with high overhead. Big-O becomes critical when n can grow unpredictably — user data, network traffic, database tables.
Real standard libraries use hybrid strategies: for small arrays, a simple O(n²) algorithm like Insertion Sort is used (less overhead). Only for larger datasets does the library switch to an asymptotically better O(n log n) algorithm like Merge Sort. The best of both worlds.
Interactive: Compare Growth Patterns
Move the slider to vary the input size n from 1 to 1,000. Watch how differently O(log n), O(n), O(n log n), and O(n²) grow — especially beyond n = 100, the gap between linear and quadratic growth becomes dramatic.
At n=100, the difference becomes visible: O(n²) requires 10,000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).
Ratio to O(n)
| Complexity | Operations | Factor vs. O(n) |
|---|---|---|
| O(log n) | 6.6 | 15x faster |
| O(n) | 100 | 1x (Reference) |
| O(n log n) | 664 | 6.6x slower |
| O(n²) | 10,000 | 100x slower |
Interactive: The Sorting Race
Two sorting methods compete on the very same data. Predict who finishes first and feel why O(n log n) pulls further ahead as n grows.
Time vs. Space — The Trade-Off
Definition
Time-Space Trade-Off
Analogy
Definition
In reality, trade-offs are often non-linear: a small cache can speed up access by a factor of 1,000. Some problems, however, have exponential space requirements where more memory doesn't help.
Note
Fibonacci: Exponential vs. Memoized
Naive Recursion vs. Memoization
Instead of billions of calls, only 49 actual computations. A small memory investment (49 values) buys an exponential speedup.
Note
In AI: LLM Optimisation
Large language models use the same trade-off: full precision (32 bits) needs lots of memory; today models mostly ship in 16 bits, some already in 8 or 4 bits. Quantisation (8 or 4 bits) halves or quarters memory use compared with 16 bits and speeds up inference — at a controlled quality cost. Here, memory space is traded for mathematical precision — the Big-O class does not change, but the hardware load is massively reduced.
Caution
Misconception: Less Memory Is Always Better
Deliberate memory use (caches, indices, precomputed tables) is one of the most powerful optimisation techniques. The goal is not minimal memory but the right balance — invest memory where it saves disproportionate computation time.
When you learn about Python dictionaries in later articles, you will see exactly this trade-off again: dictionaries invest memory in hash tables to speed up access from O(n) to O(1) on average.
Note
Key Takeaways
- Big-O measures growth shape, not clock time. A faster computer only delays the problem — it never solves a bad growth rate.
- Four classes cover most of everyday thinking: O(1) constant, O(log n) logarithmic, O(n) linear, O(n²) quadratic.
- Time and space are two currencies you trade against each other. Caching, memoization, and indexing are deliberate memory investments to buy speed.
Check
Quiz: Big-O Notation
What does Big-O notation describe?
Select one answer
1. What does Big-O notation describe?
2. You double the number of items in a list. An algorithm now takes four times as long. What is its Big-O class?
3. A naive Fibonacci function calls itself billions of times for fib(50). Which technique reduces this to only 49 real computations?
4. An O(n²) sorting algorithm is sometimes preferred over an O(n log n) algorithm. In which situation is this reasonable?
Check
Checkpoint: Do You Understand Big-O?
Can you explain this in your own words? Tick what you've got.
0 of 3 done