Fibonacci Search
Search in sorted arrays using Fibonacci numbers to choose probe positions.
Visualization
Interactive visualization for Fibonacci Search
Fibonacci Search Visualization
Search algorithm using Fibonacci numbers to choose probe positions
Time Complexity: O(log n)
Space Complexity: O(1)
How it works:
- Find smallest Fibonacci number ≥ array length
- Use Fibonacci numbers to divide array
- Compare target with element at Fibonacci index
- Eliminate portion of array based on comparison
- More efficient than binary search for non-uniform access
Interactive visualization with step-by-step execution
Implementation
1function fibonacciSearch(arr: number[], target: number): number {
2 let n = arr.length;
3 let fibMMm2 = 0, fibMMm1 = 1, fibM = fibMMm2 + fibMMm1;
4 while (fibM < n) { fibMMm2 = fibMMm1; fibMMm1 = fibM; fibM = fibMMm2 + fibMMm1; }
5 let offset = -1;
6 while (fibM > 1) {
7 const i = Math.min(offset + fibMMm2, n - 1);
8 if (arr[i] < target) { fibM = fibMMm1; fibMMm1 = fibMMm2; fibMMm2 = fibM - fibMMm1; offset = i; }
9 else if (arr[i] > target) { fibM = fibMMm2; fibMMm1 = fibMMm1 - fibMMm2; fibMMm2 = fibM - fibMMm1; }
10 else return i;
11 }
12 if (fibMMm1 && offset + 1 < n && arr[offset + 1] === target) return offset + 1;
13 return -1;
14}Complexity
Time
O(1)
O(log n)
O(log n)
Space
O(1)
Applications
Use Cases
Related Algorithms
Binary Search
Binary Search is one of the most efficient searching algorithms with O(log n) time complexity. It works on sorted arrays by repeatedly dividing the search space in half, eliminating half of the remaining elements with each comparison. This divide-and-conquer approach makes it exponentially faster than linear search for large datasets.
Linear Search
Linear Search, also known as Sequential Search, is the simplest searching algorithm that checks each element in a list sequentially until the target element is found or the end is reached. Despite its O(n) time complexity, it's the only option for unsorted data and remains practical for small datasets or when simplicity is crucial.
Jump Search
Jump Search is an efficient algorithm for sorted arrays that combines the benefits of linear and binary search. Instead of checking every element (linear) or dividing the array (binary), it jumps ahead by fixed steps of √n and then performs linear search within the identified block. This approach achieves O(√n) time complexity, making it faster than linear search while being simpler than binary search for certain applications.
Interpolation Search
Interpolation Search is an improved variant of binary search specifically optimized for uniformly distributed sorted arrays. Instead of always checking the middle element, it estimates the target's position based on the target value relative to the range of values, similar to how humans search a phone book. Achieves O(log log n) average time for uniformly distributed data, significantly faster than binary search's O(log n).