Skip to content

Exponential Search

Locate a search interval by exponential growth, then apply binary search.

Exponential search finds a target in a sorted array by first locating a range where the target may lie, then applying binary search within that range.

It is effective when the position of the target is near the beginning or when the array size is unknown.

Problem

Given a sorted array AA of length nn and a value xx, find an index ii such that

A[i]=x A[i] = x

If no such index exists, return 1-1.

Key Idea

Instead of starting with the entire array, expand the search range exponentially:

  • Check A[1],A[2],A[4],A[8],A[1], A[2], A[4], A[8], \dots
  • Stop when exceeding the target or reaching array bounds

This identifies a range [2k1,2k][2^{k-1}, 2^k] that must contain the target if it exists.

Then apply binary search in that range.

Algorithm

exponential_search(A, x):
    if length(A) == 0:
        return -1

    if A[0] == x:
        return 0

    i = 1
    while i < length(A) and A[i] <= x:
        i = i * 2

    l = i // 2
    r = min(i, length(A) - 1)

    return binary_search(A, x, l, r)

Binary search on subarray:

binary_search(A, x, l, r):
    while l <= r:
        m = l + (r - l) // 2
        if A[m] == x:
            return m
        elif A[m] < x:
            l = m + 1
        else:
            r = m - 1
    return -1

Example

Let

A=[2,4,6,8,10,12,14,16,18] A = [2, 4, 6, 8, 10, 12, 14, 16, 18]

and

x=10 x = 10

Range expansion:

stepiA[i]
114
226
3410

Now the target lies in [2,4][2, 4]. Binary search finds index 44.

Correctness

The exponential phase guarantees that:

  • The target lies between i/2i/2 and ii if it exists
  • The range is bounded and contains the target

Binary search then finds the exact position within that range.

Complexity

Let ii be the position of the target.

phasecost
exponential growthO(logi)O(\log i)
binary searchO(logi)O(\log i)
totalO(logi)O(\log i)

Worst case:

O(logn) O(\log n)

Space:

O(1) O(1)

When to Use

  • When array size is unknown
  • When accessing elements is expensive
  • When target is likely near the beginning
  • In unbounded or streaming contexts

Comparison

methodrequirementtime
linear searchnoneO(n)O(n)
binary searchsorted, known boundsO(logn)O(\log n)
exponential searchsortedO(logi)O(\log i)

Here ii is the position of the target.

Implementation

def exponential_search(a, x):
    n = len(a)
    if n == 0:
        return -1
    if a[0] == x:
        return 0

    i = 1
    while i < n and a[i] <= x:
        i *= 2

    l = i // 2
    r = min(i, n - 1)

    while l <= r:
        m = l + (r - l) // 2
        if a[m] == x:
            return m
        elif a[m] < x:
            l = m + 1
        else:
            r = m - 1

    return -1
func ExponentialSearch(a []int, x int) int {
    n := len(a)
    if n == 0 {
        return -1
    }
    if a[0] == x {
        return 0
    }

    i := 1
    for i < n && a[i] <= x {
        i *= 2
    }

    l := i / 2
    r := i
    if r >= n {
        r = n - 1
    }

    for l <= r {
        m := l + (r-l)/2
        if a[m] == x {
            return m
        } else if a[m] < x {
            l = m + 1
        } else {
            r = m - 1
        }
    }

    return -1
}