-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
53 lines (38 loc) · 904 Bytes
/
binary_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
import time
# sorting starts here...
l = []
x = int(input("how many attempts = "))
for i in range(x):
a = int(input(f'enter number{x-i} = '))
l.append(a)
def main():
for i in range(len(l)-1):
if l[i] > l[i+1]:
l[i] , l[i+1] = l[i+1] , l[i]
else:
pass
for i in range(x):
main()
print(l)
print("all numbers are allocated.. and sorted...")
# binary search starts here...
key = int(input('enter the number you want to search in list = '))
start = time.time()
i = 0
j = len(l)-1
cond = False
while i <= j and not cond:
mid = (i+j) // 2
if key == l[mid]:
cond = True
pos = mid+1
elif key > l[mid]:
i = mid+1
else:
j = mid - 1
if cond == True:
print('no. found at ',pos)
else:
print('no. not found at ')
end = time.time()
print('binary search executed in ', end - start , ' seconds')