Fibonacci Search Algorithm - Computer Geek (2024)

Table of Contents
Test Yourself BOOKS FAQs

in Hindi

PREV

NEXT

Distribute Education like Computer Geek

Menu

What is Fibonacci Sequence?

.

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.

It is named after the Italian mathematician Leonardo of Pisa, known as Fibonacci, who introduced the sequence to Western mathematics in his book “Liber Abaci” in 1202.

The Fibonacci sequence typically begins with the numbers 0 and 1, and each subsequent number is generated by adding the two previous numbers. The sequence starts as follows:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

.

Here’s how the sequence continues to develop

0 + 1 = 1

1 + 1 = 2

1 + 2 = 3

2 + 3 = 5

3 + 5 = 8

5 + 8 = 13

8 + 13 = 21

13 + 21 = 34

And so on…

.

Definition of Fibonacci Search
  1. Fibonacci Search is an algorithm used to locate a specific element within a sorted array or list with Fibonacci numbers.
  2. It is based on the Fibonacci sequence, a series of numbers where each number is the sum of the two preceding ones (e.g., 0, 1, 1, 2, 3, 5, 8, 13, 21, …) that I said it earlier.
  3. The key idea is to use the Fibonacci numbers to define two pointers left and right, within the array, which are then adjusted based on comparisons with the target element until the element is found or it is determined that the element does not exist in the sorted array.
  4. Fibonacci Search method is yet another elimination method where a portion of the input array is eliminated.
  5. Fibonacci Search is known for its time efficiency, typically having a time complexity of O(log n) for searching in sorted datasets, making it suitable for large arrays or lists. However, it requires the data to be sorted in ascending order for effective operation.
Algorithm for Fibonacci Search
1. Input a sorted array A[i] with n numbers 
and target element which we want to search for.

2. Start by generating a series of Fibonacci
numbers until you find a number that is greater
than or equal to the length of the array.
F(k) >= n

Example – if n = 10
Then, fibonacci sequence we have is
0, 1, 1, 2, 3, 5, 8, 13, because 13 >= 10.

3. If F(k) = 0, then stop and print the message
as element not found.

4. Name a variable “offset” = -1

5. Check index i = min(offset + F(k-2), n-1)

6. Compare

If Target Element = A[i],

return i and stop the search

If Target Element > A[i],

k = k-1, offset = i,

repeat step 5 and 6.

If Target Element < A[i],

k = k-2, repeat step 5 and 6.

.

How does it work?

  1. Suppose you have a sorted array and you want to find the number `15`.
Fibonacci Search Algorithm - Computer Geek (2)

.

  1. Generate the Fibonacci sequence where F(k) >= n = 11.
Fibonacci Search Algorithm - Computer Geek (3)
  1. F(k) ≠ 0, then search continues
  2. Make a variable ‘offset = -1’.

.

Iteration 1

Check i = min (offset + F(k-2), n-1), where k = 7, offset = -1, n = 11

i = min ( -1 + 5, 10)

i = min (4, 10) = 4

Compare the Target Element to Sorted Array

  • Target Element = 15, A[4] = 9
  • Do k = k-1, offset = 4 and again check for i.

.

Iteration 2

Check i = min (offset + F(k-2), n-1) where k = 6, offset = 4, n = 11.

i = min (4 + 3, 10)

i = min (7, 10) = 7

Compare the Target Element to Sorted Array

  • Target Element = 15, A[7] = 15
  • Search is successful and return i = 7, the index of the sorted array in which we found Target Element.

.

Source Code for Fibonacci Search

C

C++

Java

Python

C

C++

Java

Python

Time Complexity of Fibonacci Search
  1. In the best-case, the target element is found at the very beginning of the search process, typically after only a few comparisons. The best-case time complexity is O(1), as the algorithm may find the target element with just a single comparison.
  2. The average-case time complexity of Fibonacci Search is O(log n), where n is the number of elements in the sorted array. On average, the algorithm efficiently reduces the search space by approximately half in each comparison, similar to binary search. This is achieved by using the Fibonacci sequence to determine the positions to check.
  1. The worst-case time complexity of Fibonacci Search is also O(log n). While it may seem surprising, the worst-case occurs when the algorithm repeatedly moves two steps left in the Fibonacci sequence. Even in this scenario, the search space is effectively reduced, leading to a logarithmic time complexity. This worst-case behavior is still significantly more efficient than linear search (O(n)) for large datasets.

.

Space complexity of Fibonacci Search
  1. The space complexity of the Fibonacci Search algorithm is O(1), which means it uses a constant amount of extra space for variables regardless of the size of the input array.
  2. This constant space usage is due to the fact that the algorithm maintains a fixed number of variables to store indices, offsets, and Fibonacci numbers during the search process. The space required by the algorithm does not grow with the size of the input array, making it space-efficient even for large datasets.

.

Advantages of Fibonacci Search
  1. Efficient for searching in sorted arrays.
  2. Logarithmic time complexity, making it fast for large datasets.
  3. Minimal memory usage (O(1)).
  4. Adaptable by adjusting Fibonacci numbers.
  5. In some cases, it is faster than binary search.

.

Disadvantages of Fibonacci Search
  1. Requires the array to be sorted.
  2. Complex to implement compared to linear search and binary search.

.

Applications of Fibonacci Search
  1. Fibonacci Search can be used in search engines and database management systems to quickly locate documents, records, or entries in sorted collections.
  2. Library databases often store books, journals, and other resources in sorted order. Fibonacci Search can help users find specific titles or authors efficiently.
  3. Economic data, such as stock prices or financial records, is frequently sorted. Fibonacci Search can assist in finding historical data points or specific financial information.
  4. Geographic data, such as maps and spatial information, is often sorted by location or coordinates.
  5. Some data compression algorithms utilize Fibonacci sequences to represent compressed data efficiently.
  6. When a data packet needs to be routed to a specific destination, the network router or switch must perform a lookup in the routing table to identify the appropriate route. Fibonacci Search can be employed to efficiently search within the sorted routing table.

PREV

NEXT

Test Yourself

Q1- In Fibonacci Search, why do we generate Fibonacci numbers until F(k) is greater than or equal to the length of the array? What would happen if we used a fixed value of k instead?

We generate Fibonacci numbers until F(k) is greater than or equal to the length of the array to ensure that the search covers a range that includes the entire array.

If we used a fixed value of k, the algorithm might fail to find elements located at the end of the array, as the search range would be limited. This dynamic approach ensures that the entire array is considered during the search.

Q2- Fibonacci Search is known for its time efficiency. Can you explain a scenario where Fibonacci Search might be less efficient than linear search?

Fibonacci Search can be less efficient than linear search when the target element is located near the beginning of the array.

In this case, linear search would find the element with just a few comparisons, whereas Fibonacci Search might require more comparisons due to its logarithmic nature.

Q3- Can you explain why the Fibonacci Search algorithm is considered “adaptive” in its search behaviour?

Fibonacci Search is considered adaptive because it dynamically adjusts its search range based on the Fibonacci sequence. As it compares elements, it adapts its search direction to move closer to the target element.

This adaptability makes it efficient in scenarios where the target’s position is not known in advance and allows it to perform well in various search scenarios.

Q4- Explain the concept of the Fibonacci sequence and how it is related to the Fibonacci Search algorithm.

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones (e.g., 0, 1, 1, 2, 3, 5, …).

In Fibonacci Search, this sequence is used to define indices within a sorted array for efficient searching. The algorithm compares the target element with the element at the Fibonacci indices to narrow down the search.

Q5- What is the time complexity of the Fibonacci Search algorithm in the best, average, and worst cases? Explain.

In the best, average, and worst cases, the time complexity of Fibonacci Search is O(log n). This efficiency arises from the algorithm’s ability to reduce the search space by approximately half in each comparison, similar to binary search.

Q6- Describe a scenario where Fibonacci Search might outperform binary search.

Fibonacci Search can outperform binary search when the target element is closer to the beginning of the sorted array.

This is because Fibonacci Search starts with smaller indices and progresses toward the end, which can lead to faster results in such cases.

Q7- Explain the concept of offset in the Fibonacci Search algorithm and its significance.

In the Fibonacci Search algorithm, the concept of “offset” is a critical element used to optimize the search process. The offset represents an index within the sorted array and serves as a starting point for comparisons during the search. It has significant significance in controlling the movement and efficiency of the algorithm.

Q8- What is the role of the offset variable in the Fibonacci Search algorithm?
  1. It stores the target element.
  2. It tracks the number of iterations.
  3. It determines the starting point for comparisons.
  4. It calculates the Fibonacci sequence.

Ans – (3)

Explanation – The offset variable in Fibonacci Search helps optimize the search process by specifying where to begin comparisons in the array.

Q9- What is the space complexity of the Fibonacci Search algorithm?
  1. O(log n)
  2. O(n)
  3. O(1)
  4. O(n log n)

Ans – (3)

Explanation – The space complexity of Fibonacci Search is O(1), indicating constant space usage.

But in recursive fibonacci search, the space complexity is O(n) because we make recursion tree and the height of recursion tree is n.

F(k)

/ \

F(k-1) F(k-2)

/ \ / \

. . . . . . . . . . . . . . . . . . . .

F(2) F(1) F(2) F(1) F(2) F(1)

Q10- In which real-world scenario might Fibonacci Search be useful?
  1. Analyzing unsorted data in a spreadsheet
  2. Searching for a contact in a phone book
  3. Sorting a large dataset
  4. Generating random numbers

Ans – (2)

Explanation – Fibonacci Search can be efficient for searching in sorted collections like a phone book.

Q11- What is the Fibonacci sequence primarily used for in computer science and mathematics?
  1. Generating prime numbers
  2. Calculating square roots
  3. Sorting algorithms
  4. Modeling growth and sequences in various fields

Ans – (4)

Explanation – The Fibonacci sequence is used in computer science and mathematics to model growth, sequences, and patterns in various natural and artificial systems.

BOOKS

MCQs of Algorithm

GATE Syllabus

GATE Previous Year Paper

Fibonacci Search Algorithm - Computer Geek (2024)

FAQs

What is the Fibonacci series geek for geeks? ›

The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the Fibonacci series.

How do you answer the Fibonacci sequence? ›

The Fibonacci Sequence is a series of numbers that starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. So the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

Which algorithm is best for Fibonacci series? ›

There are principally three algorithms to generate the Fibonacci sequence: the recursive method, the iterative method, and the formula method (Binet's theorem). However, variations and optimisations of these three exist, such as the matrix exponentiation method and the fast doubling method.

What is the simple algorithm for the Fibonacci series? ›

The Fibonacci Algorithm is a numerical series where each number is the sum of the two preceding ones, starting from 0 and 1. It's a simple and significant concept in computer science with base cases F(0) = 0, F(1) = 1, and recursive case F(n) = F(n-1) + F(n-2).

Is Fibonacci worth it? ›

Don't Rely on Fibonacci Alone

Fibonacci can provide reliable trade setups, but not without confirmation. Applying additional technical tools like MACD or stochastic oscillators will support the trade opportunity and increase the likelihood of a good trade.

What is the easiest way to solve the Fibonacci sequence? ›

Fibonacci Sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21, …. “3” is obtained by adding the third and fourth term (1+2) and so on. For example, the next term after 21 can be found by adding 13 and 21. Therefore, the next term in the sequence is 34.

What is the golden rule Fibonacci sequence? ›

The Golden Ratio is a relationship between two numbers that are next to each other in the Fibonacci sequence. When you divide the larger one by the smaller one, the answer is something close to Phi. The further you go along the Fibonacci Sequence, the closer the answers get to Phi.

How to solve Fibonacci problems? ›

Rules for Fibonacci Numbers

Fibonacci sequence numbers follow a rule according to which, Fn = Fn-1 + Fn-2, where n > 1. The third Fibonacci number is given as F2 = F1 + F0. As we know, F0 = 0 and F1 = 1, the value of F2 = 0 + 1 = 1. The sequence of Fibonacci numbers goes like 0, 1, 1, 2, and so on.

Which is the most disadvantage for Fibonacci method? ›

Disadvantages of Using Fibonacci in Trading:
  • Subjectivity: Selecting the starting and ending points for drawing Fibonacci levels can be subjective, leading to variations in results among different traders.
  • No Guarantee of Accuracy: Fibonacci levels do not guarantee precise price reversals or continuations.
Mar 8, 2016

What is the golden ratio in the Fibonacci Algorithm? ›

The golden ratio is derived by dividing each number of the Fibonacci series by its immediate predecessor. In mathematical terms, if F(n) describes the nth Fibonacci number, the quotient F(n)/ F(n-1) will approach the limit 1.618... for increasingly high values of n. This limit is better known as the golden ratio.

What is the best Fibonacci setup? ›

The most popular Fibonacci retracements are 61.8% and 38.2%. Note that 38.2% is often rounded to 38%, and 61.8 is rounded to 62%.

What is the best case for Fibonacci search? ›

Time Complexity of Fibonacci Search

In the best-case, the target element is found at the very beginning of the search process, typically after only a few comparisons. The best-case time complexity is O(1), as the algorithm may find the target element with just a single comparison.

Is Fibonacci search faster than binary? ›

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.

What is the difference between Fibonacci and golden search method? ›

The Fibonacci method differs from the golden ratio method in that the ratio for the reduction of intervals is not constant. Additionally, the number of subintervals (iterations) is predetermined and based on the specified tolerance. Thus the Fibonacci numbers are 1,1,2,3, 5,8,13,21, 34ททท.

What is the Fibonacci series explained? ›

The Fibonacci sequence is a type series where each number is the sum of the two that precede it. It starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.

What is the aim of the Fibonacci series? ›

The Fibonacci sequence is a set of steadily increasing numbers where each number is equal to the sum of the preceding two numbers. Many things in nature have dimensional properties that adhere to the golden ratio of 1.618, a quotient derived from the Fibonacci sequence.

What is the logic of the Fibonacci series? ›

The sequence follows the rule that each number is equal to the sum of the preceding two numbers. The Fibonacci sequence begins with the following 14 integers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...

What is the user defined Fibonacci series? ›

Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In mathematical terms, the Fibonacci sequence is defined by the recurrence relation: F(n) = F(n-1) + F(n-2)

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6203

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.