Complexity & Big‑O a show in 8 chapters
Goal: predict the check at 100,000 people, then make it 100 times faster
1 of 8 Your guess
Before we start
Two programs each take one second on your laptop. Which is better? Programmers have a short way of saying, and interviewers ask for it on every coding question.
Which picture is closest to what that short answer measures?
Pick the closest. You find out which fits at the end.
2 of 8 The problem
The check that slows down
You run a front desk with a long line of people. Every morning the desk's computer hunts for a matching pair, two people with the same name, by checking every person against every other person.
With 10,000 people in line it takes 51.7 ms. The lesson timed this kind of every-person-against-every-person check. Next month the line is ten times longer: 100,000 people. How long then?
Pick one. Chapter 4 works it out.
Your goal: predict the check at 100,000 people before running it, then make it at least 100 times faster.
Timing the bigger line means waiting for it, on a laptop that will be replaced. The next chapters count instead.
3 of 8 The idea
Count, don't time
Start with the desk's simplest job: find one name. The line is not sorted, so the receptionist checks faces from the front, one at a time. Today Ida is at the back.
n stands for how many people are in the line. Here n = 10. Each check of one face is one step.
Press Play to check the faces.
Double the line to 20 and the receptionist makes 20 checks. On a five-year-old laptop or this year's, still 20. The seconds change with the machine. The count of steps does not.
Steps that grow exactly as n grows are written O(n), said "order n". Naming how the steps grow, with the letter O, is Big-O.
Now suppose the person asked for always stands at the front. One check, at 10 people, at 20, at a million.
Steps that never grow are written O(1), constant.
20 × 20 = 400 checks400 ÷ 100 = 4 times the 10-person check
51.7 ms × 100 = 5,170 ms = 5.17 s
40 people: 2 multiplied by itself 40 times = 1,099,511,627,776 groups
4 of 8 The cost
Every pair
Back to the morning check. Each person is checked against every person in the line. Start small: 10 people, one row of checks each.
Press Play to check every pair.
Double the line to 20. Twice the people, and each one has twice as many to meet: 400 checks, four times as many.
Steps that grow as n × n are written O(n²), said "n squared".
Now the real line. 10,000 people make 100,000,000 checks. Ten times the people, each meeting ten times as many: 100 times the checks.
Same check, same laptop, so 100 times the time: 5.17 s, worked out without running it.
What changed
- People in line: 10,000 → 100,000
- Checks: 100,000,000 → 10,000,000,000
- Time: 51.7 ms → 5.17 s
One shape is worse still. Try every possible group from the line, each person in or out. Every extra person doubles the count. At 40 people that is about 1.1 trillion groups.
Steps that double with each extra person are written O(2ⁿ), exponential: the one shape you fight in an interview.
Odd counts round up, since nobody is half a person: half of 15,625 is 7,812 and a half, so 7,813
each row: ten times the line, about 3 more questions
5 of 8 Try it
Halve the line
Before fixing the morning check, one more trick. Sort the line by name. To find K, ask "before the middle or after it?" and set aside the half that cannot hold the name.
Press Play to ask.
A million people, sorted. Each question halves what is left, so the line shrinks fast: 20 questions reach one person.
Checking every face would take a million steps.
Make the line ten times longer and it costs only about 3 more questions.
The number of halvings that takes n down to one is the log of n, written log n. Steps that grow like it are O(log n).
Why does 10 take 4 questions?
10 halves to 5, then 3 (half of 5, rounded up, since nobody is half a person), then 2, then 1. Four questions.
the sort: 10,000 people × 13.29 halvings ≈ 132,877 stepsevery pair: 10,000 × 10,000 = 100,000,000 steps100,000,000 ÷ 132,877 ≈ 753 tiles
10,000 people
every pairmeasured
sort firstsort 3.8 ms + one walk 0.078 ms, measured
100,000 people
every pairpredicted: 51.7 ms × 100
sort firstsort 37.6 ms + one walk 0.78 ms, measured
5,170 ms ÷ 38.4 ms ≈ 135 times faster
6 of 8 The fix
Sort first
Now the fix for the morning check. Sort the line by name first. The two people with the same name end up side by side, so each person checks only the one next to them.
Sorting is not free. A good sort pays about one halving's worth of steps for each person. 8 people halve to one in 3 questions (8, 4, 2, 1), so the sort costs 8 × 3 = 24 steps.
Steps that grow as n × log n are written O(n log n).
At 10,000 people the sort costs about 132,877 steps. Every pair costs 100,000,000. The sort is about 753 times less work.
The walk down the sorted line adds 10,000 checks: 142,877 in all, still about 700 times less.
Why 13.29 halvings, not a whole number?
13 halvings take 10,000 down to about 1.2 people, 14 take it to about 0.6. The exact count sits between: 13.29. A sort is counted with the exact one, which is what the lab does too.
The lesson timed both. At 100,000 people: every pair is 5.17 s, sort first is 38.4 ms. About 135 times faster. Goal met.
What changed
- Check at 100,000 people: 5.17 s → 38.4 ms
At 10,000 it is only about 13 times faster, not 753: a sort moves people around, while a check only looks. That gap is a constant Big-O leaves out on purpose.
7 of 8 Your guess
Your picture now
Goal met: 5.17 s at 100,000 people, predicted by counting, and sorting first brought it to 38.4 ms, about 135 times faster. Which picture is closest now?
Pick one.
c is Big-O: how the steps grow as n grows. a and b both time the code, and a second on an old laptop and a second on a new one are different amounts of work.
8 of 8 Keep this
Keep this
Don't time it. Count how the steps grow as the line grows, and name the shape. A faster machine changes the seconds, never the shape.
The price: Big-O leaves out the constant. The lesson's sort did 753 times fewer steps at 10,000 and ran about 13 times faster. Pick the shape first, then measure.
Read the full lessonComing up Halving worked because every person stood in a numbered place in the line. Next: the array, a row of numbered boxes where box i sits at start + i × size, one multiply and one add away.