find a number
Look for a number in a row in order, or find it is not there. Race four searches on the same row and number.
A look reads one number of the row, at the place the search chose, and compares it with the number wanted. Interpolation search also reads the smallest and biggest numbers remaining, to aim each guess or to see the number wanted is outside them. Its own page counts none of those reads as a look. At each tick of the clock, every lane still searching makes one more look.
Each picture shows one search looking for 66 in the starting row.
Linear search looks at each number in turn, from the left, until it finds the one wanted.
Looking for 66: 6 looks, found at place 6.
- a look, numbered in turn
- found, ringed
- looked at, not it
- never reached
Binary search looks at the middle number. The order says which half holds the number wanted, and it looks at that half's middle next.
Looking for 66: 3 looks, found at place 6.
How many places on each level, from the top: 1, 2, 4, 2. Each level down costs one look more.
- looked at, joined by heavy lines
- found, ringed
Interpolation search guesses the place from how far the number wanted lies between the smallest and biggest numbers remaining.
Looking for 66: the line meets it at place 6, so guess 1 is place 6, which holds 66: found.
- the line from the smallest remaining to the biggest
- the number wanted, read across
- where they meet, as a place
- the guess, that place rounded down
Jump search splits the row into blocks and looks at each block's last number in turn. Then it walks through the one block that could hold the number wanted, stopping at it or at the first number bigger.
Looking for 66: 5 looks, found at place 6.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
- a block passed whole
- the block looked through
- a block never reached
or make one
race these, up to four
Choose two to four racers.
one tick is
Without scripts the race cannot run, so each lane shows where its racer ends.
Tick 6: no lane has made more than 6 looks. Linear search, binary search, interpolation search and jump search have finished.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
Each lane looks for 66.
linear search
done in 6 looksbinary search
done in 3 looksinterpolation search
done in 1 lookjump search
done in 5 lookswhat the marks mean
- a number being looked at
- the number found
- a number ruled out
- a number not yet ruled out
- how much of its race a lane has run
which suits this row and number
On these 9 numbers, interpolation search made the fewest looks: 1, against 3 for binary search, 5 for jump search and 6 for linear search.
each racer's own page
- linear search
- binary search
- interpolation search
- jump search
the starting racers as the row grows
In equal steps, as on the starting row, interpolation search's first guess lands on the number. Its own bigger rows are numbers picked at random, close to evenly spread, so its first guess lands near. Its page measures a mean of 7.84 places off at 1,000 numbers. With each next guess the mean falls: 1.60 places off, then 0.61. The table below shows about 3 looks there, the fewest.
First, their looks on the starting row, run once. Then on bigger rows, each number is a mean of 20 searches or more, rounded in the table. At 1,000 numbers, the means of linear, binary and jump search sit near their pages' exact averages, not on them. Linear search makes 490 here against 500.5, binary search 8.65 against 8.987, and jump search 32.2 against 32.54. In linear search's picture above, place 1 costs 1 look and each next place one more. Places 1 and 1,000 average 500.5, as do places 2 and 999, and every such pair. So over 1,000 places, the average is (1 + 1,000) / 2. Linear and binary search's pages work out a formula for any size. So the plot carries their lines on in dashes. Jump search's page gets 32.54 by searching once for the number at each place. Its formula is exact only on square rows, a number times itself. Most sizes on the plot are not, so its line has no dashes. Interpolation search's page has no exact average for rows this big. It shows these same means, so its numbers sit on them. The fewest on each line is in bold.
| linear search | binary search | interpolation search | jump search | |
|---|---|---|---|---|
| the starting row11, 22, 33, 44, 55, 66, 77, 88, 99; 66 | 6 | 3 | 1 | 5 |
| 1,000 numbers | 490 | 9 | 3 | 32 |
| 1,000,000 numbers | 489,357 | 19 | 4 | 990 |
the starting racers on 9 numbers of each shape
Shape can change the winner. On the bunched row below, most numbers crowded together and one far above them, binary search wins. With the smallest wanted, linear search and interpolation search tie at 1 look.
Looking for 8: the line meets it at place 1.6, so guess 1 is place 1, which holds 1, too small.
Looking for 88: the line meets it at place 8, so guess 1 is place 8, which holds 88: found.
Looking for 71: the line meets it at place 6.5, so guess 1 is place 6, which holds 66, too small.
- the line from the smallest remaining to the biggest
- the number wanted, read across
- where they meet, as a place
- the guess, that place rounded down
Looking for 8: 8 looks, found at place 8.
Looking for 71: 1 look, not there.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
| row | linear | binary | interpolation | jump |
|---|---|---|---|---|
| bunched, wanting 8 | 8 | 3 | 8 | 5 |
| spread evenly, wanting 88 | 8 | 3 | 1 | 5 |
| bunched, wanting 9, not there | 9 | 4 | 8 | 6 |
| spread evenly, wanting 89, not there | 9 | 4 | 1 | 6 |
Their looks on the row the race makes in each shape.
| linear search | binary search | interpolation search | jump search | |
|---|---|---|---|---|
| spread evenly11, 22, 33, 44, 55, 66, 77, 88, 99; 66 | 6 | 3 | 1 | 5 |
| bunched1, 2, 3, 4, 5, 6, 7, 8, 99; 8 | 8 | 3 | 8 | 5 |
| smallest wanted11, 22, 33, 44, 55, 66, 77, 88, 99; 11 | 1 | 3 | 1 | 2 |
| biggest wanted11, 22, 33, 44, 55, 66, 77, 88, 99; 99 | 9 | 4 | 1 | 6 |
| not there11, 22, 33, 44, 55, 66, 77, 88, 99; 71 | 9 | 3 | 1 | 4 |