forked from Zircoz/Searching-Algos-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacciSearch.py
44 lines (37 loc) · 1.15 KB
/
fibonacciSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''
Consider that the element to be searched is x, in the array arr.
The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of given array n.
Let the found Fibonacci number be k.
We use (k-2)’th Fibonacci number as the index givn that it is a valid index.
Let (k-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we return i.
Else if x is greater, we recur for subarray after i, else we recur for subarray before i.
'''
def fibonaccianSearch(arr, x, n):
k2 = 0
k1 = 1
k = k2 + k1
while (k < n):
k2 = k1
k1 = k
k = k2 + k1
offset = -1;
while (k > 1):
i = min(offset+k2, n-1)
if (arr[i] < x):
k = k1
k1 = k2
k2 = k - k1
offset = i
elif (arr[i] > x):
k = k2
k1 = k1 - k2
k2 = k - k1
else :
return i
if(k1 and arr[offset+1] == x):
return offset+1;
return -1
arr = [1,2,3,4,6,12,34,56,67,87,91,92,95,97]
x = 56
n = len(arr)
print("Found at index:", fibonaccianSearch(arr, x, n))