How Fibonacci search splits on Fibonacci sizes

Short answer: Fibonacci search probes a sorted array on boundaries taken from the Fibonacci sequence, so each remaining window has a Fibonacci length and the next window is the previous Fibonacci number. The comparison count is O(log n), the same order as binary search. Index arithmetic uses addition and subtraction of those numbers. The algorithm was attractive when division was the expensive part of computing a midpoint.


What the problem actually asks

Binary search splits a window in half, which takes a division or a shift. Fibonacci numbers split a window into two smaller windows whose lengths are again Fibonacci numbers, so the same split repeats with no division. F(0) = 0, F(1) = 1, and F(k) = F(k−1)+F(k−2). A window of length F(k) probes at an offset of F(k−2), leaving a left window related to F(k−2) and a right window related to F(k−1).

The comparisons are still comparisons. Nothing about the Fibonacci split discards more of the array than the midpoint does. The gain, where there is one, is in the index arithmetic and in access patterns that prefer a particular split.


What the algorithm guarantees

Let F(k) be the smallest Fibonacci number that is at least n. The search maintains a live window and a Fibonacci index. Each comparison reduces that index, so there are O(k) = O(log_φ n) comparisons, and φ is the golden ratio. Extra memory is a few integers. The result is the same lower-bound or equality answer binary search would return on the same sorted array, provided the discarded side is the side the comparison actually rules out.

On a modern processor the division in lo+(hi−lo)//2 is not the cost of a search. Fibonacci search remains correct and remains logarithmic. It is not asymptotically faster than binary search, and the constant in the comparison count is not better.


The working steps

  1. Compute Fibonacci numbers until one is at least n. Remember the two preceding numbers.
  2. While the window is nonempty and the current Fibonacci number is greater than 1, probe at an offset of the earlier Fibonacci number from the window start, clamped to the last index.
  3. If the probed key is strictly smaller than the target, discard the prefix through the probe and step the Fibonacci pair down so the new window length matches the larger remaining part.
  4. If the probed key is strictly greater, discard the suffix from the probe and step the pair down to the smaller part.
  5. On an equal key, return that index, or keep the elimination going with a non-strict test when the caller needs the lower bound rather than any match.

The logarithmic bound is the sorted-array bound, reached by a different split than the midpoint. An unsorted array is still unsafe to split. A numeric uniform array that wants fewer than log n average probes is interpolation search, which uses the key values and gives up the worst case.


Approaches that look simpler and fail

Approach Why it fails when Fibonacci splits are treated as fewer comparisons
Claim a log log n worst case Each probe discards a constant fraction. The depth is Θ(log n), the same order as binary search.
Probe at F(k−2) without clamping to n The Fibonacci number was rounded up past n. An offset past the last index is not an array element.
Update only one side of the Fibonacci pair The next window must itself be a Fibonacci length, or the invariant that pays for the addition-only arithmetic is gone and the loop can skip the answer.
Use it on a linked list to avoid division The probe still needs the item F(k−2) steps from the window start. A list charges that walk in full.

General sorted-array search belongs to the half-open binary search. Division-free index arithmetic is a property of this split, and it is a reason to keep the algorithm only where that arithmetic shows up in the cost.


A working example

The Bash script and the Python script split the search window on Fibonacci sizes. Both print the index 5 of the value 13.

a=(1 3 5 8 13 21 34)
target=21
n=${#a[@]}
fib_m2=0
fib_m1=1
fib_m=$((fib_m2+fib_m1))
while (( fib_m < n )); do
  fib_m2=$fib_m1
  fib_m1=$fib_m
  fib_m=$((fib_m2+fib_m1))
done
offset=-1
while (( fib_m > 1 )); do
  i=$(( offset + fib_m2 ))
  if (( i >= n )); then i=$((n-1)); fi
  if (( a[i] < target )); then
    new_m=$fib_m1
    new_m1=$fib_m2
    new_m2=$((fib_m1 - fib_m2))
    fib_m=$new_m
    fib_m1=$new_m1
    fib_m2=$new_m2
    offset=$i
  elif (( a[i] > target )); then
    new_m=$fib_m2
    new_m1=$((fib_m1 - fib_m2))
    new_m2=$((fib_m - fib_m1))
    fib_m=$new_m
    fib_m1=$new_m1
    fib_m2=$new_m2
  else
    echo "$i"
    exit 0
  fi
done
if (( fib_m1 && offset + 1 < n && a[offset+1] == target )); then
  echo $((offset+1))
else
  echo -1
fi
def fib_search(a, target):
    n = len(a)
    fib_m2, fib_m1 = 0, 1
    fib_m = fib_m2 + fib_m1
    while fib_m < n:
        fib_m2, fib_m1 = fib_m1, fib_m
        fib_m = fib_m2 + fib_m1
    offset = -1
    while fib_m > 1:
        i = min(offset + fib_m2, n - 1)
        if a[i] < target:
            fib_m, fib_m1, fib_m2 = fib_m1, fib_m2, fib_m1 - fib_m2
            offset = i
        elif a[i] > target:
            fib_m, fib_m1, fib_m2 = fib_m2, fib_m1 - fib_m2, fib_m - fib_m1
        else:
            return i
    if fib_m1 and offset + 1 < n and a[offset + 1] == target:
        return offset + 1
    return -1

a = [1, 3, 5, 8, 13, 21, 34]
assert fib_search(a, 21) == 5
for i, v in enumerate(a):
    assert fib_search(a, v) == i
print(fib_search(a, 21))

Where it is used

Fibonacci search splits a sorted array on Fibonacci sizes. It is uncommon beside binary search, which needs only a midpoint. The historical motive was a probe pattern that suited some storage, not a faster comparison count.


When you can drop this algorithm

Drop Fibonacci search on ordinary RAM in favor of binary search, which has a shorter proof and the same asymptotic comparison count. Keep it when the midpoint division is a real cost, or when an existing Fibonacci window is part of a larger structure that already holds those numbers.


Why this stays published

We publish this so a search for Fibonacci search returns the Fibonacci window split and the log n bound it shares with binary search. Fibonacci search eliminates a Fibonacci-sized piece of a sorted array at each probe, which costs O(log n) comparisons and avoids a midpoint division.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *