Skip to content
StackDecode
SearchingAnimated

Interpolation Search

Guess the position from the value, like looking up a name in a phone book.

Time

O(log log n) avg, O(n) worst

Extra space

O(1)

Use it for

Sorted, evenly distributed numbers.

How it works

  1. Works best on sorted numbers that are spread evenly.
  2. Instead of the middle, guess where the target should be: pos = low + (x − a[low]) · (high − low) / (a[high] − a[low]).
  3. Compare a[pos] with the target and shrink the window as in binary search.
  4. On evenly spread data it needs about log log n steps; on skewed data it can degrade to n.

Watch it run

Loading animation…