-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
26 lines (23 loc) · 897 Bytes
/
BinarySearch.py
File metadata and controls
26 lines (23 loc) · 897 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
def binary_search(sorted_list, target):
left_pointer = 0
right_pointer = len(sorted_list)
# fill in the condition for the while loop
while left_pointer < right_pointer:
# calculate the middle index using the two pointers
mid_idx = (left_pointer + right_pointer) // 2
mid_val = sorted_list[mid_idx]
if mid_val == target:
return mid_idx
if target < mid_val:
# set the right_pointer to the appropriate value
right_pointer = mid_idx
if target > mid_val:
# set the left_pointer to the appropriate value
left_pointer = mid_idx + 1
return "Value not in list"
# test cases
print(binary_search([5,6,7,8,9], 9))
print(binary_search([5,6,7,8,9], 10))
print(binary_search([5,6,7,8,9], 8))
print(binary_search([5,6,7,8,9], 4))
print(binary_search([5,6,7,8,9], 6))