Algorithms · Visualized
Fibonacci Search
Searching Algorithms
Six search strategies on a sorted array — from the obvious linear scan to log-time binary search and beyond.
Fibonacci search locates a target in a sorted array using Fibonacci numbers to choose probe points instead of a midpoint, narrowing the range with additions and subtractions only — no division at all. Time: O(log n) average, O(log n) worst; space: O(1).
Big-O: Binary O(log n), Linear O(n), Jump O(√n), Interpolation O(log log n) avg, Exponential O(log n).
Fibonacci Search
SearchingFibonacci search locates a target in a sorted array using Fibonacci numbers to choose probe points instead of a midpoint, narrowing the range with additions and subtractions only — no division at all. This made it attractive on hardware where division was expensive, and it remains a neat example of probe-based search.
How Fibonacci Search Works
- Find the smallest Fibonacci number F(k) greater than or equal to the array length n.
- Probe the index offset + F(k−2), where offset tracks the excluded left prefix.
- Compare the target to the value at the probe.
- If it's smaller, discard the portion after the probe and shift down to the F(k−2) sub-range.
- If it's larger, move the offset past the probe and reduce the Fibonacci window by two steps.
- Repeat until the window collapses; check the remaining last element explicitly.
Fibonacci Search Worked Example
Input: {array: [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100], target: 85}
Smallest Fibonacci ≥ 11 is 13, so F=13, F(k−2)=5. Probe at index 4 (value 45); 85 > 45 → shift the offset right and shrink the window. Next probe lands near index 7 (82); 85 > 82 → shift again. Next probe at index 8 (85) → match. Found 85 at index 8.
def fibonacci_search(a, target):
n = len(a)
fm2, fm1, fm = 0, 1, 1
while fm < n:
fm2, fm1, fm = fm1, fm, fm1 + fm
offset = -1
while fm > 1:
i = min(offset + fm2, n - 1)
if a[i] < target:
fm, fm1, fm2 = fm1, fm2, fm1 - fm2
offset = i
elif a[i] > target:
fm, fm1, fm2 = fm2, fm1 - fm2, fm - fm2
else:
return i
if fm1 and offset + 1 < n and a[offset + 1] == target:
return offset + 1
return -1Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Fibonacci Search Time & Space Complexity
When to Use Fibonacci Search
- Searching where division is expensive relative to addition/subtraction.
- Searching data on slow random-access media (historically magnetic tape).
- Teaching probe-based search strategies beyond simple bisection.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.