How exponential search brackets a nearby key

Short answer: Exponential search probes a sorted array at indices 1, 2, 4, 8, and so on, until the key at the probe is at least the target or the array ends. It then binary-searches the bracket that contains the answer. If the target sits at position p, the cost is O(log p) comparisons. The algorithm is binary search with a growing right end, useful on an unbounded sorted domain or when the hit is near the front.


What the problem actually asks

Binary search needs both ends of the range. A sorted stream, an unbounded key space, or a huge array whose match is expected near the beginning does not hand you a tight right end. Doubling finds one. Once a[high] ≥ target and a[high/2] < target, the ordinary half-open search runs on a range of length about p.

The same doubling is the gallop inside a timsort merge: one run has been winning, so the next loser is expected nearby, and a doubling probe finds how far to copy.


What the algorithm guarantees

On a sorted random-access array, the doubling phase stops by index at most 2p, having made O(log p) probes. The following binary search costs O(log p) as well. An absent key, or a key past the end, costs O(log n). Extra memory is a few indices. Correctness is exactly binary search’s correctness on the bracket, plus the fact that a sorted array cannot hide a smaller key to the right of a key that is already ≥ the target.

The base of the exponent can be any constant greater than 1. Base 2 is the usual choice. It does not improve the worst case over binary search on an array whose hit is uniformly random, because the expected position is then Θ(n) and log p is log n.


The working steps

  1. If a[0] equals the target, return 0. If the array is empty or a[0] is already greater than the target, the lower bound is 0.
  2. Set bound = 1. While bound < n and a[bound] < target, double bound.
  3. Binary-search the half-open range [bound//2, min(bound+1, n)). The left end was still strictly below the target, or it is the start of the array.
  4. Return the lower bound from that binary search.
  5. On an unbounded sorted generator, the same doubling runs until a produced key is ≥ the target, then binary search is replaced by the same probe pattern inside the bracket.

The O(log p) bound needs sorted order and random access. An unsorted array, or a linked list, cannot skip to index 2^k. A uniform hit position removes the advantage over starting binary search on [0, n).


Approaches that look simpler and fail

Approach Why it fails when the bracket is not actually a bracket
Double until a[bound] equals the target and stop The first key that is ≥ the target may overshoot a run of equals, and a miss never equals the target. The binary search inside the bracket returns the lower bound.
Binary search from 0 to n when the point was a nearby hit The result is correct and the cost is log n. Doubling was how the cost was supposed to track p.
Use the same probes on unsorted data a[bound] ≥ target does not confine the answer to the bracket unless every earlier key is ≤ every later key.
Gallop one element at a time after the bracket is known That is linear search inside a range of length p. The second phase is binary search.

A known finite sorted array with no reason to expect a small p belongs to ordinary binary search. The doubling probe inside a merge belongs to timsort’s gallop, which is this search on one run.


A working example

The Bash script and the Python script exponential-search for a nearby key, then binary-search the bracket. Both print the index 3.

a=(1 3 5 7 9 11)
target=7
bound=1
n=${#a[@]}
while (( bound < n && a[bound] < target )); do bound=$((bound*2)); done
lo=$((bound/2))
hi=$(( bound + 1 < n ? bound + 1 : n ))
while (( lo < hi )); do
  mid=$(( lo + (hi - lo) / 2 ))
  if (( a[mid] < target )); then lo=$((mid+1))
  else hi=$mid; fi
done
echo "$lo"
a = [1, 3, 5, 7, 9, 11]
target = 7
if a[0] == target:
    print(0)
else:
    bound = 1
    while bound < len(a) and a[bound] < target:
        bound *= 2
    lo, hi = bound // 2, min(bound + 1, len(a))
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if a[mid] < target:
            lo = mid + 1
        else:
            hi = mid
    assert a[lo] == target
    print(lo)

Where it is used

Exponential search finds a bound around a key in a sorted array of unknown or unbounded length, then hands that range to binary search. A caller that already knows the length skips the bracketing. The probes sit at 1, 2, 4, and so on.


When you can drop this algorithm

Drop exponential search when both ends are known and the target is not expected near one end. Binary search is simpler and has the same worst case. Keep it for unbounded sorted domains, for hits concentrated near the front, and as the probe inside a galloping merge.


Why this stays published

We publish this so a search for exponential search returns the doubling bracket and the binary search that finishes it. Exponential search doubles an index until it brackets the target, then binary-searches a range whose length is proportional to the answer’s position.

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

Leave a Reply

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