forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_search.py
55 lines (46 loc) · 1.23 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
def fibonacci_search(arr, x, n):
"""
Returns index of x if present, else returns -1
:param arr:
:param x:
:param n:
:return:
"""
fib2 = 0 # (m-2)'th Fibonacci No.
fib1 = 1 # (m-1)'th Fibonacci No.
fib = fib2 + fib1 # m'th Fibonacci
while fib < n:
fib2 = fib1
fib1 = fib
fib = fib2 + fib1
offset = -1
while fib > 1:
# to get a valid index
i = min(offset + fib2, n - 1)
if arr[i] < x:
fib = fib1
fib1 = fib2
fib2 = fib - fib1
offset = i
elif arr[i] > x:
fib = fib2
fib1 = fib1 - fib2
fib2 = fib - fib1
else:
return i
if fib1 and arr[offset + 1] == x:
return offset + 1
# element not found. return -1
return -1
if __name__ == "__main__":
n = int(input("Enter the number of elements: "))
print("Enter the elements each line")
arr = []
for i in range(n):
arr.append(int(input()))
x = int(input("Enter element to be searched: "))
idx = fibonacci_search(arr, x, n)
if idx == -1:
print("Element not found!")
else:
print("Element found at position: "+str(idx+1))