-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.py
46 lines (34 loc) · 1.04 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
"""
The list only has distinct elements,
meaning there are no repeated values, and
elements are in a strictly increasing order.
"""
def binary_search(input_array, value):
"""
inputs: a list to search through, the value to search for.
outputs: the index of value, or -1 if the value doesn't exist in the list.
"""
result = -1
if input_array!=[]:
switch = False
start = 0
end = len(input_array)-1
while start<=end and not switch:
pointer = (start+end)//2
if input_array[pointer]==value:
result = pointer
switch = True
break
elif input_array[pointer]<value:
start = pointer+1
else:
end = pointer-1
return result
"""
Test case:
test_list = [1,3,9,11,15,19,29]
test_val1 = 25
test_val2 = 15
print binary_search(test_list, test_val1)
print binary_search(test_list, test_val2)
"""