-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathFibonacci_Search.py
79 lines (58 loc) · 1.91 KB
/
Fibonacci_Search.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#Made by SHASHANK CHAKRAWARTY
# Python3 program for Fibonacci search.
'''
Works for sorted arrays
A Divide and Conquer Algorithm.
Has Log n time complexity.
1)Fibonacci Search divides given array in unequal parts
'''
#Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1.
def fibMonaccianSearch(arr, x, n):
# Initialize fibonacci numbers
var2 = 0 # (m-2)'th Fibonacci No.
var1 = 1 # (m-1)'th Fibonacci No.
res = var2 + var1 # m'th Fibonacci
# fibM is going to store the smallest
# Fibonacci Number greater than or equal to n
while (res < n):
var2 = var1
var1 = res
res = var2 + var1
# Marks the eliminated range from front
offset = -1;
# while there are elements to be inspected.
# Note that we compare arr[var2] with x.
# When fibM becomes 1, var2 becomes 0
while (res > 1):
# Check if var2 is a valid location
i = min(offset+var2, n-1)
# If x is greater than the value at
# index var2, cut the subarray array
# from offset to i
if (arr[i] < x):
res = var1
var1 = var2
var2 = res - var1
offset = i
# If x is greater than the value at
# index var2, cut the subarray
# after i+1
elif (arr[i] > x):
res = var2
var1 = var1 - var2
var2 = res - var1
# element found. return index
else :
return i
# comparing the last element with x */
if(var1 and arr[offset+1] == x):
return offset+1;
# element not found. return -1
return -1
# Driver Code, you can change the values accordingly
arr = [10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100]
n = len(arr)
x = 85
print("Found at index:",
fibMonaccianSearch(arr, x, n))