![]() | This article may be too technical for most readers to understand.(July 2013) |
In computer science, the Fibonacci search technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers. [1] The technique is conceptually similar to a binary search, which repeatedly splits the search interval into two equal halves. Fibonacci search, however, splits the array into two unequal parts, with sizes that are consecutive Fibonacci numbers.
This method has a key advantage on older computer hardware where arithmetic division or bit-shifting operations were computationally expensive compared to addition and subtraction. Since the Fibonacci sequence is based on addition, this search method could be implemented more efficiently. While on modern CPUs the performance difference is often negligible, the algorithm remains of theoretical and historical interest. On average, Fibonacci search performs about 4% more comparisons than binary search, [2] and it has an average- and worst-case complexity of (see Big O notation).
Fibonacci search can also have an advantage in searching data stored in certain structures, such as on a magnetic tape, where seek times are heavily dependent on the distance from the current head position. It is derived from Golden section search, an algorithm by Jack Kiefer (1953) to search for the maximum or minimum of a unimodal function in an interval. [3]
The Fibonacci search technique works by first finding the smallest Fibonacci number that is greater than or equal to the length of the array. Let this Fibonacci number be . The algorithm then uses the preceding Fibonacci numbers, and , to probe the array at an index, narrowing the search space in each step. The size of the step decreases at each iteration according to the Fibonacci sequence.
Let k be defined as an element in F, the array of Fibonacci numbers. n = Fm is the array size. If n is not a Fibonacci number, let Fm be the smallest number in F that is greater than n.
The array of Fibonacci numbers is defined where Fk+2 = Fk+1 + Fk, when k ≥ 0, F1 = 1, and F0 = 1.
To test whether an item is in the list of ordered numbers, follow these steps:
Alternative implementation (from "Sorting and Searching" by Knuth [4] ):
The two variants of the algorithm presented above always divide the current interval into a larger and a smaller subinterval. The original algorithm, [1] however, would divide the new interval into a smaller and a larger subinterval in Step 4. This has the advantage that the new i is closer to the old i and is more suitable for accelerating searching on magnetic tape.
Suppose we want to search for the number x = 85
in the following sorted array of n = 11
elements:
[10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
n = 11
is . So, m = 8
.offset = -1
.i = min(offset + F_6, n-1) = min(-1 + 8, 10) = 7
.x
with the element at index 7: array[7]
is 82.x = 85
is greater than 82
, we eliminate the elements from the start of the array up to index 7. The new search space is the subarray from index 8 onwards.m = m - 2 = 6
(so the new Fibonacci numbers for the step size will be and ).offset = i = 7
.i = min(offset + F_4, n-1) = min(7 + 3, 10) = 10
.x
with the element at index 10: array[10]
is 100.x = 85
is less than 100
, we eliminate the elements from index 10 to the end.m = m - 1 = 5
(new step sizes will be and ).offset = 7
.i = min(offset + F_3, n-1) = min(7 + 2, 10) = 9
.x
with the element at index 9: array[9]
is 90.x = 85
is less than 90
, we eliminate the elements from index 9 to the end.m = m - 1 = 4
(new step sizes will be and ).offset = 7
.i = min(offset + F_2, n-1) = min(7 + 1, 10) = 8
.x
with the element at index 8: array[8]
is 85.