Skip to content

Interpolation search

finding

Level 2 of 3

how it works

The idea in a few sentences and three pictures, then one search step by step.

Interpolation search needs a row in order, smallest first. It guesses where the number wanted should stand from how far it lies between the smallest and biggest numbers remaining. You open a dictionary the same way, near the back for a word starting with W. Found, it stops. Too small or too big, the order says which side to keep. A number outside the smallest and biggest is not there, and costs no look. Its plain name is guessing the place; to interpolate is to guess a value between two known ones.

  • Looking for 57: the line meets it at place 7.8, so guess 1 is place 7, which holds 50, too small.

    31112183274335426507578669741081119012577.81
  • Looking for 9: the line meets it at place 1.5, so guess 1 is place 1, which holds 2, too small.

    2132436475869798891.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
Join the smallest and the biggest with a line, and read across at the number wanted: the guess is where they meet, rounded down. In nearly equal steps, 57 meets it at place 7.8, next to 57 itself. Bunched, 9 meets it at place 1.5, six places short.
stepworked for 57
how far 57 is above 3, the smallest54
how far 90, the biggest, is above 387
54 over 87, the share of the wayabout 0.62
steps from place 1 to place 1211
0.62 of 11 stepsabout 6.8
place 1, then 6.8 steps onplace 7.8
rounded downplace 7, which holds 50
The first picture's crossing in numbers. Every guess on this page is worked out the same way.
  • Looking for 57: 2 looks, found at place 8.

    3111218327433542650715782669741081119012
  • Looking for 60: 1 look, not there.

    311121832743354265075781669741081119012
  • Looking for 95: 0 looks, not there.

    31112183274335426507578669741081119012
  • Looking for 90: 1 look, found at place 12.

    311121832743354265075786697410811190121
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • ruled out without a look
  • never reached
One row, four searches. 57 costs 2 looks: the first lands on 50, and then 57 is the smallest remaining. 60 costs 1: 57 is too small, so the part left is 66 to 90, and 60 is below it. 95 costs none, being past 90. 90, the biggest, is where the line ends.
Bunched at the top: 7 moments of its 7 steps, the steps between them left for the watch page
  1. step 1 of 7

    190919293949599

    There are 8 numbers in a row, in order, smallest first. Look for 50 by guessing where it should stand.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    1
    part ends at place
    8
    guessed place
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 2 of 7

    190919293949599

    50 lies between 1 and 99, the smallest and biggest left, so in proportion it should stand near place 4. Place 4 holds 92.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    1
    part ends at place
    8
    guessed place
    4

    watch from here

  3. step 3 of 7

    190919293949599

    92 is bigger than 50, so 50 can only be to its left. Rule out places 4 to 8.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    1
    part ends at place
    8
    guessed place
    4

    watch from here

  4. step 4 of 7

    190919293949599

    50 lies between 1 and 91, the smallest and biggest left, so in proportion it should stand near place 2. Place 2 holds 90.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    1
    part ends at place
    3
    guessed place
    2

    watch from here

  5. step 5 of 7

    190919293949599

    90 is bigger than 50, so 50 can only be to its left. Rule out places 2 to 3.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    1
    part ends at place
    3
    guessed place
    2

    watch from here

  6. step 6 of 7

    190919293949599

    50 is outside 1 to 1, the smallest and biggest numbers left, so it cannot be here. Rule them all out: 50 is not in the row.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    not yet
    part ends at place
    not yet
    guessed place
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  7. step 7 of 7

    190919293949599

    Done after 2 guesses.

    what it remembers to know where it is
    looking for
    50
    part starts at place
    not yet
    part ends at place
    not yet
    guessed place
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. the part to search is the whole rowPython line 8
  2. if the number wanted lies outside the part's smallest and biggest, it is not therePython lines 10, 11, 12 and 13
  3. guess its place from where it lies between them, and look therePython lines 14, 15, 16, 17, 18 and 19
  4. if it is the number wanted, it is foundPython line 20
  5. if it is smaller, keep only the part to its rightPython lines 21 and 22
  6. if it is bigger, keep only the part to its leftPython lines 23 and 24
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

def interpolation_search(row, target):
    """Guess where target stands in a row in order, smallest first, from
    how far it lies between the smallest and biggest numbers left.

    Returns its place, counted from 0 as Python counts places, or None
    when target is not in the row.
    """
    low, high = 0, len(row) - 1  # the part to search is the whole row
    while True:  # each pass finds it, gives up, or keeps a smaller part
        above = target - row[low]  # how far target is above the smallest
        span = row[high] - row[low]  # how far the biggest is above it
        if above < 0 or above > span:  # outside the smallest and biggest
            return None  # so it is not in the row
        if span == 0:  # every number left is the same
            guess = low
        else:  # as far along the places as target is along the numbers
            guess = low + above * (high - low) // span
        number = row[guess]
        if number == target:  # one look: is this the number wanted?
            return guess  # found
        if number < target:  # the same look: is it smaller?
            low = guess + 1  # keep only the part to its right
        else:
            high = guess - 1  # keep only the part to its left

how it scales

How the looks grow, from ten numbers to ten million.

Each step across or up is ten times the one before. In equal steps, every number in the row takes 1 look, a flat line. Bunched, the most is n−1n - 1: about ten times the numbers, ten times the looks. Numbers picked at random sit between. Their points climb about one look each time the row is squared, from 100 to 10,000, a little less from 10 to 100, as section 4's fourth question shows. Each point averages 20,000 searches.

guesses, ten times more at each mark up

slope 1102104106101102103104105106107slope 1101102103104105106107101102103104105106107slope 1101102103104105106107101102103104105106107

nn, how many numbers are in the row, ten times more at each mark to the right

  • a number in a bunched row: 1 to n−1n - 1, then one far bigger number, looking for n−1n - 1solid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
  • a number in the row, the row picked at random from 1 to four times nn, each place as likelypoints only, each measured by a real run, since no single formula for the guesses of this row is worked out here
  • a number in the row where the first guess lands, as in equal stepssolid where measured by running it; dashed where its formula carries it on to ten million
what a dearest row costs, at a billion guesses a second
nnguessestime
1,000999about 999 nanoseconds
100,00099,999about 100 microseconds
10,000,0009,999,999about 10 milliseconds

The guesses are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count the looks of interpolation search yourself.

Run it with Python 3.12 or later. For each size n it prints the looks for
the middle number of a row spread evenly, 1 to n (the cheapest: 1); for the
number just below the end of a bunched row, 1 to n - 1 then one far bigger
number (the dearest: n - 1); and the average over every place of 20 rows
of n different numbers picked at random from 1 to 4n, beside log2 log2 n.
Its random rows are not this page's, so its averages may differ from the
page's by a few tenths of a look. The counts are the same on any computer;
only the time differs.
"""
import math
import random


class Counted:
    """A number that counts every look: each new pair of numbers compared.

    A second question about the same pair, such as == then <, is the same
    look. Subtracting is no look: the guess is made from the differences.
    """

    looks = 0
    last = None  # the pair compared last

    def __init__(self, value):
        self.value = value

    def _look(self, other):
        pair = {id(self), id(other)}
        if pair != Counted.last:
            Counted.looks += 1
            Counted.last = pair

    def __eq__(self, other):
        self._look(other)
        return self.value == other.value

    def __lt__(self, other):
        self._look(other)
        return self.value < other.value

    def __sub__(self, other):
        return self.value - other.value


def interpolation_search(row, target):
    """Guess where target stands in a row in order, smallest first, from
    how far it lies between the smallest and biggest numbers left.

    Returns its place, counted from 0 as Python counts places, or None
    when target is not in the row.
    """
    low, high = 0, len(row) - 1  # the part to search is the whole row
    while True:  # each pass finds it, gives up, or keeps a smaller part
        above = target - row[low]  # how far target is above the smallest
        span = row[high] - row[low]  # how far the biggest is above it
        if above < 0 or above > span:  # outside the smallest and biggest
            return None  # so it is not in the row
        if span == 0:  # every number left is the same
            guess = low
        else:  # as far along the places as target is along the numbers
            guess = low + above * (high - low) // span
        number = row[guess]
        if number == target:  # one look: is this the number wanted?
            return guess  # found
        if number < target:  # the same look: is it smaller?
            low = guess + 1  # keep only the part to its right
        else:
            high = guess - 1  # keep only the part to its left


def count(row, target):
    """How many looks interpolation search makes looking for target in row."""
    Counted.looks, Counted.last = 0, None
    interpolation_search([Counted(x) for x in row], Counted(target))
    return Counted.looks


if __name__ == "__main__":
    random.seed(1)
    print(f"{'n':>6} {'spread':>7} {'bunched':>8} {'average':>8} {'loglog':>7}")
    for n in [10, 100, 1000]:
        spread = list(range(1, n + 1))
        bunched = list(range(1, n)) + [max(99, n * n - 3 * n + 4)]
        total = 0
        for _ in range(20):
            row = sorted(random.sample(range(1, 4 * n + 1), n))
            total += sum(count(row, number) for number in row)
        average = total / (20 * n)
        loglog = math.log2(math.log2(n))
        print(f"{n:>6} {count(spread, (n + 1) // 2):>7} "
              f"{count(bunched, n - 1):>8} {average:>8.2f} {loglog:>7.2f}")

best, worst and average

Each case looks for a number that is in the row. One search then costs from 1 look to n−1n - 1, from 2 numbers on.

A case is one kind of input, the thing the method is given, such as a row back to front, whatever its length. A bound says how fast that case's cost grows as the input gets longer.

The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
besta number in the row where the first guess lands, as in equal stepsaveragea number in the row, the row picked at random from 1 to four times nn, each place as likelyworsta number in a bunched row: 1 to n−1n - 1, then one far bigger number, looking for n−1n - 1
guesses1, the first guess lands on itmeasured, a little under log⁡2  log⁡2 n\log_2\, \,\log_2\, n from 10 numbers on: about 4.09 at a millionn−1n - 1, from 2 numbers on
how the guesses grow in each case
tight, Θ\ThetaΘ(1)\Theta(1)not derived here, known to grow like log⁡2  log⁡2 n\log_2\, \,\log_2\, nΘ(n)\Theta(n)
ceiling, OOO(1)O(1)O(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(1)\Omega(1)Ω(1)\Omega(1)Ω(n)\Omega(n)

extra memory: the part's two ends and the guess, beside the row

Extra memory is the room it needs on top of the input itself, while it runs.

  • Looking for 65: 1 look, found at place 6.

    511722934145356561777898
  • Looking for 9: 7 looks, found at place 7.

    211322433644755866977988
  • Looking for 23: 1 look, found at place 4.

    812322332341235306
  • Looking for 95: 0 looks, not there.

    51172293414535656777898
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • ruled out without a look
  • never reached
In equal steps, 1 look. Bunched, 7 looks for 8 numbers, one place per look. With equal numbers the guess may land on any copy: here place 4, not the first 23. Apart from the cases, 95 is outside the row and costs none.
numbersaverage lookslog⁡2  log⁡2 n\log_2\, \,\log_2\, nunder it by
101.661.730.07
1002.482.730.26
1,0003.073.320.24
10,0003.493.730.24
100,0003.814.050.24
1,000,0004.094.320.23
Each average is a point of section 2's plot, 20,000 searches on numbers picked at random from 1 to four times nn. They stay a little under log⁡2  log⁡2 n\log_2\, \,\log_2\, n. Perl, Itai and Avni proved in 1978 that the average grows that way on numbers picked at random (Communications of the ACM, volume 21).
numbersfirst misssecond misssquare root of the firstthird misssquare root of the second
1,0007.841.602.800.611.27
10,00024.483.054.950.921.75
How many places each guess lands from the number wanted, averaged over the searches that make that guess, on 20 rows picked at random, each square root worked before rounding. Each miss is under the square root of the one before. Taking square roots, 65,536 comes down to 2 in 4 steps, as section 4's fourth question's table shows, so few guesses are needed.

where the formula comes from

Each count above, worked out in small steps. Every step names the line it uses.

Why does a number in a row in equal steps cost 1 look, however long the row?
  1. the move: watch the first guess

    the line:
    the first guess lands on it\text{the first guess lands on it}
    • Looking for 51: the line meets it at place 6, so guess 1 is place 6, which holds 51: found.

      611522433344255166076985161
    • Looking for 11: the line meets it at place 3.2, so guess 1 is place 3, which holds 8, too small.

      214283114125246297318113.21
    • 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
    Steps of 9: the line meets 51 at place 6 exactly, so the first guess finds it. Picked at random, the numbers miss the line: 11 meets it at place 3.2, which holds 8, one place short.

    why it is allowed: In equal steps, the line from the smallest to the biggest passes through every number, so it meets the number wanted at its own place.

  2. the move: count it

    the line:
    B=1B = 1

    why it is allowed: BB is the looks for a number in a row in equal steps. By line 1, one, for 8 numbers or 8 million.

  3. the move: a ceiling

    the line:
    B≤1B \le 1

    why it is allowed: By line 2. An equals gives a ceiling and a floor at once.

  4. the move: read it as a bound

    the line:
    B∈O(1)B \in O(1)

    why it is allowed: By line 3, a fixed number of looks, whatever nn is.

  5. the move: a floor

    the line:
    B≥1B \ge 1

    why it is allowed: By line 2 again.

  6. the move: read it as a bound

    the line:
    B∈Ω(1)B \in \Omega(1)

    why it is allowed: By line 5.

  7. the move: ceiling and floor together

    the line:
    B∈Θ(1)B \in \Theta(1)

    why it is allowed: Lines 4 and 6 bound BB by the same growth, 1, which is to say none.

try it with a small nn: at n=8n = 8, line 2 gives B=1B = 1, which you can work out by hand

From 2 numbers on, why do nn numbers never take nn looks, and which row takes n−1n - 1?
  1. the move: a look rules out its place

    the line:
    each look rules outits own place, at least\begin{gathered} \text{each look rules out} \\ \text{its own place, at least} \end{gathered}
    • Looking for 9: 7 looks, found at place 7.

      211322433644755866977988
    • a look, numbered in turn
    • found, ringed
    • looked at, not it
    • never reached
    7 looks for 8 numbers. Each guess is too small, rules out its own place, and the search creeps on.

    why it is allowed: A guess that is not the number wanted is too small or too big. The order rules out its place and everything past it on that side.

  2. the move: the biggest is found first

    the line:
    the biggest number leftis found at the first guess\begin{gathered} \text{the biggest number left} \\ \text{is found at the first guess} \end{gathered}
    • Looking for 98: 1 look, found at place 8.

      213243647586979881
    • Looking for 2: 1 look, found at place 1.

      211324364758697988
    • a look, numbered in turn
    • found, ringed
    • never reached
    Either end costs 1 look. The line starts on 2 and ends on 98.

    why it is allowed: The guess line ends on the biggest number of the part, at its last place. Read across at that number, it meets the guess line at the last place exactly. Any smaller number meets it before the last place, and rounding down keeps that guess off it.

  3. the move: a guess too big skips a place

    the line:
    a guess too big rules outthe last place unseen\begin{gathered} \text{a guess too big rules out} \\ \text{the last place unseen} \end{gathered}
    • Looking for 40: 2 looks, not there.

      1127027231754795846907978
    • a look, numbered in turn
    • looked at, not it
    • ruled out without a look
    72 is too big, so places 3 to 8 go, 5 of them unseen. Then 1 is too small, and 40 is outside what remains, 70, which goes unseen too. 2 looks for 8 numbers.

    why it is allowed: By line 2, a guess that is not the number wanted is never at the last place. So a guess too big rules out the last place without a look. A guess too small, past the part's first place, skips the places before it.

  4. the move: never one look at every place

    the line:
    from 2 numbers on, neverone look at every place\begin{gathered} \text{from 2 numbers on, never} \\ \text{one look at every place} \end{gathered}
    • Looking for 2: 2 looks, found at place 2.

      111222993
    • Looking for 50: 1 look, not there.

      11221993
    • a look, numbered in turn
    • found, ringed
    • looked at, not it
    • ruled out without a look
    • never reached
    2 costs 2 looks, at places 1 and 2, and 99 is never looked at. For 50, place 2 holds 2, too small, and 50 is outside what is left, 99: 1 look.
    number wanted in 1, 2, 99looksplaces looked at
    11place 1
    22places 1 and 2
    991place 3
    50, not there1place 2
    No number from 1 to 99 takes 3 looks on these 3 numbers.

    why it is allowed: By lines 1 and 3, one look at every place needs every guess too small, each at the part's first place, until a last look at the last place alone. A part of one number is looked at only when it holds the number wanted; any other number is outside it and costs no look. So the number wanted is the biggest, and by line 2 the search's first guess finds it.

  5. the move: a row that creeps

    the line:
    W=n−1W = n - 1
    • Looking for 7: the line meets it at place 2, so guess 1 is place 2, which holds 2, too small.

      11223344556677438721
    • Looking for 7: the line meets it at place 1.98, so guess 1 is place 1, which holds 1, too small.

      1122334455667744871.981
    • 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 guess. With 43 the line meets 7 at place 2 exactly, so the guess skips place 1 and the search takes 6 looks. With 44 it meets 7 at place 1.98, before place 2, and the search creeps.
    lookpartthe line meets 7 atguess
    1places 1 to 81.98place 1
    2places 2 to 82.71place 2
    3places 3 to 83.49place 3
    4places 4 to 84.30place 4
    5places 5 to 85.15place 5
    6places 6 to 86.05place 6
    7places 7 to 87.00place 7, found
    Each part's crossing on 1 to 7 then 44, worked as in section 1. The first is 6 over 43 of 7 steps, about 0.98, so place 1.98.

    why it is allowed: WW is the looks on 1, 2 and on to n−1n - 1, then one far bigger number, looking for n−1n - 1. Take 8 numbers, looking for 7. 7 is 6 above 1, so the guess line meets it at place 1, then 6 over the far number less 1 of the 7 steps on. That stays before place 2 only when the far number less 1 is more than 6 times 7, which is 42. So it is at least 43, and the far number at least 44. 6 is 2 fewer than the numbers and 7 is 1 fewer, so for 12 numbers it is 10 times 11, plus 2, which is 112. A later part has fewer small numbers before the far one, so its guess stays before its second place too, as the table shows: one place per look. For 8 numbers, 7.

  6. the move: no row takes more

    the line:
    no row of n numberstakes more\begin{gathered} \text{no row of } n \text{ numbers} \\ \text{takes more} \end{gathered}

    why it is allowed: By line 1 no search takes more than nn looks, and by line 4, from 2 numbers on, none takes nn, so this row's n−1n - 1 is the most.

  7. the move: a ceiling

    the line:
    W≤nW \le n

    why it is allowed: By line 5.

  8. the move: read it as a bound

    the line:
    W∈O(n)W \in O(n)

    why it is allowed: By line 7, 1 times nn at most.

  9. the move: a floor

    the line:
    W≥n2 for n≥2W \ge \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: In line 5, taking 1 from nn leaves at least half of it, from 2 numbers on. 2 less 1 is 1, half of 2; 8 less 1 is 7, more than 4.

  10. the move: read it as a bound

    the line:
    W∈Ω(n)W \in \Omega(n)

    why it is allowed: By line 9.

  11. the move: ceiling and floor together

    the line:
    W∈Θ(n)W \in \Theta(n)

    why it is allowed: Lines 8 and 10 bound WW by the same growth, nn.

try it with a small nn: at n=8n = 8, line 5 gives W=7W = 7, which you can work out by hand

Between which floor and ceiling do the looks lie, on average, for numbers picked at random?
  1. the move: a floor, one look at least

    the line:
    A≥1A \ge 1
    number wanted in 2, 7, 9, 15looks
    21
    71
    92
    151
    on average5 over 4, which is 1.25
    One of the 1,820 choices of 4 numbers from 1 to 16, each number looked for. 1,820 is 16 times 15 times 14 times 13, which is 43,680, over 4 times 3 times 2 times 1, which is 24, the orders of 4 numbers. 1,820 times 4 is 7,280 searches.

    why it is allowed: AA is the average looks over every choice of nn different numbers from 1 to four times nn, each place as likely, with no formula derived. A number that is there costs a look at least, so the average is 1 at least.

  2. the move: read it as a bound

    the line:
    A∈Ω(1)A \in \Omega(1)

    why it is allowed: By line 1.

  3. the move: a ceiling from the dearest rows

    the line:
    A≤n−1A \le n - 1

    why it is allowed: By the second question's line 6, no search of 2 or more numbers costs more, so neither does the average. For 4 numbers, about 1.27 against 3.

  4. the move: read it as a bound

    the line:
    A∈O(n)A \in O(n)

    why it is allowed: By line 3. The measured points grow far slower, a little under log⁡2  log⁡2 n\log_2\, \,\log_2\, n from 10 numbers on, as section 3's table shows.

try it on small inputs: over every choice of 4 different numbers from 1 to 16, each place as likely, 7,280 searches, the average AA is about 1.27

What happens to log⁡2  log⁡2 n\log_2\, \,\log_2\, n when the row's length is squared?
  1. the move: square the row's length

    the line:
    S=log⁡2  log⁡2 n2S = \log_2\, \,\log_2\, n^2
    fromto its squarelooks gained on average
    101000.82
    10010,0001.01
    1,0001,000,0001.02
    Section 3's averages. Squaring the row adds about one look, a little less from 10 to 100; this question shows it adds exactly 1 to the log of the log.

    why it is allowed: SS is the log of the log of nn squared. For 16, that is 256, whose log is 8, and the log of 8 is 3.

  2. the move: a square doubles the log

    the line:
    S=log⁡2(2 log⁡2 n)S = \log_2 (2 \,\log_2\, n)
    numberits log
    65,53616
    2568
    164
    42
    21
    Down the table, each number is the square root of the one above, and each log is half the one above. Four steps to 2, so the log of the log of 65,536 is 4.
    where does this come from?
    question 2 of the page log-log plots and slopesWhere does a count of n2n^2 stand?
    1. the move: name the height

      the line:
      H=log⁡10 n2H = \log_{10}\, n^2
      itemsthe countmarks acrossmarks up
      1010012
      10010,00024
      1,0001,000,00036
      Each row down is one mark across and two up.

      why it is allowed: The input stands log⁡10 n\log_{10}\, n across, and the count at its own log, up. Let HH be that height, the log of the whole square.

    2. the move: write the square out

      the line:
      H=log⁡10(n×n)H = \log_{10} (n \times n)

      why it is allowed: Two nns multiplied together.

    3. the move: multiplying adds the logs

      the line:
      H=log⁡10 n+log⁡10 nH = \log_{10}\, n + \log_{10}\, n

      why it is allowed: Line 2 of question 1 of the page log-log plots and slopes, for nn times nn.

    4. the move: add like with like

      the line:
      H=2 log⁡10 nH = 2 \,\log_{10}\, n

      why it is allowed: Two lots of log⁡10 n\log_{10}\, n.

    5. the move: read it

      the line:
      twice as far up as across:a straight line of slope 2\begin{gathered} \text{twice as far up as across:} \\ \text{a straight line of slope 2} \end{gathered}

      why it is allowed: By line 4, twice as far up as across at every size. So the points lie on one straight line, 2 marks up for each mark across.

    the whole page: log-log plots and slopes

    why it is allowed: The box under this line shows, for the log of 10, that the log of nn squared is twice the log of nn. The log of 2 works the same way. nn is 2 to the power of its log, so nn squared is that power twice over. So 2 to the power 8 is 2 to the power 4, times 2 to the power 4. For 16, the log of 256 is 8, twice 4.

  3. the move: a double adds one

    the line:
    S=1+log⁡2  log⁡2 nS = 1 + \log_2\, \,\log_2\, n
    where does this come from?
    question 1 of the page growth and the growth ladderDouble the input to 2n2n items. How big is log⁡2(2n)\log_2 (2n)?
    1. the move: start from twice the input

      the line:
      G=log⁡2(2n)G = \log_2 (2n)
      numberlog× 2+ 11644833422211100
      Each halving hops one power down. Four hops take 16 to 1, and its log from 4 to 0.
      halvingsfrom 16from 24
      01624
      1812
      246
      323
      411.5
      50.75
      16 reaches 1 after 4 halvings; 24 passes 1 between the 4th and the 5th.

      why it is allowed: log⁡2 n\log_2\, n counts the halvings that take nn down to 1. Let GG be the halvings that take 2n2n down to 1.

    2. the move: one more halving

      the line:
      G=1+log⁡2 nG = 1 + \log_2\, n

      why it is allowed: Halving 2n2n once gives nn, so 2n2n takes one halving more than nn. 16 takes 4, and 8 takes 3.

    3. the move: read it

      the line:
      twice the inputadds one\begin{gathered} \text{twice the input} \\ \text{adds one} \end{gathered}

      why it is allowed: By line 2, twice the input costs one step more.

    the whole page: growth and the growth ladder

    why it is allowed: The box under this line shows that doubling a number adds 1 to its log, as the log of 4 is 2 and of 8 is 3. Here the number doubled is the log of nn. For 16, 1 plus 2 is 3, line 1's answer.

try it with a small nn: at n=16n = 16, line 1 gives S=3S = 3, which you can work out by hand

At most how many looks does binary search make on a row of nn numbers in order? Section 7 compares it with interpolation search.
  1. the move: what one look keeps

    the line:
    each look keeps at mosthalf, rounded down\begin{gathered} \text{each look keeps at most} \\ \text{half, rounded down} \end{gathered}
    two middleslooking for 604111926384755633 beforedropped4 afterkeptthe look, in neither sidethe other middle
    The first look, at 26. 60 must be after it, so binary search keeps those 4 and drops the other 3.

    why it is allowed: Binary search looks at the middle, the first of two middles, and keeps the side the number must be on, the middle in neither side. Of these 8, the side after the middle holds 4, the side before 3.

  2. the move: count the halvings

    the line:
    H=halvings to one number+1 look at it\begin{gathered} H = \text{halvings to one number} \\ + 1 \text{ look at it} \end{gathered}
    rounding down12631.5 → 13 halvings
    12 halved, rounding down, goes 6, 3 and 1. Three halvings, then one more look, so 4.

    why it is allowed: HH is binary search's most looks. By line 1 no look keeps more than half, and a smaller part never needs more looks, as the box under line 3 shows. A number above them all keeps the bigger half each time, so it takes the halvings to one number, and one more look.

  3. the move: use the page on halving

    the line:
    H=⌊log⁡2 n⌋+1H = \lfloor \log_2\, n \rfloor + 1
    where does this come from?
    question 3 of the page halving and logarithmsBinary search on a row of nn numbers. At most how many looks?
    1. the move: what one look keeps

      the line:
      at most half, rounded down,of what is left\begin{gathered} \text{at most half, rounded down,} \\ \text{of what is left} \end{gathered}
      two middleslooking for 7510203040506070803 beforedropped4 afterkeptthe look, in neither sidethe other middle
      8 numbers have two middles; the table's 7 have one.
      lookstill to searchhalf, rounded downthe middlekept
      110, 20, 30, 40, 50, 60, 703.5 to 34050, 60, 70
      250, 60, 701.5 to 16070
      3700.5 to 070none
      75 is above all 7 numbers, so each look keeps the side after.

      why it is allowed: This site's binary search looks at the middle of what is left, the first of two middles, and keeps the side the wanted number must be on. From 8 it keeps 3 before or 4 after: the side after holds half, rounded down, the side before no more.

    2. the move: fewer numbers, no more looks

      the line:
      keeping a smaller part nevercosts more looks\begin{gathered} \text{keeping a smaller part never} \\ \text{costs more looks} \end{gathered}
      after lookkeeping the bigger sidekeeping the smaller side
      01010
      154
      221
      31none
      4none
      Two searches on 10 numbers. Keeping the bigger side runs out after 4 looks, the smaller after 3.

      why it is allowed: Let A always keep the bigger side, and S be any search on the same row. While S holds no more than A, it keeps at most half of its part, rounded down, so no more than A keeps. In the table, S keeps 4 to A's 5, then 1 to 2. So S runs out no later.

    3. the move: a number that keeps the bigger side

      the line:
      a number above them allkeeps the bigger side\begin{gathered} \text{a number above them all} \\ \text{keeps the bigger side} \end{gathered}

      why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.

    4. the move: count that search's looks

      the line:
      ⌊log⁡2 n⌋ looks leaveone number, one morelook leaves none\begin{gathered} \lfloor \log_2\, n \rfloor \text{ looks leave} \\ \text{one number, one more} \\ \text{look leaves none} \end{gathered}

      why it is allowed: Each look halves, rounding down, so question 2 of the page halving and logarithms counts the looks to one number. 1 number takes no halving, log 0. For 7, 7 leaves 3, 3 leaves 1, and look 3 leaves none.

    5. the move: the most looks

      the line:
      B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1

      why it is allowed: Let BB be the most looks, which lines 2 to 4 count. For 7 numbers, the search for 75 takes 3, as the table at line 1 shows.

    6. the move: read it

      the line:
      one more look thanthe log rounded down\begin{gathered} \text{one more look than} \\ \text{the log rounded down} \end{gathered}

      why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.

    the whole page: halving and logarithms

    why it is allowed: The box under this line works out the most looks this way. For 12, 3 and 1 more is 4, its count in section 7's table.

try it with a small nn: at n=12n = 12, line 2 gives H=4H = 4, which you can work out by hand

count it yourself

Count every look for 9 in 3, 4, 6, 7, 9, 95.

3, 4, 6, 7, 9, 95; 9, every step written down
stepwhat happensthe line meets 9 atguesses
1There are 6 numbers in a row, in order, smallest first. Look for 9 by guessing where it should stand.0
29 lies between 3 and 95, the smallest and biggest left, so in proportion it should stand near place 1. Place 1 holds 3.1.31
33 is smaller than 9, so 9 can only be to its right. Rule out 3.1
49 lies between 4 and 95, the smallest and biggest left, so in proportion it should stand near place 2. Place 2 holds 4.2.22
54 is smaller than 9, so 9 can only be to its right. Rule out 4.2
69 lies between 6 and 95, the smallest and biggest left, so in proportion it should stand near place 3. Place 3 holds 6.3.13
76 is smaller than 9, so 9 can only be to its right. Rule out 6.3
89 lies between 7 and 95, the smallest and biggest left, so in proportion it should stand near place 4. Place 4 holds 7.4.04
97 is smaller than 9, so 9 can only be to its right. Rule out 7.4
109 lies between 9 and 95, the smallest and biggest left, so in proportion it should stand near place 5. Place 5 holds 9.5.05
119 is the number wanted, at place 5, so it is found.5
12Done after 5 guesses.5

In all, 5 guesses. The formula from section 4, n−1n - 1, gives W=5W = 5 at n=6n = 6: the same.

where you meet it

Few programming languages hand you interpolation search ready made, but its first guess turns up where numbers come close to equal steps.

  • Git, looking a name up in a list in memory

    Git, a tool that keeps every saved version of a set of files, names each by a long code worked out from its contents, spread about evenly. To find a name in a list in order, held in the computer's working memory, it makes its first guess in proportion, then halves. In 2017 a change by Jeff King took out a trial that guessed in proportion at every step. It saved looks, but with the data already in fast memory its sums cost more time than the looks saved.

    source: git's source (opens in a new tab), hash-lookup.c and its function oid_pos; git commit f1068efefe by Jeff King, 2017

  • Rows in order in a computer's memory, in database research

    Researchers tuned interpolation search for long rows of numbers in order, held in a computer's working memory. On numbers close to equal steps it ran up to 4 times faster than binary search. For uneven numbers they built a second kind of guess, and a way to pick whichever search is fastest for the numbers.

    source: Van Sandt, Chronis and Patel, Efficiently Searching In-Memory Sorted Arrays, Revenge of the Interpolation Search? (opens in a new tab), SIGMOD 2019

  • A paper dictionary

    The table below reads a dictionary of 1,000 pages as a row of letters, from A on page 1 to Z on page 1,000. A real dictionary is not in equal steps, so the first guess misses. Each page opened then becomes a new end of the part, and the next guess is worked from it, as in section 1.

    source: NIST, Dictionary of Algorithms and Data Structures, entry interpolation search (opens in a new tab)

a word starting withits letter, of 26open near page, of 1,000
D4120
M13480
W23880
Worked as in section 1. D is 3 of the 25 steps from A to Z, and 3 over 25 of the 999 pages after page 1 is about 119.9. Page 1, then 119.9 pages on, is page 120.9, rounded down, page 120.

what is used instead

Binary search is what programming languages hand you ready made. Its most, ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1, holds for any numbers in order. Interpolation search's few looks hold only when the numbers come close to equal steps.

how it compares

Interpolation search beside three other searches. Each counts its looks.

wayneeds the row in ordermost on 12 in equal stepsmost on 12 bunchedaverage, numbers in the row, 12 picked at randomreach for it when
linear searchno12126.5the row is short, or in no order and searched once
binary searchyes44about 3.08the row is in order and searched many times
jump search, a block at a timeyes774.5the row is in order but can only be read from the front, as on tape
interpolation searchyes110about 1.72the row is long, in order, and close to equal steps
Each count is that search's own run, every number from 1 to 99 looked for. Equal steps is 8, 16, 24 and on to 96; bunched is 1 to 11 then 99. Bunched costs 10 looks, not 11: 99 is under the 112 that 12 numbers need, 10 times 11, plus 2. The averages look for every number of 200 rows of 12, picked at random from 1 to 48.
102104106101102103104105106107101102103104105106107101102103104105106107
  • linear search at most, nn
  • interpolation search at most, n−1n - 1
  • binary search at most, ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1
  • interpolation search at best, 1
Bunched, interpolation search's most climbs as fast as linear search's. Its average on numbers picked at random, measured in section 2, is about 3.81 looks at 100,000, against binary search's 17 at most.

race them on one row of your own