Interpolation search
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.
Looking for 9: the line meets it at place 1.5, so guess 1 is place 1, which holds 2, 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
| step | worked for 57 |
|---|---|
| how far 57 is above 3, the smallest | 54 |
| how far 90, the biggest, is above 3 | 87 |
| 54 over 87, the share of the way | about 0.62 |
| steps from place 1 to place 12 | 11 |
| 0.62 of 11 steps | about 6.8 |
| place 1, then 6.8 steps on | place 7.8 |
| rounded down | place 7, which holds 50 |
Looking for 57: 2 looks, found at place 8.
Looking for 60: 1 look, not there.
Looking for 95: 0 looks, not there.
Looking for 90: 1 look, found at place 12.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
step 1 of 7
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.
step 2 of 7
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
step 3 of 7
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
step 4 of 7
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
step 5 of 7
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
step 6 of 7
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.
step 7 of 7
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.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- the part to search is the whole rowPython line 8
- if the number wanted lies outside the part's smallest and biggest, it is not therePython lines 10, 11, 12 and 13
- guess its place from where it lies between them, and look therePython lines 14, 15, 16, 17, 18 and 19
- if it is the number wanted, it is foundPython line 20
- if it is smaller, keep only the part to its rightPython lines 21 and 22
- 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 : 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
, how many numbers are in the row, ten times more at each mark to the right
- a number in a bunched row: 1 to , then one far bigger number, looking for solid 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 , 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
| guesses | time | |
|---|---|---|
| 1,000 | 999 | about 999 nanoseconds |
| 100,000 | 99,999 | about 100 microseconds |
| 10,000,000 | 9,999,999 | about 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 , 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.
| besta number in the row where the first guess lands, as in equal steps | averagea number in the row, the row picked at random from 1 to four times , each place as likely | worsta number in a bunched row: 1 to , then one far bigger number, looking for | |
|---|---|---|---|
| guesses | 1, the first guess lands on it | measured, a little under from 10 numbers on: about 4.09 at a million | , from 2 numbers on |
| how the guesses grow in each case | |||
| tight, | not derived here, known to grow like | ||
| ceiling, | |||
| floor, | |||
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.
Looking for 9: 7 looks, found at place 7.
Looking for 23: 1 look, found at place 4.
Looking for 95: 0 looks, not there.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
| numbers | average looks | under it by | |
|---|---|---|---|
| 10 | 1.66 | 1.73 | 0.07 |
| 100 | 2.48 | 2.73 | 0.26 |
| 1,000 | 3.07 | 3.32 | 0.24 |
| 10,000 | 3.49 | 3.73 | 0.24 |
| 100,000 | 3.81 | 4.05 | 0.24 |
| 1,000,000 | 4.09 | 4.32 | 0.23 |
| numbers | first miss | second miss | square root of the first | third miss | square root of the second |
|---|---|---|---|---|---|
| 1,000 | 7.84 | 1.60 | 2.80 | 0.61 | 1.27 |
| 10,000 | 24.48 | 3.05 | 4.95 | 0.92 | 1.75 |
where the formula comes from
Each count above, worked out in small steps. Every step names the line it uses.
the move: watch the first guess
the line:Looking for 51: the line meets it at place 6, so guess 1 is place 6, which holds 51: found.
Looking for 11: the line meets it at place 3.2, so guess 1 is place 3, which holds 8, 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
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.
the move: count it
the line:why it is allowed: is the looks for a number in a row in equal steps. By line 1, one, for 8 numbers or 8 million.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2. An equals gives a ceiling and a floor at once.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3, a fixed number of looks, whatever is.
guess it, thenthe move: a floor
the line:why it is allowed: By line 2 again.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 4 and 6 bound by the same growth, 1, which is to say none.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: a look rules out its place
the line:Looking for 9: 7 looks, found at place 7.
- 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.
the move: the biggest is found first
the line:Looking for 98: 1 look, found at place 8.
Looking for 2: 1 look, found at place 1.
- 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.
guess it, thenthe move: a guess too big skips a place
the line:Looking for 40: 2 looks, not there.
- 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.
guess it, thenthe move: never one look at every place
the line:Looking for 2: 2 looks, found at place 2.
Looking for 50: 1 look, not there.
- 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, 99 looks places looked at 1 1 place 1 2 2 places 1 and 2 99 1 place 3 50, not there 1 place 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.
guess it, thenthe move: a row that creeps
the line:Looking for 7: the line meets it at place 2, so guess 1 is place 2, which holds 2, too small.
Looking for 7: the line meets it at place 1.98, so guess 1 is place 1, which holds 1, 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
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. look part the line meets 7 at guess 1 places 1 to 8 1.98 place 1 2 places 2 to 8 2.71 place 2 3 places 3 to 8 3.49 place 3 4 places 4 to 8 4.30 place 4 5 places 5 to 8 5.15 place 5 6 places 6 to 8 6.05 place 6 7 places 7 to 8 7.00 place 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: is the looks on 1, 2 and on to , then one far bigger number, looking for . 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.
guess it, thenthe move: no row takes more
the line:why it is allowed: By line 1 no search takes more than looks, and by line 4, from 2 numbers on, none takes , so this row's is the most.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 5.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, 1 times at most.
guess it, thenthe move: a floor
the line:why it is allowed: In line 5, taking 1 from 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.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 9.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 8 and 10 bound by the same growth, .
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: a floor, one look at least
the line:number wanted in 2, 7, 9, 15 looks 2 1 7 1 9 2 15 1 on average 5 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: is the average looks over every choice of different numbers from 1 to four times , 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.
the move: read it as a bound
the line:why it is allowed: By line 1.
guess it, thenthe move: a ceiling from the dearest rows
the line: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.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3. The measured points grow far slower, a little under from 10 numbers on, as section 3's table shows.
guess it, then
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 is about 1.27
the move: square the row's length
the line:from to its square looks gained on average 10 100 0.82 100 10,000 1.01 1,000 1,000,000 1.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: is the log of the log of squared. For 16, that is 256, whose log is 8, and the log of 8 is 3.
the move: a square doubles the log
the line:number its log 65,536 16 256 8 16 4 4 2 2 1 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 stand? the move: name the height
the line:items the count marks across marks up 10 100 1 2 100 10,000 2 4 1,000 1,000,000 3 6 Each row down is one mark across and two up. why it is allowed: The input stands across, and the count at its own log, up. Let be that height, the log of the whole square.
the move: write the square out
the line:why it is allowed: Two s multiplied together.
the move: multiplying adds the logs
the line:why it is allowed: Line 2 of question 1 of the page log-log plots and slopes, for times .
the move: add like with like
the line:why it is allowed: Two lots of .
the move: read it
the line: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.
why it is allowed: The box under this line shows, for the log of 10, that the log of squared is twice the log of . The log of 2 works the same way. is 2 to the power of its log, so 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.
guess it, thenthe move: a double adds one
the line:where does this come from?
question 1 of the page growth and the growth ladderDouble the input to items. How big is ? the move: start from twice the input
the line:Each halving hops one power down. Four hops take 16 to 1, and its log from 4 to 0. halvings from 16 from 24 0 16 24 1 8 12 2 4 6 3 2 3 4 1 1.5 5 0.75 16 reaches 1 after 4 halvings; 24 passes 1 between the 4th and the 5th. why it is allowed: counts the halvings that take down to 1. Let be the halvings that take down to 1.
the move: one more halving
the line:why it is allowed: Halving once gives , so takes one halving more than . 16 takes 4, and 8 takes 3.
the move: read it
the line:why it is allowed: By line 2, twice the input costs one step more.
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 . For 16, 1 plus 2 is 3, line 1's answer.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: what one look keeps
the line: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.
the move: count the halvings
the line:12 halved, rounding down, goes 6, 3 and 1. Three halvings, then one more look, so 4. why it is allowed: 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.
guess it, thenthe move: use the page on halving
the line:where does this come from?
question 3 of the page halving and logarithmsBinary search on a row of numbers. At most how many looks? the move: what one look keeps
the line:8 numbers have two middles; the table's 7 have one. look still to search half, rounded down the middle kept 1 10, 20, 30, 40, 50, 60, 70 3.5 to 3 40 50, 60, 70 2 50, 60, 70 1.5 to 1 60 70 3 70 0.5 to 0 70 none 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.
the move: fewer numbers, no more looks
the line:after look keeping the bigger side keeping the smaller side 0 10 10 1 5 4 2 2 1 3 1 none 4 none 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.
the move: a number that keeps the bigger side
the line:why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.
the move: count that search's looks
the line: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.
the move: the most looks
the line:why it is allowed: Let 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.
the move: read it
the line:why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.
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.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
count it yourself
Count every look for 9 in 3, 4, 6, 7, 9, 95.
| step | what happens | the line meets 9 at | guesses |
|---|---|---|---|
| 1 | There are 6 numbers in a row, in order, smallest first. Look for 9 by guessing where it should stand. | 0 | |
| 2 | 9 lies between 3 and 95, the smallest and biggest left, so in proportion it should stand near place 1. Place 1 holds 3. | 1.3 | 1 |
| 3 | 3 is smaller than 9, so 9 can only be to its right. Rule out 3. | 1 | |
| 4 | 9 lies between 4 and 95, the smallest and biggest left, so in proportion it should stand near place 2. Place 2 holds 4. | 2.2 | 2 |
| 5 | 4 is smaller than 9, so 9 can only be to its right. Rule out 4. | 2 | |
| 6 | 9 lies between 6 and 95, the smallest and biggest left, so in proportion it should stand near place 3. Place 3 holds 6. | 3.1 | 3 |
| 7 | 6 is smaller than 9, so 9 can only be to its right. Rule out 6. | 3 | |
| 8 | 9 lies between 7 and 95, the smallest and biggest left, so in proportion it should stand near place 4. Place 4 holds 7. | 4.0 | 4 |
| 9 | 7 is smaller than 9, so 9 can only be to its right. Rule out 7. | 4 | |
| 10 | 9 lies between 9 and 95, the smallest and biggest left, so in proportion it should stand near place 5. Place 5 holds 9. | 5.0 | 5 |
| 11 | 9 is the number wanted, at place 5, so it is found. | 5 | |
| 12 | Done after 5 guesses. | 5 |
In all, 5 guesses. The formula from section 4, , gives at : 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 with | its letter, of 26 | open near page, of 1,000 |
|---|---|---|
| D | 4 | 120 |
| M | 13 | 480 |
| W | 23 | 880 |
what is used instead
Binary search is what programming languages hand you ready made. Its most, , 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.
| way | needs the row in order | most on 12 in equal steps | most on 12 bunched | average, numbers in the row, 12 picked at random | reach for it when |
|---|---|---|---|---|---|
| linear search | no | 12 | 12 | 6.5 | the row is short, or in no order and searched once |
| binary search | yes | 4 | 4 | about 3.08 | the row is in order and searched many times |
| jump search, a block at a time | yes | 7 | 7 | 4.5 | the row is in order but can only be read from the front, as on tape |
| interpolation search | yes | 1 | 10 | about 1.72 | the row is long, in order, and close to equal steps |
- linear search at most,
- interpolation search at most,
- binary search at most,
- interpolation search at best, 1