How interpolation search places the next probe

Short answer: Interpolation search probes a sorted numeric array at the index the target would occupy if the keys between the ends were evenly spaced. Under independent uniform keys the expected number of probes is O(log log n). On a skewed distribution the same formula walks the array almost one step at a time and the worst case is Θ(n). The even-spacing guess is the whole algorithm, and it is only as good as the distribution.


What the problem actually asks

Binary search always probes the middle, which is optimal when the only information is the comparison. Numeric keys carry a second piece of information: how far the target sits between the left and right key values. If those keys are spread evenly, the proportional index is near the answer, and repeating the estimate double-exponentially shrinks the range.

The formula divides by the gap between the end keys. A zero gap, a run of duplicates, or an exponential distribution makes the proportion meaningless. The algorithm then needs a rule that still terminates.


What the algorithm guarantees

The probe index is lo + (target − a[lo]) · (hi − lo) / (a[hi] − a[lo]), computed in a type wide enough not to overflow, and clamped to the open interval between lo and hi. If the ends are equal, or the target lies outside them, the search stops or falls back to a binary step. Each probe discards one side, exactly as in binary search, so a sorted array is handled correctly even when the guess is poor.

For independent uniform keys the expected probe count is O(log log n). There are sorted inputs, including geometrically increasing keys, on which the probe advances by O(1) and the cost is Θ(n). Extra memory is a few indices. The algorithm does not replace binary search as a worst-case logarithmic method.


The working steps

  1. Set lo = 0 and hi = n−1 on a sorted numeric array. If n is 0, the key is absent.
  2. While lo ≤ hi and the target lies between a[lo] and a[hi] inclusive, compute the proportional index and clamp it to [lo, hi].
  3. If a[pos] equals the target, return pos. If a[pos] is smaller, set lo = pos+1. Otherwise set hi = pos−1.
  4. If a[lo] equals a[hi], the proportion is undefined. Compare that key directly and stop.
  5. When a worst-case log n bound is required, take the probe as the median whenever the proportional index has not shrunk the range by a fixed fraction.

Expected double-logarithmic time is a statement about uniform numeric keys. Strings, arbitrary comparable objects, and skewed numeric data do not inherit it. A library lower-bound on a sorted array should stay with binary search unless the distribution has been measured.


Approaches that look simpler and fail

Approach Why it fails when keys are not spread evenly
Quote O(log log n) as a worst case Geometric keys and long duplicate runs produce a linear number of probes. The double logarithm is an expectation under uniformity.
Divide by a[hi] − a[lo] when the ends are equal The proportion is undefined. The search has to stop or switch to a comparison step.
Use the formula on a sorted array of strings Letter order does not make the code-point gap a uniform position estimate. Binary search uses the only operation the keys actually support.
Omit the clamp on the computed index Integer division and a target outside the current ends can produce an index off the slice. The probe has to stay inside [lo, hi].

A worst-case logarithmic search belongs to binary search. A uniform spread that is being sorted, rather than searched, is the hypothesis behind bucket sort.


A working example

This Python fragment places the next probe where the target would sit if the values were linear between the ends. On this arithmetic progression the first probe is the answer, index 3.

a = [10, 20, 30, 40, 50]
target = 40
lo, hi = 0, len(a) - 1
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])
    if a[pos] == target:
        print(pos)
        break
    if a[pos] < target:
        lo = pos + 1
    else:
        hi = pos - 1

Where it is used

Interpolation search probes a sorted numeric array where the keys are spread evenly, the way a person looks up a name in a uniformly printed book. Skewed keys make the probe worse than binary search. Standard libraries binary-search unless they know the spread.


When you can drop this algorithm

Drop interpolation search when the key distribution is unknown or known to be skewed, and when a hard log n bound matters. Binary search is then the algorithm. Keep interpolation search for large sorted numeric arrays whose keys really are close to uniform.


Why this stays published

We publish this so a search for interpolation search returns the proportional probe and the uniform hypothesis behind the log log n expectation. Interpolation search probes where a uniform spread would place the key, which is O(log log n) in expectation on uniform data and linear on a skewed sorted array.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *