forked from raamav/StandalonePrograms_Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBisection-Search-Lists.py
More file actions
36 lines (33 loc) · 811 Bytes
/
Bisection-Search-Lists.py
File metadata and controls
36 lines (33 loc) · 811 Bytes
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
#Implement bi-section search on a list
# use a list of first 100 odd numbers
L = []
for i in range(1,200,2):
L.append(i)
def bisectionSearch(L,n):
i = 0
low = 0
high = len(L)-1
mid = int((low+high)*0.5)
while True:
if n > L[high] or n < L[low]:
i+=1
return False
elif n == L[high]:
i+=1
print("Iterations ",i)
return i
elif n > L[mid]:
low = mid
mid = int((low+high)*0.5)
i+=1
elif n < L[mid]:
high = mid
mid = int((low+high)*0.5)
i+=1
elif n == L[mid]:
i+=1
print ("iterations ",i)
return mid
elif i > 1000:
return -99
bisectionSearch(L,19)