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.
Guessing the Position from the Value
- Set lo = 0 and hi = n − 1.
- While lo ≤ hi and a[lo] ≤ target ≤ a[hi], compute pos with the formula.
- If a[pos] equals the target, return pos.
- If a[pos] is smaller, set lo = pos + 1; otherwise set hi = pos − 1.
- 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
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
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.
| Search | Even values, find 69 | Even values, miss 50 | Skewed values, find 15 |
|---|---|---|---|
| Interpolation | 2 | 1 | 15 |
| Binary | 3 | 4 | 4 |
| Linear | 14 | 16 | 15 |
- 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
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.
Related
- Binary searchThe same loop with the probe fixed at the middle.
- Exponential searchAnother sorted search that adapts to where the target is.
- Searching algorithmsLinear, binary, hash and tree search compared.
- Search a 2D MatrixA sorted-search problem to practise on.
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]);
}
}
}Step through it
Running on [3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80] · target 69
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