Searching

Interpolation Search

Target:
Size:14
Visualizer is loading, please wait...
Execution Narrative
Step 1 of 5Start
Line 1
Starting Interpolation Search for target 55 in range [0..13].
Comparisons0
Operations0

Overview & Concept

Interpolation Search is an improvement over Binary Search for sorted arrays where values are uniformly distributed. Instead of always checking the middle, it estimates the target position using linear interpolation formula: pos = low + ⌊((target - A[low]) / (A[high] - A[low])) × (high - low)⌋, achieving average O(log log n) time.

How It Works

Proportional Estimation: Models real-world phonebook lookups (looking up a name starting with "Z" near the end).

Interpolation Formula: Calculates probe index based on value magnitude relative to range bounds.

O(log log n) Average Time: For uniformly distributed values, search interval shrinks super-exponentially.

Step-by-Step Execution

1. Bounds & Value CheckVerify low ≤ high and A[low] ≤ target ≤ A[high].
2. Compute Probe PositionCalculate pos = low + Math.floor(((target - A[low]) * (high - low)) / (A[high] - A[low])).
3. Evaluate ProbeIf A[pos] == target, return pos. If A[pos] < target, low = pos + 1. If A[pos] > target, high = pos - 1.
4. Repeat or TerminateContinue until found or target falls outside range.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1)O(log log n)O(n)O(1)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
procedure interpolationSearch(A, target):
  low = 0; high = n - 1
  while low <= high and target in [A[low]..A[high]]:
    pos = low + floor(((target - A[low]) / (A[high] - A[low])) * (high - low))
    if A[pos] == target: return pos
    else if A[pos] < target: low = pos + 1
    else: high = pos - 1
  return -1
Algorithmic Invariants
  • Interpolation Invariant: Target value resides within [A[low], A[high]] at each iteration.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Average-case O(log log n) time on uniformly distributed data (faster than binary search).
  • •Minimizes total memory access probes by calculating expected target location directly.

Trade-offs & Limitations

  • •Degenerates to O(n) worst-case time on exponentially distributed or heavily skewed data.
  • •More complex arithmetic operations (division and multiplication) per probe.

Real-World Applications

Uniformly Distributed Telemetry Indexes

Searching time-series timestamps sampled at regular fixed intervals.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When keys are sorted AND known to follow a uniform probability distribution.

Consider Alternatives When

  • ✕When data has severe clustering or non-linear distributions (use Binary Search).

Compare trade-offs and complexity across complementary tools in AlgoViz.