AlgoLab

O(log log n)·O(1) space
10101112121213141414151617181818202020925

Search runs on a sorted copy of your array, because binary and jump search need sorted input. The bars are that sorted copy.

Searching a sorted array of 20 items for 15 by estimating where it should be.

Searching a sorted array of 20 items for 15 by estimating where it should be.
  1. 1set the low and high boundaries to the first and last position of the array
  2. 2if the target is outside the values at the low and high boundaries, it cannot be in the array
  3. 3estimate the target's position, proportional to where its value falls between the low and high values
  4. 4compare the value at the estimated position with the target
  5. 5the target is found
  6. 6the target is larger, so search above the estimated position
  7. 7the target is smaller, so search below the estimated position
  8. 8the target is not in the array
Comparisons
0
Writes
0
Accesses
0

Complexity

BestO(1)
AverageO(log log n)
WorstO(n)
Extra spaceO(1)
n at n=20 predicts 20; this run took 0.

Interpolation search

Interpolation search estimates where the target is likely to be, proportional to where its value falls between the values at the low and high boundaries, and checks that position directly instead of always checking the midpoint. On values that are roughly uniformly spread out, this estimate lands close to the target almost immediately.

When it is the right choice

Interpolation search is worth using in place of binary search when the data is numeric and roughly uniformly distributed, where its average case of O(log log n) comparisons is a real improvement over binary search's O(log n). It is a poor choice when the data is clustered — bunched tightly in places with a few outliers far away — where its estimate keeps landing short of a distant target and it degrades toward a slow, near-linear scan.

How it works

Worth noticing

Cost

Best
O(1)
Average
O(log log n)
Worst
O(n)
Extra space
O(1)

Worth comparing

All algorithms