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
Big-O Notation
The library analogy hides that binary search requires pre-sorted data — sorting itself costs O(n log n). In a real library you can scan shelves visually and skip sections, making the search richer than the pure algorithmic model.
Binary Search: Step by Step
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. A thousand times faster on large data.
Misconception: A Faster Processor Solves the Problem
Deep Dive: The Supercomputer Loses
O(n) vs. O(n²) — The Four Everyday Classes
Potatoes vs. Handshakes
In reality, you can peel potatoes in parallel and greet guests in groups — in CS, this corresponds to designing better algorithms that avoid the all-pairs pattern.
Misconception: O(n²) Is Always Bad
Deep Dive: Hybrid Sorting in Practice
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), 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
Time-Space Trade-Off
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.
Fibonacci: Exponential vs. Memoized
In AI: LLM Optimisation
Misconception: Less Memory Is Always Better
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).
Key Takeaways
Quiz: Big-O Notation
Checkpoint: Do You Understand Big-O?
- Why does an exponential algorithm fail even on the world's fastest supercomputer once n grows large enough?
- You double the input data. An algorithm now takes four times as long. Which Big-O class is this?
- Naive Fibonacci recursion is extremely slow. Which technique reduces the runtime from O(2ⁿ) to O(n), and what does it cost?