Algorithm · Searching · jump O(√n); exponential O(log i); interpolation O(log log n) expected on uniform keys

Jump, exponential and interpolation search

Three searches on sorted data: jump search in √n blocks, exponential search with a doubling bound, and interpolation search that guesses by value.

The full derivation, edge cases and exercises are in the lesson Jump, exponential and interpolation search. Open it full width in the playground.

On this page
  1. The idea
  2. Steps
  3. Invariants
  4. Complexity
  5. When to use it
  6. Step through it
  7. Implementation
  8. Learn more
  9. References

The idea

All three find the first index of a target in a sorted array and differ in how they narrow the range. Jump search skips ahead in blocks of √n and scans one block. Exponential search doubles a bound until it passes the target, then binary-searches the last interval, which is useful when the size is unknown or the target is near the front.

Interpolation search estimates the position from the values at the ends of the range. On evenly spread values that takes O(log log n) probes on average, but on skewed data it can degrade to O(n).

Steps

  1. Jump: step by m = ⌊√n⌋ while the block’s last element is below the target, then scan inside the block.
  2. Exponential: try bounds 1, 2, 4, … until values[bound] ≥ target, then lower-bound search between the last two bounds.
  3. Interpolation: probe at low + (target − v[low]) · (high − low) / (v[high] − v[low]) and shrink the range around the probe.

Invariants

What holds at every step; the correctness argument rests on these.

  • Each method keeps a range that contains the first occurrence if the target is present.
  • The array must be sorted; interpolation also assumes numeric values that can be interpolated.

Complexity

Jump, exponential and interpolation search complexity
CaseTimeExtra spaceNotes
Jump searchO(√n)O(1)
Exponential searchO(log i)O(1)i = position of the target; O(log n) overall.
Interpolation searchO(log log n) average, O(n) worstO(1)Average case assumes uniformly distributed values.

When to use it

Use it when

  • Exponential: unbounded or very large sorted sequences where the target is likely near the start.
  • Jump: stepping backwards is expensive and a forward scan within a block is cheap.

Avoid it when

  • Plain binary search is available and fits: it is simpler and has no bad cases.
  • Interpolation on skewed or clustered values.

Step through it

The interactive lab for jump, exponential and interpolation search: custom and seeded inputs, stepping forward and backward, with the active line highlighted in the code.

Implementation in four languages

Jump, exponential and interpolation search in four languages, each checked against one shared fixture (empty, one element, first, last, missing, duplicates, a short last block, uniform and skewed keys) including the exact number of element reads per method, against 2,000 random sorted arrays with duplicates compared with a first-occurrence reference (Array.IndexOf, bisect_left, List.indexOf, std::lower_bound), and on int extremes to rule out overflow.

namespace Bekadan.Dsa.Examples.Searching;
 
/// <summary>
/// Jump, exponential and interpolation search over values sorted in ascending order.
/// All three return the index of the FIRST element equal to the target, or -1, and
/// none checks that the input is sorted. Each <c>values[i]</c> access is one read
/// (the tests count them through a wrapping <see cref="IReadOnlyList{T}"/>).
/// </summary>
public static class JumpExponentialSearch
{
    /// <summary>Blocks of floor(sqrt(n)): O(sqrt(n)) reads.</summary>
    public static int JumpSearch(IReadOnlyList<int> values, int target)
    {
        int n = values.Count;
        if (n == 0)
            return -1;
        int step = Math.Max(1, (int)Math.Sqrt(n)); // block size: floor(sqrt(n))
        int low = 0, high = Math.Min(step, n) - 1; // the first block is [low, high]
        while (values[high] < target) // the whole block is smaller: skip it
        {
            low = high + 1;
            if (low >= n)
                return -1; // every element is smaller than the target
            high = Math.Min(high + step, n - 1);
        }
        // values[high] >= target: the first copy, if any, is in [low, high].
        int i = low;
        while (values[i] < target) // stops by high at the latest
            i++;
        return values[i] == target ? i : -1;
    }
 
    /// <summary>Doubling bound, then a lower-bound binary search: O(log i) reads for an answer at index i.</summary>
    public static int ExponentialSearch(IReadOnlyList<int> values, int target)
    {
        int n = values.Count;
        if (n == 0)
            return -1;
        int low = 0, bound = 1;
        while (bound < n && values[bound] < target)
        {
            low = bound + 1; // everything up to bound is smaller
            bound = bound <= n / 2 ? bound * 2 : n; // min(2 * bound, n) without overflowing int
        }
        // The lower bound (first index with value >= target) is in [low, bound].
        int high = bound;
        while (low < high)
        {
            int mid = low + (high - low) / 2;
            if (values[mid] < target)
                low = mid + 1;
            else
                high = mid; // mid may be the answer: keep it
        }
        return low < n && values[low] == target ? low : -1;
    }
 
    /// <summary>Guesses the position by linear interpolation: O(log log n) expected on uniform keys, O(n) worst.</summary>
    public static int InterpolationSearch(IReadOnlyList<int> values, int target)
    {
        int low = 0, high = values.Count - 1;
        while (low <= high)
        {
            int lo = values[low], hi = values[high];
            if (target < lo || target > hi)
                return -1;
            if (lo == hi)
                return low; // every candidate equals the target; also avoids dividing by 0
            // 64-bit arithmetic: (target - lo) * (high - low) overflows int for large values.
            int pos = low + (int)(((long)target - lo) * (high - low) / ((long)hi - lo));
            int value = values[pos];
            if (value < target)
                low = pos + 1;
            else if (value > target)
                high = pos - 1;
            else
            {
                while (pos > low && values[pos - 1] == target)
                    pos--; // an earlier copy: the first one is further left
                return pos;
            }
        }
        return -1;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Searching/JumpExponentialSearch.cs

"""Jump, exponential and interpolation search on a sorted list.
 
All three return the index of the FIRST element equal to the target, or -1.
Each element is read through values[i] once per use; the tests count those
reads. None of them checks that the input is sorted.
 
- jump_search: O(sqrt n) reads with blocks of floor(sqrt n).
- exponential_search: O(log i) reads, where i is the position of the answer.
- interpolation_search: O(log log n) expected reads on uniformly distributed
  keys, O(n) in the worst case.
"""
 
import math
from collections.abc import Sequence
 
 
def jump_search(values: Sequence[int], target: int) -> int:
    n = len(values)
    if n == 0:
        return -1
    step = max(1, math.isqrt(n))  # block size: floor(sqrt(n))
    low, high = 0, min(step, n) - 1  # the first block is [low, high]
    while values[high] < target:  # the whole block is smaller: skip it
        low = high + 1
        if low >= n:
            return -1  # every element is smaller than the target
        high = min(high + step, n - 1)
    # values[high] >= target: the first copy, if any, is in [low, high].
    i = low
    while values[i] < target:  # stops by high at the latest
        i += 1
    return i if values[i] == target else -1
 
 
def exponential_search(values: Sequence[int], target: int) -> int:
    n = len(values)
    if n == 0:
        return -1
    low, bound = 0, 1
    while bound < n and values[bound] < target:
        low = bound + 1  # everything up to bound is smaller
        bound = min(bound * 2, n)
    # The lower bound (first index with value >= target) is in [low, bound].
    high = bound
    while low < high:
        mid = (low + high) // 2
        if values[mid] < target:
            low = mid + 1
        else:
            high = mid  # mid may be the answer: keep it
    return low if low < n and values[low] == target else -1
 
 
def interpolation_search(values: Sequence[int], target: int) -> int:
    low, high = 0, len(values) - 1
    while low <= high:
        lo, hi = values[low], values[high]
        if target < lo or target > hi:
            return -1
        if lo == hi:
            return low  # every candidate equals the target; also avoids dividing by 0
        pos = low + (target - lo) * (high - low) // (hi - lo)
        value = values[pos]
        if value < target:
            low = pos + 1
        elif value > target:
            high = pos - 1
        else:
            while pos > low and values[pos - 1] == target:
                pos -= 1  # an earlier copy: the first one is further left
            return pos
    return -1

Tested on CPython 3.14.0 · examples/python/dsa_examples/jump_exponential_search.py

package dev.bekadan.dsa.searching;
 
import java.util.List;
 
/**
 * Jump, exponential and interpolation search over values sorted in ascending
 * order. All three return the index of the FIRST element equal to the target,
 * or -1, and none checks that the input is sorted. Each {@code values.get(i)}
 * is one read (the tests count them through a wrapping list). Pass a
 * random-access list such as {@code ArrayList} or {@code List.of}.
 */
public final class JumpExponentialSearch {
    private JumpExponentialSearch() {
    }
 
    /** Blocks of floor(sqrt(n)): O(sqrt(n)) reads. */
    public static int jumpSearch(List<Integer> values, int target) {
        int n = values.size();
        if (n == 0) {
            return -1;
        }
        int step = Math.max(1, (int) Math.sqrt(n)); // block size: floor(sqrt(n))
        int low = 0, high = Math.min(step, n) - 1; // the first block is [low, high]
        while (values.get(high) < target) { // the whole block is smaller: skip it
            low = high + 1;
            if (low >= n) {
                return -1; // every element is smaller than the target
            }
            high = Math.min(high + step, n - 1);
        }
        // values[high] >= target: the first copy, if any, is in [low, high].
        int i = low;
        while (values.get(i) < target) { // stops by high at the latest
            i++;
        }
        return values.get(i) == target ? i : -1;
    }
 
    /** Doubling bound, then a lower-bound binary search: O(log i) reads for an answer at index i. */
    public static int exponentialSearch(List<Integer> values, int target) {
        int n = values.size();
        if (n == 0) {
            return -1;
        }
        int low = 0, bound = 1;
        while (bound < n && values.get(bound) < target) {
            low = bound + 1; // everything up to bound is smaller
            bound = bound <= n / 2 ? bound * 2 : n; // min(2 * bound, n) without overflowing int
        }
        // The lower bound (first index with value >= target) is in [low, bound].
        int high = bound;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (values.get(mid) < target) {
                low = mid + 1;
            } else {
                high = mid; // mid may be the answer: keep it
            }
        }
        return low < n && values.get(low) == target ? low : -1;
    }
 
    /** Guesses the position by linear interpolation: O(log log n) expected on uniform keys, O(n) worst. */
    public static int interpolationSearch(List<Integer> values, int target) {
        int low = 0, high = values.size() - 1;
        while (low <= high) {
            int lo = values.get(low), hi = values.get(high);
            if (target < lo || target > hi) {
                return -1;
            }
            if (lo == hi) {
                return low; // every candidate equals the target; also avoids dividing by 0
            }
            // 64-bit arithmetic: (target - lo) * (high - low) overflows int for large values.
            int pos = low + (int) (((long) target - lo) * (high - low) / ((long) hi - lo));
            int value = values.get(pos);
            if (value < target) {
                low = pos + 1;
            } else if (value > target) {
                high = pos - 1;
            } else {
                while (pos > low && values.get(pos - 1) == target) {
                    pos--; // an earlier copy: the first one is further left
                }
                return pos;
            }
        }
        return -1;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/searching/JumpExponentialSearch.java

#pragma once
 
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <span>
 
namespace bekadan::dsa {
 
// Jump, exponential and interpolation search over values sorted in ascending
// order. All three return the index of the FIRST element equal to the target,
// or -1, and none checks that the input is sorted. Each values[i] is one read
// (the tests count them through a wrapper). Values is any random-access
// sequence of int with size() and operator[](std::size_t), e.g. std::span<const int>.
 
// Blocks of floor(sqrt(n)): O(sqrt(n)) reads.
template <typename Values = std::span<const int>>
std::ptrdiff_t jump_search(const Values& values, int target) {
    const std::size_t n = values.size();
    if (n == 0) return -1;
    const std::size_t step = std::max<std::size_t>(1, static_cast<std::size_t>(std::sqrt(static_cast<double>(n))));
    std::size_t low = 0, high = std::min(step, n) - 1;  // the first block is [low, high]
    while (values[high] < target) {  // the whole block is smaller: skip it
        low = high + 1;
        if (low >= n) return -1;  // every element is smaller than the target
        high = std::min(high + step, n - 1);
    }
    // values[high] >= target: the first copy, if any, is in [low, high].
    std::size_t i = low;
    while (values[i] < target) ++i;  // stops by high at the latest
    return values[i] == target ? static_cast<std::ptrdiff_t>(i) : -1;
}
 
// Doubling bound, then a lower-bound binary search: O(log i) reads for an answer at index i.
template <typename Values = std::span<const int>>
std::ptrdiff_t exponential_search(const Values& values, int target) {
    const std::size_t n = values.size();
    if (n == 0) return -1;
    std::size_t low = 0, bound = 1;
    while (bound < n && values[bound] < target) {
        low = bound + 1;                         // everything up to bound is smaller
        bound = bound <= n / 2 ? bound * 2 : n;  // min(2 * bound, n) without overflow
    }
    // The lower bound (first index with value >= target) is in [low, bound].
    std::size_t high = bound;
    while (low < high) {
        const std::size_t mid = low + (high - low) / 2;
        if (values[mid] < target)
            low = mid + 1;
        else
            high = mid;  // mid may be the answer: keep it
    }
    return low < n && values[low] == target ? static_cast<std::ptrdiff_t>(low) : -1;
}
 
// Guesses the position by linear interpolation: O(log log n) expected on uniform keys, O(n) worst.
// Signed indices: high starts at -1 for an empty sequence.
template <typename Values = std::span<const int>>
std::ptrdiff_t interpolation_search(const Values& values, int target) {
    const auto at = [&values](std::ptrdiff_t i) { return values[static_cast<std::size_t>(i)]; };
    std::ptrdiff_t low = 0, high = static_cast<std::ptrdiff_t>(values.size()) - 1;
    while (low <= high) {
        const int lo = at(low), hi = at(high);
        if (target < lo || target > hi) return -1;
        if (lo == hi) return low;  // every candidate equals the target; also avoids dividing by 0
        // 64-bit arithmetic: (target - lo) * (high - low) overflows int for large values.
        std::ptrdiff_t pos = low + static_cast<std::ptrdiff_t>((std::int64_t{target} - lo) * (high - low) / (std::int64_t{hi} - lo));
        const int value = at(pos);
        if (value < target)
            low = pos + 1;
        else if (value > target)
            high = pos - 1;
        else {
            while (pos > low && at(pos - 1) == target) --pos;  // an earlier copy: the first one is further left
            return pos;
        }
    }
    return -1;
}
 
}  // namespace bekadan::dsa

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/jump_exponential_search.hpp

Learn more

Data structures it uses

Problems, patterns and exercises

References