Fibonacci Search
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, the Fibonacci search technique is a method of searching a
sorted array A sorted array is an array data structure in which each element is sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory. It is typically used in computer science to implement static l ...
using a
divide and conquer algorithm In computer science, divide and conquer is an algorithm design paradigm. A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved direc ...
that narrows down possible locations with the aid of
Fibonacci number In mathematics, the Fibonacci numbers, commonly denoted , form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors start the sequence from ...
s. Note that the running time analysis is this article is flawed, as pointed out by Overholt in 1972 (published 1973). Compared to
binary search In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the ...
where the sorted array is divided into two equal-sized parts, one of which is examined further, Fibonacci search divides the array into two parts that have sizes that are consecutive Fibonacci numbers. On average, this leads to about 4% more comparisons to be executed, but it has the advantage that one only needs addition and subtraction to calculate the indices of the accessed array elements, while classical binary search needs bit-shift (see Bitwise operation), division or multiplication, operations that were less common at the time Fibonacci search was first published. Fibonacci search has an average- and worst-case complexity of ''O''(log ''n'') (see Big O notation). The Fibonacci sequence has the property that a number is the sum of its two predecessors. Therefore the sequence can be computed by repeated addition. The ratio of two consecutive numbers approaches the
Golden ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
, 1.618... Binary search works by dividing the seek area in equal parts (1:1). Fibonacci search can divide it into parts approaching 1:1.618 while using the simpler operations. If the elements being searched have non-uniform access memory storage (i. e., the time needed to access a storage location varies depending on the location accessed), the Fibonacci search may have the advantage over binary search in slightly reducing the average time needed to access a storage location. If the machine executing the search has a direct mapped
CPU cache A CPU cache is a hardware cache used by the central processing unit (CPU) of a computer to reduce the average cost (time or energy) to access data from the main memory. A cache is a smaller, faster memory, located closer to a processor core, whic ...
, binary search may lead to more cache misses because the elements that are accessed often tend to gather in only a few cache lines; this is mitigated by splitting the array in parts that do not tend to be powers of two. If the data is stored on a magnetic tape where seek time depends on the current head position, a tradeoff between longer seek time and more comparisons may lead to a search algorithm that is skewed similarly to Fibonacci search. Fibonacci search is derived from
Golden section search The golden-section search is a technique for finding an extremum (minimum or maximum) of a function inside a specified interval. For a strictly unimodal function with an extremum inside the interval, it will find that extremum, while for an inter ...
, an algorithm by Jack Kiefer (1953) to search for the maximum or minimum of a
unimodal function In mathematics, unimodality means possessing a unique mode. More generally, unimodality means there is only a single highest value, somehow defined, of some mathematical object. Unimodal probability distribution In statistics, a unimodal pr ...
in an interval.


Algorithm

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 ''F''''k''+2 = ''F''''k''+1 + ''Fk'', when ''k'' ≥ 0, ''F''1 = 1, and ''F0'' = 1. To test whether an item is in the list of ordered numbers, follow these steps: #Set ''k'' = ''m''. #If ''k'' = 0, stop. There is no match; the item is not in the array. #Compare the item against element in ''F''''k''−1. #If the item matches, stop. #If the item is less than entry ''F''''k''−1, discard the elements from positions ''F''''k''−1 + 1 to ''n''. Set ''k'' = ''k'' − 1 and return to step 2. #If the item is greater than entry ''F''''k''−1, discard the elements from positions 1 to ''F''''k''−1. Renumber the remaining elements from 1 to ''F''''k''−2, set ''k'' = ''k'' − 2, and return to step 2. Alternative implementation (from "Sorting and Searching" by Knuth): Given a table of records ''R1'', ''R2'', ..., ''RN'' whose keys are in increasing order ''K1'' < ''K2'' < ... < ''KN'', the algorithm searches for a given argument ''K''. Assume ''N+1'' = ''F''''k''+1 Step 1. nitialize''i'' ← ''F''''k'', ''p'' ← ''F''''k''-1, ''q'' ← ''F''''k''-2 (throughout the algorithm, ''p'' and ''q'' will be consecutive Fibonacci numbers) Step 2. ompareIf ''K'' < ''Ki'', go to ''Step 3''; if ''K'' > ''Ki'' go to ''Step 4''; and if ''K'' = ''Ki'', the algorithm terminates successfully. Step 3. ecrease ''i''If ''q''=0, the algorithm terminates unsuccessfully. Otherwise set (''i'', ''p'', ''q'') ← (''i - q'', ''q'', ''p - q'') (which moves ''p'' and ''q'' one position back in the Fibonacci sequence); then return to ''Step 2'' Step 4. ncrease ''i''If ''p''=1, the algorithm terminates unsuccessfully. Otherwise set (''i'',''p'',''q'') ← (''i + q'', ''p - q'', ''2q - p'') (which moves ''p'' and ''q'' two positions back in the Fibonacci sequence); and return to ''Step 2'' The two variants of the algorithm presented above always divide the current interval into a larger and a smaller subinterval. The original algorithm, 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.


See also

*
Search algorithms In computer science, a search algorithm is an algorithm designed to solve a search problem. Search algorithms work to retrieve information stored within particular data structure, or calculated in the search space of a problem domain, with eit ...


References

* Implements the above algorithm (not Ferguson's original one). {{DEFAULTSORT:Fibonacci Search Technique Search algorithms