Searching

Fibonacci Search

Target:
Size:14
Visualizer is loading, please wait...
Execution Narrative
Step 1 of 11Start
Line 1
Starting Fibonacci Search for target 55. Smallest Fibonacci number >= 14 is Fib=21 (fibMMm1=13, fibMMm2=8).
Comparisons0
Operations6

Overview & Concept

Fibonacci Search is a comparison-based search algorithm for sorted arrays that divides the search interval using Fibonacci numbers. Unlike Binary Search which uses division by 2, Fibonacci Search uses only addition and subtraction to calculate split points, making it advantageous on low-power hardware lacking fast hardware division units.

How It Works

Fibonacci Split: Uses the identity Fib(k) = Fib(k - 1) + Fib(k - 2) to divide the search space.

Zero Division: Probe positions are calculated purely through addition and subtraction.

Asymptotic Efficiency: Achieves O(log n) time complexity matching binary search.

Step-by-Step Execution

1. Find Smallest Fibonacci NumberFind Fib(k) such that Fib(k) ≥ n.
2. Calculate Probe IndexSet i = Math.min(offset + Fib(k-2), n - 1).
3. Compare with TargetIf target > A[i], cut to Fib(k-1) and update offset = i. If target < A[i], cut to Fib(k-2). If match, return i.
4. TerminationRepeat until found or Fib(k) ≤ 1.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1)O(log n)O(log n)O(1)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
procedure fibonacciSearch(A, target):
  find smallest fibM >= n; fibMm2 = fib(M-2); fibMm1 = fib(M-1)
  offset = -1
  while fibM > 1:
    i = min(offset + fibMm2, n - 1)
    if A[i] < target: fibM = fibMm1; fibMm1 = fibMm2; offset = i
    else if A[i] > target: fibM = fibMm2; fibMm1 = fibMm1 - fibMm2
    else: return i
  if fibMm1 and A[offset + 1] == target: return offset + 1
  return -1
Algorithmic Invariants
  • Fibonacci Bounding Invariant: Search space length corresponds to a Fibonacci number at each iteration.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Requires only addition and subtraction: zero division or multiplication hardware required.
  • •O(log n) time complexity.

Trade-offs & Limitations

  • •More complex to implement than binary search.

Real-World Applications

Low-Power Embedded Microcontrollers

CPUs without hardware division units running fast sorted lookups.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When deploying on hardware architectures where CPU division operations are costly.

Consider Alternatives When

  • ✕On modern x86/ARM processors where hardware division is instantaneous.

Compare trade-offs and complexity across complementary tools in AlgoViz.