Skip to content

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.

    111122223333444455556666777888999
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • never reached
Every number before 66 is looked at once, then 66 itself.

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.

level 1level 2level 3level 4112233445566778899111222333444555666777888999

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
The starting row as a tree: each number over its own place, one level lower for each look. The search for 66 follows the heavy lines: 55, then 77, the first of the two middles of the four remaining, then 66.

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.

    1112223334445556667778889996661
  • 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
The starting row goes up in equal steps, 11 a place. The boxed ends, 11 and 99, aim the guess, and reading them is no look. 66 is 55 of the 88 from 11 to 99, five eighths of the way. Five eighths of the 8 steps from place 1 is 5 steps: place 6, which holds 66.

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.

    11122233314443555466625777888999block 1block 2block 3
  • 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
3 blocks of 3 numbers: on these 9, as many blocks as numbers in each. Block 1 ends on 33, smaller than 66, so it is passed. Block 2 ends on 66, not smaller. A jump asks only whether a block's last number is smaller, not whether it is the one wanted. So the walk goes back through block 2: 44, 55, then 66 again.

or make one

race these, up to four

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.

Each lane looks for 66.

linear search

done in 6 looks
112233445566778899

binary search

done in 3 looks
112233445566778899

interpolation search

done in 1 look
112233445566778899

jump search

done in 5 looks
112233445566778899

what 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

The same racers on bigger rows: average looks up, how many numbers across, each labelled grid line ten times the last. A solid line is measured; a dashed one carries on the formula its own page derives.
102104106101102103104105106107101102103104105106107101102103104105106107
  • 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 searchbinary searchinterpolation searchjump search
the starting row11, 22, 33, 44, 55, 66, 77, 88, 99; 666315
1,000 numbers4909332
1,000,000 numbers489,357194990

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.

    112233445566778899981.61
  • Looking for 88: the line meets it at place 8, so guess 1 is place 8, which holds 88: found.

    1112223334445556667778889998881
  • Looking for 71: the line meets it at place 6.5, so guess 1 is place 6, which holds 66, too small.

    111222333444555666777888999716.51
  • 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
The first two rows want their eighth number; the third wants 71, not in the row. Bunched, 99 pulls the line up. 8 is 7 of the 98 from 1 to 99, 7 over 98 of the way. That share of the 8 steps from place 1 is about 0.57 steps, so the line meets 8 near place 1.6. The first guess is place 1, seven places short. Spread evenly, the line meets 88 at place 8. Looking for 71: 71 is 60 of the 88 from 11 to 99, and 60 over 88 of the 8 steps from place 1 is about 5.5. So the line meets 71 near place 6.5, and the guess, rounded down, is place 6.
  • Looking for 8: 8 looks, found at place 8.

    111222333444555666777888999
  • Looking for 71: 1 look, not there.

    1112223334445556661777888999
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • ruled out without a look
  • never reached
Bunched, each guess lands on the first place remaining, one place right of the last, until the eighth finds 8. Looking for 71, the guess at place 6 reads 66, too small, so 77 is the smallest remaining. 71 is below 77, so it is not there.
rowlinearbinaryinterpolationjump
bunched, wanting 88385
spread evenly, wanting 888315
bunched, wanting 9, not there9486
spread evenly, wanting 89, not there9416
Linear, binary and jump search ask of each number read only whether it is the one wanted, smaller or bigger, never how far off. On two rows in order holding as many numbers, neither holding a number twice, the number at the same place costs them the same looks. So does a number missing from the same gap. Here the bunched row and the evenly spread row each hold 9 numbers. Each wants its eighth, then a number between its eighth and ninth, not there. Interpolation search's looks change with the numbers.

Their looks on the row the race makes in each shape.

linear searchbinary searchinterpolation searchjump search
spread evenly11, 22, 33, 44, 55, 66, 77, 88, 99; 666315
bunched1, 2, 3, 4, 5, 6, 7, 8, 99; 88385
smallest wanted11, 22, 33, 44, 55, 66, 77, 88, 99; 111312
biggest wanted11, 22, 33, 44, 55, 66, 77, 88, 99; 999416
not there11, 22, 33, 44, 55, 66, 77, 88, 99; 719314