AlgoLab

O(log n)·O(1) space
511181928314243454851515563647377969899

Search runs on a sorted copy of your array, because binary and jump search need sorted input. The bars are that sorted copy.

Searching a sorted array of 20 items for 51 using only Fibonacci numbers, addition and subtraction.

Searching a sorted array of 20 items for 51 using only Fibonacci numbers, addition and subtraction.
  1. 1find the smallest Fibonacci number that is at least as large as the array length
  2. 2use the two Fibonacci numbers below it to split off a block from the front of the remaining range
  3. 3compare the value at the split point with the target
  4. 4the target is found
  5. 5the target is larger, so drop the front block and shrink to the next-smaller pair of Fibonacci numbers
  6. 6the target is smaller, so keep only the front block and shrink to the pair of Fibonacci numbers below that
  7. 7check the one remaining position directly, by addition, since a single element can be left over
  8. 8the target is not in the array
Comparisons
0
Writes
0
Accesses
0

Complexity

BestO(1)
AverageO(log n)
WorstO(log n)
Extra spaceO(1)
log n at n=20 predicts 4; this run took 0.

Fibonacci search

Fibonacci search finds the smallest Fibonacci number at least as large as the array, then uses the two Fibonacci numbers below it to split off a block from the front of the remaining range, comparing the value at that split point with the target and shrinking to a smaller consecutive pair of Fibonacci numbers depending on the result. It narrows the range the way binary search does, but by adding and subtracting Fibonacci numbers rather than dividing a midpoint in half.

When it is the right choice

Fibonacci search exists for machines where division is markedly more expensive than addition and subtraction — true of some early processors, not of anything in general use today. On modern hardware, integer division by two is as cheap as any other arithmetic operation, so binary search is simply the better choice: the same O(log n) bound, no Fibonacci bookkeeping, and a simpler halving rule. It is kept here for what it demonstrates: an algorithm whose entire reason to exist was a hardware constraint that no longer holds.

How it works

Worth noticing

Cost

Best
O(1)
Average
O(log n)
Worst
O(log n)
Extra space
O(1)

Worth comparing

All algorithms