Fibonacci Search
Fibonacci search is an algorithm used to search for a given element in a sorted array. It works by dividing the search space into subarrays using Fibonacci numbers and comparing the element at the midpoint of each subarray to the target element. If the target element is larger than the midpoint element, it searches the right half of the subarray using the same process. If the target element is smaller, it searches the left half. It continues to divide the search space in this way until it finds the target element or determines that it is not in the array.
Example in TypeScript
The fibonacciSearch()
function starts by initializing three variables, fibM2
, fibM1
, and fibM
, to the first three Fibonacci numbers. It then uses a loop to generate the next Fibonacci number and assign it to fibM
until fibM
is greater than or equal to the length of the array. It then initializes a variable offset
to -1
and enters another loop.
The inner loop uses the value of fibM
to determine the size of the current subarray and the index of the element at its midpoint. It compares the value at this index to the target element and adjusts fibM
, fibM1
, and fibM2
accordingly. If the value at the midpoint index is equal to the target element, it returns the index. If the inner loop finishes without finding the target element, it checks if the value immediately following the current subarray is equal to the target element and, if so, returns the index of that element. If the target element is not found, the function returns -1
.
Usage
This code searches for the value 5
in the sorted array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
. The fibonacciSearch()
function should return 4
, which is the index of 5
in the array. It also searches for the value 11
, which is not in the array, and should return -1
.
Last updated