Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Big-O lab1 of 5
1,000,000 items to handle a sort 20 ms a loop inside a loop 16 min 40 s about 50,000 times longer

the story

1,000,000 items. How long does your code take?

Cost depends on how the code is shaped. A sort does a little work per item. A loop inside a loop compares every item with every other item. We count steps, and assume a machine that does 1,000,000,000 steps a second.

With 1,000,000 items, do both take about the same time?

a sort 20 ms 19,931,569 steps a loop inside a loop ? time your guess every item meetsevery other item both are for 1,000,000 items

guess first

A sort takes 20 ms. How long does the loop inside a loop take?

16 min 40 sfor a loop inside a loop. A sort: 20 ms

19,931,569 sort steps 1,000,000,000,000 loop steps

run it

Add items. When does the loop inside a loop hurt?

1,000,000 items

Each step up is ten times the items. Green is the sort's steps, red is the loop inside a loop's steps.

10,000 items100 million steps 100,000 items10 billion steps 10 times the items,100 times the work

why it explodes

Ten times the items is a hundred times the work.

Every item meets every other item. So the work is n times n, a square. Make the side ten times longer and the area is a hundred times bigger.

the working

10,000 × 10,000 = 100,000,000 steps100,000 × 100,000 = 10,000,000,000 steps10,000,000,000 ÷ 100,000,000 = 100 times1,000,000 × 1,000,000 = 1,000,000,000,0001,000,000,000,000 ÷ 1,000,000,000 = 1,000 s
unsortedsorted AshaRaviMeenaAshaDevRaviKiranDev AshaAshaDevDevKiranMeenaRaviRavi find the repeated names in a list of 1,000,000

your call

Find repeated names in 1,000,000. Compare pairs, or sort first?

DSABig-O: growth

How cost grows

A million items. O(n log n) finishes in a blink. How long does O(n²) take at the same speed?

Every rung of the ladder is a shape of code, from a single loop to a loop inside a loop to every ordering of the input. One dial moves the input size, and the operation counts and times beside each rung move with it. Small inputs make the rungs look alike, and a million items pulls them apart.

try this Drag Input size, n up to a million and watch the Off the chart panel count rungs that now read never.

1,000,000

Every time on this page assumes 1,000,000,000 operations a second. This is an assumption, not a measurement. The operation counts are exact. A different rate would change the times, but the gap between rows stays the same shape.

O(n log n)

a sort, or a search that halves per item

19,931,569

operations

20 msat the assumed rate

O(n²)

a loop inside a loop, both over the input

1,000,000,000,000

operations

16 min 40 sat the assumed rate

Off the chart

rungs whose count at this n is too large to print

2

of 7 rungs are already "never"

The working

Where the operation counts and times come from, redone every time you move the slider.

  1. A sort: operationsone halvingfor each itemHalving 1,000,000 items down to one takes about 20 steps, 19.931569 to be exact, and a sort pays that once for every item. 1,000,000 × 19.931569= 19,931,569
  2. A sort: time20 ms01 s1,000,000,000 operations a second is 1,000,000 every thousandth of a second (a millisecond). Divide the sort's 19,931,569 operations by that to get milliseconds. 19,931,569 ÷ 1,000,000= 20 ms
  3. A loop inside a loop: operationssort19.9 millionloop in a loop1,000,000 millionEvery one of the 1,000,000 items meets all 1,000,000 again. Counted in thousands that is 1,000 thousand times 1,000 thousand, and a thousand times a thousand is a million. 1,000 × 1,000= 1,000,000 million
  4. A loop inside a loop: time16 min 40 s0Counted in millions, the loop inside a loop is 1,000,000 million operations, and the machine does 1,000 million a second. 1,000 seconds is 16 min 40 s. 1,000,000 ÷ 1,000= 1,000 s

Call 1: ten times the customers

A report compares every customer with every other customer, a loop inside a loop. With 10,000 customers it runs in 100 ms. The list grows ten times, to 100,000.

Call 2: sort it first

To find repeated names in a list of 10,000, you can compare every pair, or sort the list so repeats sit side by side. A sort halves its way down once for every item.

Call 3: every ordering

A delivery planner tries every ordering of its stops and keeps the shortest route. With 10 stops it finishes in 4 ms. One more stop multiplies the orderings by the new number of stops.

The ladder

RungHow to spot itOperations at n = 1,000,000Time at the assumed rate
O(1) constantno loop over the input: an array index, a hash lookup, a fixed number of steps1instant
O(log n) logarithmicthe search range halves (or shrinks by another constant fraction) every step20instant
O(n) linearone loop over the input, doing a constant amount of work per item1,000,0001 ms
O(n log n) linearithmica loop that halves its range, run once per item, or a sort19,931,56920 ms
O(n²) quadratica loop inside a loop, each over the input, comparing every item with every other1,000,000,000,00016 min 40 s
O(2ⁿ) exponentiala function that calls itself twice per item, visiting every subset of the inputnevernever
O(n!) factorialevery ordering of the input, with one level of nesting per itemnevernever

Read it off the code

One loop over n, with a constant amount of work per item, is linear. A loop inside a loop, each over the input, multiplies the work: quadratic. Halving the search range every step gives logarithmic. A sort that halves its range once per item is n log n. You can read the class from the nesting in the code.

Drop the constants

As n grows, only the biggest term matters. 2n + 3 is just n, because the plus three stops mattering once n is more than a handful. n² + 100n is just n², because the 100n term is a rounding error once n reaches the thousands. Big-O keeps only the term that decides how cost grows.

Pick the complexity first

Machines get faster every year, so a benchmark today says little about next year's hardware. Operations as a function of n do not change when the CPU does. Fix a quadratic loop before you shave milliseconds off it.

Limits of this model

1,000,000,000 operations a second is an assumption, not a measurement. No real machine runs at a constant rate. Cache misses, branch prediction, garbage collection and parallelism move the true number by orders of magnitude. A different rate changes every time on this page. The gap between rungs keeps its shape, and that shape is what this lab shows.

Dropping the constants can be wrong at small n. Big-O keeps only the leading term. A linear algorithm with a large constant can lose to a quadratic one with a tiny constant until n grows large enough. This lab shows only the leading term and does not model that crossover. At small n, a real choice between two algorithms sometimes has to.

"Never" is a cap, not infinity. O(2ⁿ) and O(n!) are computed only while the result is still a readable duration. Past that, the lab shows "never" instead of a number with hundreds of digits. The true counts are finite, but they grow too large to print long before n reaches a million.

Sources

On a narrow screen, the ladder scrolls sideways. The slider and the key numbers stack above it.

the lesson Read the code in this track in twenty minutes
The whole idea this lab came from, told start to finish.

on the path Module 1 of 11 in DSA: The toolbox
The lessons, boards and videos around this lab, in reading order.

Board: Complexity and Big-O

see also The Bitter Lesson, MapReduce: Simplified Data Processing on Large Clusters and Validity of the Single Processor Approach to Achieving Large Scale Computing Capabilities in Papers.

next lab The selectivity crossover
Your query has an index and Postgres refuses to use it. At what fraction of the table does reading all of it in order start to win?