-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
51 lines (37 loc) · 1.24 KB
/
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
51
'''
left, right and middle represent indicies in the list
binary search returns the index of the found element
if the element is not found, it will return -1
'''
def iterative_binary_search(list_to_search, num_to_find):
left = 0
right = len(list_to_search) - 1
middle = (left + right) / 2
while (left <= right):
if (num_to_find == list_to_search[middle]):
return middle
elif (num_to_find < list_to_search[middle]):
right = middle -1
middle = (left + right) / 2
else:
left = middle + 1
middle = (left + right) / 2
return -1
def recursive_binary_search(list_to_search, num_to_find, right, left=0):
middle = (left + right) / 2
if (left > right):
return -1
if num_to_find == list_to_search[middle]:
return middle
if num_to_find > list_to_search[middle]:
return recursive_binary_search(list_to_search, num_to_find, right, middle+1)
else:
return recursive_binary_search(list_to_search, num_to_find, middle-1, left)
def main():
list_to_search = [1, 2, 3, 4, 5, 6, 10, 11, 29]
num_to_find = 1
print "iterative: ", iterative_binary_search(list_to_search, num_to_find)
right = len(list_to_search) - 1
print "recursive: ", recursive_binary_search(list_to_search, num_to_find, right)
if __name__ == '__main__':
main()