Lesson 9 · Core algorithms

Interpolation Search

Binary search always probes the middle. Interpolation search probes where the target should be if the values rose in a straight line, so on evenly spread data it needs only a handful of probes.

Interpolation Search concept diagramThe sixteen sorted values plotted as dots, value against index, with a straight line from the first value, 3, to the last, 80. The target 69 is drawn as a horizontal line; it meets the straight line above index 12.86, so the first probe is index 12, value 64. The next guess lands on index 13, value 69: two probes. Binary search would start at index 7 in the middle. A side panel works the formula with these numbers.guess the position from the value071215index04080target 6912.86binary starts here1st: 642nd: 69pos = lo + (t − a[lo]) × (hi − lo) ÷ (a[hi] − a[lo])1st: 0 + 66 × 15 ÷ 77 = 12.86 → index 12 holds 64 < 692nd: lo = 13, a[13] = 69 ✓interpolation: 2 probesbinary search: 3 · linear: 14
1

Guessing the Position from the Value

  1. Set lo = 0 and hi = n − 1.
  2. While lo ≤ hi and a[lo] ≤ target ≤ a[hi], compute pos with the formula.
  3. If a[pos] equals the target, return pos.
  4. If a[pos] is smaller, set lo = pos + 1; otherwise set hi = pos − 1.
  5. If the loop ends, return -1.

Looking up "Smith" in a paper phone book, nobody opens it in the middle. They open it about three quarters of the way in, because S is late in the alphabet. Interpolation search does the same on a sorted array: it estimates where the target should be from how its value compares with the values at both ends of the range.

On 3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80, the target 69 is about 86% of the way from 3 to 80, so the first probe goes to index 12, not index 7. a[12] = 64 is just below, and the next probe lands on 69: two probes, where binary search needs three and linear search fourteen.

  • The range check a[lo] ≤ target ≤ a[hi] keeps pos inside the array
  • It also ends most misses early, often after one probe
2

The Interpolation Search Formula

The interpolation search formula draws a straight line from (lo, a[lo]) to (hi, a[hi]) and reads off the index where the line reaches the target: pos = lo + (target − a[lo]) × (hi − lo) / (a[hi] − a[lo]), rounded down.

The fraction (target − a[lo]) / (a[hi] − a[lo]) says how far the target is through the range of values, from 0 to 1. Multiplying by (hi − lo) turns that into how far through the range of indexes. For 69: (69 − 3) / (80 − 3) = 0.857, and 0.857 × 15 = 12.86, so pos = 12.

Binary search is the same loop with the fraction fixed at one half, so the two differ only in that one line.

  • Multiply before dividing, or integer division rounds the fraction to 0
  • Use 64-bit arithmetic for the product in C++ and Java
  • If a[hi] = a[lo], skip the formula and probe lo
3

Time Complexity: Fast on Average, Slow at Worst

On keys that are uniformly distributed, each probe typically lands within about √(size) of the target, so the range shrinks from n to about √n per probe. A million keys go to about 1,000, then 32, then 6, then 1. That is O(log log n) probes on average, about 4 for a million keys against 20 for binary search.

The bound depends entirely on that assumption. Take the values 1 to 15 followed by 1000: the straight line from 1 to 1000 is nearly flat over the small values, every guess rounds down to lo, and finding 15 takes 15 probes, one cell at a time. Binary search needs 4. The worst case is O(n). Space is O(1) either way.

Probes on the lesson's arrays
SearchEven values, find 69Even values, miss 50Skewed values, find 15
Interpolation2115
Binary344
Linear141615
  • Average O(log log n) only when values are spread evenly
  • Worst case O(n): one outlier is enough to cause it
  • Each probe costs a multiply and a divide, not just a comparison
Key reference

Terms, operations, and practical uses

Key terms

  • ProbeOne read of a[pos] and comparison with the target.
  • Uniform distributionValues spread evenly over their range, so a straight line fits them well.
  • Interpolation-sequential searchA variant that makes one guess and then scans from it.
Implementation

Code in Python, C++ and Java

def interpolation_search(a, target):
    lo, hi = 0, len(a) - 1
    probes = 0
    while lo <= hi and a[lo] <= target <= a[hi]:
        if a[hi] == a[lo]:
            pos = lo
        else:
            pos = lo + (target - a[lo]) * (hi - lo) // (a[hi] - a[lo])
        probes += 1
        if a[pos] == target:
            return pos, probes
        if a[pos] < target:
            lo = pos + 1
        else:
            hi = pos - 1
    return -1, probes


nums = [3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80]
skewed = list(range(1, 16)) + [1000]
same = [7, 7, 7, 7]

for name, a, target in (("even", nums, 69), ("even", nums, 50),
                        ("skewed", skewed, 15), ("all equal", same, 7)):
    i, probes = interpolation_search(a, target)
    print(f"{name}, find {target}: index {i}, probes: {probes}")
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

pair<int, int> interpolationSearch(const vector<int>& a, int target) {
    int lo = 0, hi = a.size() - 1, probes = 0;
    while (lo <= hi && a[lo] <= target && target <= a[hi]) {
        int pos = lo;
        if (a[hi] != a[lo])
            pos = lo + (int)((long long)(target - a[lo]) * (hi - lo) / (a[hi] - a[lo]));
        probes++;
        if (a[pos] == target) return {pos, probes};
        if (a[pos] < target) lo = pos + 1;
        else hi = pos - 1;
    }
    return {-1, probes};
}

int main() {
    vector<int> nums = {3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80};
    vector<int> skewed;
    for (int v = 1; v <= 15; v++) skewed.push_back(v);
    skewed.push_back(1000);
    vector<int> same = {7, 7, 7, 7};
    struct Case { string name; const vector<int>* a; int target; };
    for (const Case& c : {Case{"even", &nums, 69}, Case{"even", &nums, 50},
                          Case{"skewed", &skewed, 15}, Case{"all equal", &same, 7}}) {
        auto [i, probes] = interpolationSearch(*c.a, c.target);
        cout << c.name << ", find " << c.target << ": index " << i << ", probes: " << probes << "\n";
    }
}
public class Main {
    static int[] interpolationSearch(int[] a, int target) {
        int lo = 0, hi = a.length - 1, probes = 0;
        while (lo <= hi && a[lo] <= target && target <= a[hi]) {
            int pos = lo;
            if (a[hi] != a[lo])
                pos = lo + (int) ((long) (target - a[lo]) * (hi - lo) / (a[hi] - a[lo]));
            probes++;
            if (a[pos] == target) return new int[]{pos, probes};
            if (a[pos] < target) lo = pos + 1;
            else hi = pos - 1;
        }
        return new int[]{-1, probes};
    }

    public static void main(String[] args) {
        int[] nums = {3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80};
        int[] skewed = new int[16];
        for (int v = 1; v <= 15; v++) skewed[v - 1] = v;
        skewed[15] = 1000;
        int[] same = {7, 7, 7, 7};
        String[] names = {"even", "even", "skewed", "all equal"};
        int[][] arrays = {nums, nums, skewed, same};
        int[] targets = {69, 50, 15, 7};
        for (int k = 0; k < 4; k++) {
            int[] r = interpolationSearch(arrays[k], targets[k]);
            System.out.println(names[k] + ", find " + targets[k] + ": index " + r[0] + ", probes: " + r[1]);
        }
    }
}
Watch it run

Step through it

Running on [3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80] · target 69

target 69 · lo = 0, hi = 15probes: 0target 69the dashed line joins a[lo] and a[hi]69 is near the top of the range,so the guess should be near the endbinary search would probe index 73071122183214275336387428479531058116412691374148015guess the position from the value, like opening a dictionary near 'S'
Step 1

Values that rise evenly

These 16 values climb by about 5 per index. Interpolation search uses that. Instead of probing the middle like binary search, it asks where 69 would sit if the values rose in a straight line from a[lo] = 3 to a[hi] = 80, and probes there.

Output
1 / 10
4

Interpolation Search vs Binary Search

Binary search guarantees O(log n) whatever the values look like, and each probe costs one comparison. Interpolation search can be much faster, but only on data close to a straight line, and its probes cost more arithmetic. On small arrays in memory the difference is too small to notice, and the risk of an O(n) case usually decides it in favour of binary search.

Interpolation search pays off when probes are expensive and keys are even: looking up numeric IDs, timestamps or hashes in a large sorted file on disk, where every probe is a disk read. A safe hybrid starts with interpolation guesses and falls back to binary halving whenever a guess fails to shrink the range enough, which caps the worst case at O(log n).

  • Binary: guaranteed O(log n), cheap probes, ignores the values
  • Interpolation: O(log log n) on even data, O(n) on skewed data
  • Hybrid: interpolate, but fall back to halving when progress stalls