-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search-algorithm.py
44 lines (36 loc) · 1.15 KB
/
binary-search-algorithm.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
'''
Binary Search
-------------------------------------------------------------
'''
def binary_search(a_list, an_item):
first = 0
last = len(a_list) - 1
while first <= last:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
else:
first = mid_point + 1
return False
def binary_search_rec(a_list, first, last, an_item):
if len(a_list) == 0:
return False
else:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
return binary_search_rec(a_list, first, last, an_item)
else:
first = mid_point + 1
return binary_search_rec(a_list, first, last, an_item)
if __name__ == '__main__':
a_list = [1, 4, 7, 10, 14, 19, 102, 2575, 10000]
print('Binary Search:', binary_search(a_list, 4))
print('Binary Search Recursive:',
binary_search_rec(a_list, 0, len(a_list) -1, 4))