Skip to content

Interpolation search

finding

Level 2 of 3

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Interpolation search looks for 63 in 12, 20, 31, 44, 58, 63, 79, 85, 97. At which place does its first guess land?

122031445863798597
the answer, and why

the answer is C

The line from 12 to 97 meets 63 at place 5.8. Rounded down, that is place 5, which holds 58. The second guess then finds 63.

  • Looking for 63: the line meets it at place 5.8, so guess 1 is place 5, which holds 58, too small.

    121202313444585636797858979635.81
  • 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 line meets 63 at place 5.8, so the first guess is place 5.

if you chose another

  • 63 stands at place 6, but the guess is the crossing rounded down, 5.
  • That needs a far bigger last number, making the line climb steeply.
  • Place 9 holds 97, the biggest. Only 97 is guessed there first.

question 2

How many looks does interpolation search take to find 8 in 2, 5, 7, 8, 10, 96?

25781096
the answer, and why

the answer is A

96 makes the line climb steeply, so each guess is the part's first place, 1, 2, 3, then 4, which holds 8.

  • Looking for 8: 4 looks, found at place 4.

    211522733844105966
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • never reached
Four guesses, one place each, creeping up to 8.

if you chose another

  • One look holds on a row in equal steps. Here 96 makes the line climb steeply.
  • 5 is the most for 6 numbers, n−1n - 1. It is 10, the number before 96, that costs 5.
  • That is binary search's count, its middles 7, 10, then 8.

question 3

Which search costs interpolation search the fewest looks? Each option is a row, then, after the semicolon, the number wanted.

the answer, and why

the answer is D

70 is past 66, the biggest, so the search stops before any guess. No look at all.

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

    611823034241545666
  • Looking for 9: 4 looks, found at place 4.

    311522833944115976
  • Looking for 70: 0 looks, not there.

    61182303424545666
  • Looking for 40: 1 look, not there.

    611823031424545666
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • ruled out without a look
  • never reached
The four options in order, a to d. Only c makes no guess.

if you chose another

  • In equal steps, 42 costs 1 look, cheap but not the cheapest.
  • 97 makes the line climb steeply, so the guesses creep, 4 looks.
  • 40 is not there, but lies inside the row. One guess, 30, and then it is outside what remains, 1 look.

question 4

Which is true of interpolation search's looks, for a number in the row?

the answer, and why

the answer is D

A number in a row in equal steps costs 1 look, Θ(1)\Theta(1). A bunched row costs n−1n - 1, Θ(n)\Theta(n).

this tests best, worst and average, on the learn page

if you chose another

  • That is near its average on numbers picked at random, which log⁡2  log⁡2 n\log_2\, \,\log_2\, n sits a little over. Its worst, bunched, is n−1n - 1.
  • A number outside the smallest and biggest costs no look at all.
  • On 1 to 11 then 99 it makes up to 10 looks, binary search at most 4.

pen and paper

question 5about 8 minutes

Interpolation search looks for 7 in the row 1, 2, 4, 5, 7, 99.

  1. Find each guess, place by place.
  2. Say why each guess lands on the first place of the part.
  3. Say why 6 numbers never take 6 looks.
a hint

Work each crossing as in section 1, from the smallest remaining number to 99.

a worked answer

The guesses fall on places 1, 2, 3, 4 and 5: 5 looks, n−1n - 1 for 6 numbers. 99 makes the line climb steeply, so it meets 7 at places 1.3, 2.2, 3.1 and 4.0, each before the part's second place, and rounds down to its first. 6 looks on 6 places would need each guess too small, at its part's first place, by the worst ladder's lines 1 and 3. A sixth look would be on place 6 alone, which is looked at only for the number it holds, 99. A search for 99 finds it at the first guess. So 6 looks never come.

  • Looking for 7: 5 looks, found at place 5.

    111222433544755996
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • never reached
Five guesses, creeping one place each, for 6 numbers.
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

this tests where the formula comes from, on the learn page

what a full answer includes
  • Five guesses, at places 1 to 5.
  • Each crossing before the second place of the part, so rounded down to its first.
  • Place 6 alone is looked at only for 99, which the first guess finds, so never 6 looks.

question 6about 6 minutes

A bank keeps its 100,000 account numbers in order. They were picked at random from 1 to 400,000.

  1. About how many looks does interpolation search average, by section 3's table?
  2. At most how many does binary search make?
  3. Another bank's numbers are 1, 2, 3 and on to 99,999, then one far bigger. At most how many looks might interpolation search take there?
a hint

Binary search's most is ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1; interpolation search's most is n−1n - 1.

a worked answer

About 3.81 looks, the measured point at 100,000. 2 to the power 16 is 65,536 and to the power 17 is 131,072. So the log of 100,000 rounds down to 16, and 1 more is 17, binary search's most. Bunched, with the far number at least 99,998 times 99,999, plus 2, about 10 billion, interpolation search may take n−1n - 1, which is 99,999 looks.

whathow many
2 to the power 1665,536
2 to the power 17131,072
interpolation search, picked at randomabout 3.81 looks on average
binary search, any numbers in order17 looks at most
interpolation search, bunched99,999 looks at most
100,000 account numbers, three ways.

this tests how it compares, on the learn page

what a full answer includes
  • About 3.81, from section 3's table.
  • 17, from 16 and 1 more.
  • 99,999, which is n−1n - 1.