-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_11(part2)(python).py
75 lines (56 loc) · 1.7 KB
/
Practical_11(part2)(python).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
'''
Write a python program to store roll numbers of student array who attended training
program in sorted order. Write function for searching whether particular student
attended training program or not, using Binary search and Fibonacci search
'''
def binary_search(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return True
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return False
def fibonacci_search(arr, x):
fib2 = 0
fib1 = 1
fib = fib1 + fib2
while fib < len(arr):
fib2 = fib1
fib1 = fib
fib = fib1 + fib2
offset = -1
while fib > 1:
i = min(offset + fib2, len(arr) - 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 True
if fib1 and arr[offset + 1] == x:
return True
return False
def main():
roll_numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
roll_numbers.sort()
search_num = 50
if binary_search(roll_numbers, search_num):
print(f"{search_num} attended the training program (Binary Search)")
else:
print(f"{search_num} did not attend the training program (Binary Search)")
if fibonacci_search(roll_numbers, search_num):
print(f"{search_num} attended the training program (Fibonacci Search)")
else:
print(f"{search_num} did not attend the training program (Fibonacci Search)")
if __name__ == '__main__':
main()