Fibonacci Search
-
Fibonacci Search is a Divide and Conquer algorithm that splits array into two parts in each iteration. Array is divided using Fibonacci Sequence, unlike using middle nodes in case of Binary Search. For each iteration, 2 consecutive numbers in a Fibonacci Sequence is used in decreasing order.
Fibonacci Search has better performance than Linear Search. But compared to Binary Search, this requires about 4% more comparisons on average. The advantage of Fibonacci Search is that it only requires addition and subtraction operations to split the array.
This doesn't make much difference in today's world. But at the time it was introduced in 1953, computing power was very less and division and multiplication operations where much costlier than addition and subtraction.
Time complexity of Fibonacci Search for average and worst cases are represented as O(log n) and Space complexity is O(1).
Practical use cases of Fibonacci Search are limited as Binary Search perform better in most of the scenarios.
-
How Fibonacci Search Works
Program starts by finding the first Fibonacci number (we will denote it as f2) that is greater than or equal to the length of the array. For example, if the array size is 10, f2 = 13.
Then previous 2 Fibonacci numbers are stored in 2 variables.
f1 = 8
f0 = 5Now the array is split into 2 parts of length f0 and f1. In this case, array will be split into two parts of 5 elements each, because even though f1 = 8, there is only 5 elements left after splitting first part with 5 elements (f0 = 5).
Next we can compare the searched item with the final element of part one to identify which block to be discarded.
For next iteration, we need to apply the split on the remaining part of the array using the previous consecutive Fibonacci Sequences.
In above case, for the second iteration;
f2 = 8
f1 = 5
f0 = 3Repeat this process until searched item is found.
-
Code Samples
Below are few sample code implementations of Fibonacci Search Algorithm in Python, Java, C# and JavaScript.