Data Structures & Algorithms / Module 0: Thinking in cost
Topic 0.2
Big-O
A way to estimate how much more work code does when the data grows.
Watch: slide the amount of data to the right and compare the five pillars. Some barely move; some shoot upwards.
| Rate | Steps | Estimated time |
|---|---|---|
| O(1) constant | 1 | < 1 µs |
| O(log n) logarithmic | 10 | < 1 µs |
| O(n) linear | 1,000 | 10.0 µs |
| O(n log n) n log n | 10,000 | 100 µs |
| O(n²) quadratic | 1,000,000 | 10.0 ms |
Picture looking for a friend in a classroom
There are four ways to look, and each grows differently as the class gets bigger:
- Your friend always sits in the front seat. You walk straight there. Ten people or a thousand, the work is the same. That is O(1).
- The register is sorted by name. You open it in the middle, see whether the name comes before or after, and throw away half. A thousand people take about 10 looks. That is O(log n).
- You ask people one at a time. A class twice as big means twice the work. That is O(n).
- You want to know whether two people share a birthday, and you do it by comparing everyone with everyone else. A class twice as big means four times the work. That is O(n²).
The letter n just means "how many items". Big-O ignores how fast the computer is; it only looks at how the work grows as n grows.
Why it matters
Move n in the figure. With 100 items all five pillars are far below the red 1-second line, so everything feels fast. With 1,000,000 items, O(n²) needs about 2.8 h, while O(n log n) is done in 200 ms. Both programs give the right answer; the only difference is how their work grows. That is why a program that runs smoothly on a laptop with sample data can grind to a halt on a real server.
| Rate | 10× more data, the work… |
|---|---|
| O(1) | stays the same |
| O(log n) | grows by a few steps |
| O(n) | is 10× more |
| O(n log n) | is a bit more than 10× |
| O(n²) | is 100× more |
What it looks like in code
You don't need formulas. Usually the shape of the loops tells you:
Code, for programmersNo loop: O(1)
function first(list) {
return list[0]; // one step, however long the list is
}Code, for programmersOne loop over all the data: O(n)
function total(list) {
let sum = 0;
for (const x of list) sum += x; // one step per item
return sum;
}Code, for programmersA loop inside a loop over the same data: O(n²)
function hasDuplicate(list) {
for (let i = 0; i < list.length; i++)
for (let j = i + 1; j < list.length; j++) // every pair is compared
if (list[i] === list[j]) return true;
return false;
}Code, for programmersSorting with the built-in sort: O(n log n)
const sorted = [...list].sort((a, b) => a - b);Quick rules of thumb
- Nested loops multiply. A loop inside a loop over the same data means n × n.
- Loops one after another add. Two separate loops are still O(n), because n + n is only double.
- Multipliers are ignored. 3n and n are both O(n). The number still matters in real life, but it doesn't change whether the code blows up as the data grows.
- The biggest part wins. If part of the code is O(n²) and part is O(n), the whole thing is O(n²).
Quiz
A function has two separate (not nested) loops, each over n items. What is its Big-O?
Which is slowest for 1,000,000 items?
Roughly how many "looks" does it take to find one name in a sorted list of 1,000,000 names, if every look throws away half the list?
Code calls list.includes(x) inside a for loop over the same list. What is its Big-O?
0 of 4 answered.