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?
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
loop in a loop, time
19,931,569 sort steps1,000,000,000,000 loop steps
run it
Add items. When does the loop inside a loop hurt?
101001,00010,000100,0001,000,000
Each step up is ten times the items. Green is the sort's steps, red is the loop inside a loop's steps.
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.
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.
Preparing for interviews? The ten-week plan puts this in order with DSA and the design rounds.
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.
A million items. O(n log n) finishes in a blink. How long does O(n²) take at the same speed?
The ladder, as towers
a token climbing every tower at the same speed, so tall means slow
the sort, n log n, beside the loop inside a loop, n², in red
a rung too large to print: a short stub under a red lid
Seven towers, one per rung, from O(1) at the left to O(n!) at the right. Each is as tall as its operation count on a log scale, so one floor is a tenfold step. Drag n and the skyline rebuilds: at ten every tower is a stump, at a million the red one stands far above the green one.
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.
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
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
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
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.
How long does the report take now?
The dials wait until you make the call.
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.
How many times less work is the sort than comparing every pair?
The dials wait until you make the call.
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.
How many stops can it plan in under a minute?
The dials wait until you make the call.
The ladder
Rung
How to spot it
Operations at n = 1,000,000
Time at the assumed rate
O(1)constant
no loop over the input: an array index, a hash lookup, a fixed number of steps
1
instant
O(log n)logarithmic
the search range halves (or shrinks by another constant fraction) every step
20
instant
O(n)linear
one loop over the input, doing a constant amount of work per item
1,000,000
1 ms
O(n log n)linearithmic
a loop that halves its range, run once per item, or a sort
19,931,569
20 ms
O(n²)quadratic
a loop inside a loop, each over the input, comparing every item with every other
1,000,000,000,000
16 min 40 s
O(2ⁿ)exponential
a function that calls itself twice per item, visiting every subset of the input
never
never
O(n!)factorial
every ordering of the input, with one level of nesting per item
never
never
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.
next labThe 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?