AlgoPlusAlgoPlus
Learn/Data Structures & Algorithms
Lesson

Interpolation Search

Probe by value, not by the middle — guess where the target should be assuming values are evenly spread. O(log log n) on uniform data.

8 min read Watch it move Build it

Interpolation search improves on binary search with one insight: if a sorted list's values are *evenly spread*, you can guess where the target should be instead of always splitting the middle. Hunting for 'Smith' in a phone book, you don't open to the center — you open near the back. Hunting for 95 in a 1…100 list, you probe near the far end. That informed guess, not a blind halving, is the whole idea.

The probe formula

Binary search always picks the midpoint. Interpolation search instead computes how far the target's value sits between the low and high values, and probes that fraction of the way through the index window:

pos = lo + ((target - arr[lo]) * (hi - lo))
               / (arr[hi] - arr[lo])

// target near arr[lo]  -> pos near lo   (probe left)
// target near arr[hi]  -> pos near hi   (probe right)
// target halfway value -> pos near mid  (like binary search)
  1. 1Track a window with lo and hi, as in binary search.
  2. 2Compute the probe position pos from the formula above — proportional to the target's *value*, not the window's center.
  3. 3If arr[pos] equals the target → found.
  4. 4If arr[pos] is less → the answer is to the right, so lo = pos + 1.
  5. 5If arr[pos] is greater → the answer is to the left, so hi = pos - 1.
  6. 6Repeat until found or the window closes.
Why O(log log n) on uniform data
On evenly distributed values each probe lands very close to the target, so the remaining window doesn't just halve — it shrinks to roughly its *square root* each step. Squaring-down converges in about log log n probes: a list of a billion uniform values is found in around five.
Skewed data wrecks it — down to O(n)
The formula *assumes* values are evenly spaced. On lopsided data — clusters and big gaps — the guesses land far off, the window barely shrinks, and performance degrades all the way to O(n), worse than binary search's reliable O(log n). When in doubt about the distribution, prefer binary search.
OperationTimeSpace
Average (uniform data) · guesses land near the targetO(log log n)O(1)
Worst (skewed data) · probes go wrong, window barely shrinksO(n)O(1)
Check yourself
Interpolation search beats binary search only when the data is...