Program to Implement Fibonacci Search in Python

Python Program to Implement Fibonacci Search

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Introduction of searching algorithms:

Searching for data stored in various data structures is an essential aspect of almost any program.

When searching, there are numerous algorithms to choose from, each with its own implementation and dependence on distinct data formats.

The ability to select a specific algorithm for a given task is a critical talent for developers, and it can make the difference between a speedy, dependable, and stable application and one that crumbles due to a simple request.

Program to Implement Fibonacci Search in Python

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)Fibonacci Numbers

Fibonacci search is a divide and conquer technique that is comparable to both binary search and jump search. It derives its name from the fact that it calculates the block size or search range in each step using Fibonacci numbers.

  • It is applicable to sorted arrays.
  • An Algorithm of Divide and Conquer.
  • Has a Time complexity of Log n.

Fibonacci numbers

The Fibonacci Series is made up of Fibonacci numbers. So first, let us define the Fibonacci Series. The series can be defined recursively as follows:

F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1

We do have a straightforward means of calculating Fibonacci numbers using exponents and the Golden Ratio, but this is how the series is intended to be understood.

F(n) is defined as the “nth Fibonacci Number” in the preceding definition.

Thus, the 0th number is 0, the 1st numbers is 1, the 2nd is the total 1st and 0th numbers of Fibonacci, the 3rd is the sum 2nd and 1st numbers of Fibonacci and so on.

Fibonacci numbers begin with zero and go in the following order: 0, 1, 1, 2, 3, 5, 8, 13, 21,…, where each element is the sum of the two numbers that come before it.

  • It is applicable to sorted arrays.
  • An Algorithm of Divide and Conquer.
  • Has a Time complexity of Log n.

Fibonacci Search splits the array into different portions
Divided range is used by Binary Search. Search is not used by Fibonacci, but employs + and -. Some CPUs could be expensive for the division operator.
Fibonacci Search reviews in succeeding steps relatively closer items. So Fibonacci Search can be helpful if the input array is large that does not fit into the CPU cache or even in ram.

Fibonacci search, like binary search, is a divide and conquer method that requires a sorted list. It also divides the list into two sections, compares the target to the item in the middle of the two sections, and eliminates one side based on the comparison. So, how does it differ from binary search?

The Fibonacci numbers are used to divide the list into two parts in the Fibonacci search, therefore it will divide the list into two portions of different lengths. Furthermore, instead of conducting division, it conducts addition, which is less costly on the CPU. Let’s get into the specifics now.

First, we need to know how long the given list is. Then we look for the smallest Fibonacci number that is bigger than or equal to the length of the list. That is, if the list has 100 items, the least Fibonacci number greater than 100 is 144. Assume this is the nth Fibonacci number. In the preceding example, the 12th Fibonacci number is 144.

Following that, we go back twice in the Fibonacci sequence from that number. Essentially, we are looking for the

(n-2)th Fibonacci number. So, in the preceding example, we discovered the 12th Fibonacci number, which is 144, and now we need the 10th, which is 55.

This is used as the index to divide the list into two sections. That is, we examine at this index in the list, and assuming the list is sorted in increasing order, we eliminate the left side if the item at this index is smaller than the target, else we eliminate the right side. We repeat this process until we locate the item we’re seeking for, which will occur when the calculated index’s item matches the target.

Below is the implementation:

# function which implements the fibonacci search
def fib_search(givenlist, givenelement):
    # ffinding the length of given list
    length = len(givenlist)

    first = -1

    x0 = 0
    x1 = 1
    x2 = 1
    while(x2 < length):
        x0 = x1
        x1 = x2
        x2 = x1 + x0

    while(x2 > 1):
        eleIndex = min(first + x0, length - 1)
        if givenlist[eleIndex] < givenelement:
            x2, x1 = x1, x0
            x0 = x2 - x1
            start = eleIndex
        elif givenlist[eleIndex] > givenelement:
            x2 = x0
            x1 = x1 - x0
            x0 = x2 - x1
            return eleIndex
    if (x1) and (givenlist[length - 1] == givenelement):
        return length - 1
    return None

# given list
given_list = [1, 9, 4, 7, 2, 8, 6]
# sorting the given list
# given element to search
given_element = 4
# passing given list and given element to fibonacci search function to check whether
# the given element is present in list or not
print(fib_search(given_list, given_element))




In this tutorial, we reviewed the Fibonacci numbers, how they are employed in the Fibonacci search algorithm, the functioning of the method, and the implementation of the python code.
Related Programs: