forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacciSearch.py.py
55 lines (39 loc) · 1.21 KB
/
fibonacciSearch.py.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
# return the fibonacci number at the index of n
def FibonacciGenerator(n):
if n < 1:
return 0
elif n == 1:
return 1
else:
return FibonacciGenerator(n - 1) + FibonacciGenerator(n - 2)
# return the index at which x exists inside arr
# return -1 otherwise
def FibonacciSearch(arr, x):
# find the smallest Fibonacci number greater than or equal
# to the length of arr
m = 0
while FibonacciGenerator(m) < len(arr): #
m = m + 1
offset = -1
while (FibonacciGenerator(m) > 1):
i = min( offset + FibonacciGenerator(m - 2) , len(arr) - 1)
if (x > arr[i]):
m = m - 1
offset = i
elif (x < arr[i]):
m = m - 2
else:
return i
# this will run if there is one last element left
if(FibonacciGenerator(m - 1) and arr[offset + 1] == x):
return offset + 1
# return -1 if the element doesn't exist in the array
return -1
# the search array
print("Enter array : ", end='')
arr = list(map(int, input().split()))
x = int(input("Enter number to be searched : "))
#if array is not sorted.
print(FibonacciSearch(sorted(arr), x))
#if array is sorted
#print(FibonacciSearch(arr, x))