Exponential Search locates a target in a sorted array in two phases: first, it doubles an index bound (1, 2, 4, 8, ...) until it brackets the target; second, it runs Binary Search within that narrowed range. If the target resides at position i, Exponential Search finds it in O(log i) time — making it faster than standard Binary Search whenever the target lies close to the front of a massive array.
if arr[0] == target: return 0
bound = 1
while bound < n and arr[bound] <= target:
bound = bound * 2
low = bound / 2
high = min(bound, n - 1)
while low <= high:
mid = floor((low + high) / 2)
if arr[mid] == target: return mid
else if arr[mid] < target: low = mid + 1
else: high = mid - 1
return -1
Control speed:
high protects against reading past the end of finite arrays when doubling overshoots n.0 and doubles outward, it never needs to know the total length of the array ahead of time.Exponential Search shines on unbounded or infinite sorted sequences (such as streamed sorted logs or lazy generators where n is unknown) and on very large arrays where queries are skewed toward the beginning. In those cases, O(log i) comparisons can be dramatically smaller than O(log n).
In practice, Exponential Search is a core building block of Timsort (the default sorting algorithm in Python and Java for objects), where "galloping mode" skips ahead exponentially when one sorted run repeatedly wins during merging.
Metric / Operation | Complexity | Description |
|---|---|---|
| Best Case | O(1) | When the target is located at index 0. |
| Average Case | O(log i) | Where i is the index of the target: doubling finds the range in O(log i) and Binary Search runs on a slice of size i/2. |
| Worst Case | O(log n) | When the target sits near the end of the array or is absent, requiring O(log n) steps across both phases. |
| Space Complexity | O(1) | Iterative Exponential Search uses constant space for range boundaries. |
Exponential Search discovers an upper bound by doubling the index in O(log i) steps (where i is the target's position) without needing the array size up front, then runs Binary Search within [bound/2, bound].
The bracketing loop advances by doubling bound *= 2; if bound started at 0, doubling would stay at 0 forever. Checking arr[0] first handles index 0 cleanly and lets doubling proceed further.
to join the discussion
Hand-picked resources to deepen your understanding
© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0