-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
29 lines (25 loc) · 887 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
import random
def binarySearch(dataList, num):
index = len(dataList) // 2
prevIndex = None
while(index != 0 or index != len(dataList) - 1):
# print("Prev Index: {}".format(prevIndex))
# print("Index: {}".format(index))
if dataList[index] == num:
return index
elif dataList[index] > num:
prevIndex = index
index = index // 2
else:
if not prevIndex:
prevIndex = len(dataList)
index = (index + prevIndex) // 2
return None
if __name__ == "__main__":
dataList = sorted(random.sample(range(-100, 100), 10))
for index in range(0, 10):
num = dataList[index]
print("Input list: {}".format(dataList))
print("Search for number: {}\tIndex: {}".format(num, index))
result = binarySearch(dataList, num)
print(result)