Exponential Search

Intermediate
Search
Requires sorted array

Exponential Search first finds a range where the target may exist by doubling the index (1, 2, 4, 8…), then runs Binary Search within that range. Especially useful for unbounded or infinite-length sorted arrays.

Auto-sorted: The array is automatically sorted in ascending order. Phase 1 doubles the index to find a range; Phase 2 runs binary search within it.

Search Target

Quick examples:

↑ Set a target value above to begin searching the array

Loading controls...
Loading visualization...
Time: O(log n)
Space: O(1)

Legend

E — Exponential boundary
Current doubling index being checked
M — Mid (binary phase)
Mid-point during binary search phase
Found!
Target value located here
Active search window
Current binary search range
Eliminated
Faded — outside current range
Not Found
Search exhausted
Phases:
  • • Orange phase — exponential doubling
  • • Violet phase — binary search in range
  • • Footer label shows current phase

Inspector

No active frame to inspect

Reference implementation of Exponential Search. Select a language tab to view and copy the code.

def exponential_search(arr: list[int], target: int) -> int:
    n = len(arr)
    if arr[0] == target:
        return 0

    # Phase 1: find the range [bound/2, bound]
    bound = 1
    while bound < n and arr[bound] <= target:
        bound *= 2

    low  = bound // 2
    high = min(bound, n - 1)

    # Phase 2: binary search within [low, high]
    while low <= high:
        mid = low + (high - low) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low  = mid + 1
        else:
            high = mid - 1
    return -1

Python — Code Walkthrough

  1. Phase 1 comment — range discovery

    # Phase 1: find the range [bound/2, bound]

    bound starts at 1 and doubles each step. After the loop, we know target (if present) sits in the half-open interval [bound//2, min(bound, n−1)].

  2. Floor division for low

    low = bound // 2

    Python's // operator gives integer floor division. bound is always a power of 2 after the doubling loop, so bound // 2 equals the previous power of 2 — the left boundary of our range.

  3. min() for safe high

    high = min(bound, n - 1)

    Python built-in min clamps bound to n−1. No import needed. This prevents an IndexError when bound overshoots the list length.

  4. Phase 2 — binary search with floor division

    mid = low + (high - low) // 2

    Python integers don't overflow, but using // keeps mid an integer. The same overflow-safe form as other languages is used as a best-practice habit.