Homeβ€Ί DSAβ€Ί Exponential Search and Fibonacci Search Algorithms

Exponential Search and Fibonacci Search Algorithms

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Searching β†’ Topic 7 of 8
Learn exponential search and Fibonacci search.
βš™οΈ Intermediate β€” basic DSA knowledge assumed
In this tutorial, you'll learn:
  • Doubling to find range: 1, 2, 4, 8... until arr[i] >= target. Then binary search in [i//2, min(i, n-1)]. O(log i) where i = target index.
  • O(log i) is better than O(log n) when target is near the beginning β€” for the first 10% of elements, exponential search is 3-4x fewer comparisons.
  • Key use case: unbounded/infinite sorted arrays where you cannot start binary search at n//2 because n is unknown.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
Exponential search is like binary search but you don't know the array size. Start at index 1, jump to 2, then 4, 8, 16 β€” doubling each time until you overshoot. Then binary search in the last range. The doubling ensures you find the right range in O(log i) where i is the target's index β€” extremely fast when the target is near the beginning.

Exponential search shines in one specific scenario: you have a sorted sequence but do not know its length β€” or the target is likely near the beginning. Streaming data (real-time sensor readings, log entries), infinite virtual sequences in dynamic programming, or situations where you have prior knowledge that the answer is small.

The doubling-then-binary structure is also the basis for many binary search 'find the boundary' problems: exponential doubling to locate the range, binary search within it. Any time you see 'find the smallest k such that f(k) is true' and you expect k to be small relative to the search space, exponential doubling is the right first move.

  1. Start at index 1
  2. Double: 1, 2, 4, 8, 16... until arr[i] >= target
  3. Binary search in [i/2, min(i, n-1)]
exponential_search.py Β· PYTHON
12345678910111213141516171819
import bisect

def exponential_search(arr: list, target) -> int:
    n = len(arr)
    if n == 0: return -1
    if arr[0] == target: return 0
    # Find range by doubling
    i = 1
    while i < n and arr[i] <= target:
        i *= 2
    # Binary search in [i//2, min(i, n-1)]
    lo, hi = i // 2, min(i, n - 1)
    idx = bisect.bisect_left(arr, target, lo, hi + 1)
    return idx if idx < n and arr[idx] == target else -1

arr = [1,3,5,7,9,11,13,15,17,19,21]
print(exponential_search(arr, 7))   # 3
print(exponential_search(arr, 15))  # 7
print(exponential_search(arr, 6))   # -1
β–Ά Output
3
7
-1

Unbounded / Infinite Arrays

Exponential search shines when the array is 'infinite' (unknown size or streams).

infinite_array_search.py Β· PYTHON
123456789101112131415
def search_infinite(stream, target):
    """Search in a conceptually infinite sorted stream."""
    lo, hi = 0, 1
    # Expand range until stream[hi] >= target
    while stream[hi] < target:
        lo = hi
        hi *= 2
    # Binary search in [lo, hi]
    while lo <= hi:
        mid = (lo + hi) // 2
        val = stream[mid]
        if val == target: return mid
        elif val < target: lo = mid + 1
        else: hi = mid - 1
    return -1

Fibonacci search uses Fibonacci numbers instead of powers of 2 for range jumps. It avoids division (useful on hardware without division instructions) and has the same O(log n) complexity.

fibonacci_search.py Β· PYTHON
123456789101112131415161718192021
def fibonacci_search(arr: list, target) -> int:
    n = len(arr)
    fib2, fib1 = 1, 1
    fib = fib2 + fib1
    while fib < n:
        fib2, fib1 = fib1, fib
        fib = fib2 + fib1
    offset = -1
    while fib > 1:
        i = min(offset + fib2, n - 1)
        if arr[i] < target:
            fib, fib1, fib2 = fib1, fib2, fib - fib1
            offset = i
        elif arr[i] > target:
            fib, fib1, fib2 = fib2, fib1 - fib2, fib - fib1 - fib2
        else:
            return i
    return offset + 1 if fib1 and arr[offset+1] == target else -1

arr = [10,22,35,40,45,50,80,82,85,90,100]
print(fibonacci_search(arr, 85))  # 8
β–Ά Output
8

Complexity

Exponential Search: O(log i) where i is the target's index β€” much faster than O(log n) when target is near the start. Fibonacci Search: O(log n) Both use: O(1) space

🎯 Key Takeaways

  • Doubling to find range: 1, 2, 4, 8... until arr[i] >= target. Then binary search in [i//2, min(i, n-1)]. O(log i) where i = target index.
  • O(log i) is better than O(log n) when target is near the beginning β€” for the first 10% of elements, exponential search is 3-4x fewer comparisons.
  • Key use case: unbounded/infinite sorted arrays where you cannot start binary search at n//2 because n is unknown.
  • Fibonacci search: same concept with Fibonacci numbers instead of powers of 2. Avoids division β€” historically relevant for hardware without division instructions.
  • Interview pattern: 'find the boundary' problems (first true in FFFFTTTT) often benefit from exponential doubling to locate the range before binary search.

Interview Questions on This Topic

  • QWhy is exponential search useful for infinite arrays?
  • QCompare the time complexity of exponential search vs binary search when the target is near index i.
  • QWhat is the advantage of Fibonacci search over binary search on certain hardware?
  • QHow many range-finding steps does exponential search take to find index i?

Frequently Asked Questions

When would you choose exponential search over binary search?

When the array is unbounded or when you expect the target to be near the beginning. For fixed-size arrays with no prior knowledge of target position, binary search is simpler and equally fast.

πŸ”₯
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousInterpolation Search AlgorithmNext β†’Bidirectional Search Algorithm
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged