-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.py
More file actions
32 lines (25 loc) · 906 Bytes
/
binarySearch.py
File metadata and controls
32 lines (25 loc) · 906 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
27
28
29
30
31
32
# Given a sorted array arr and a value x to locate, returns location of x if present, else returns -1. O(log(n))
def binary_search(arr, x):
# Set boundaries of search
lower = 0
upper = len(arr) - 1
# Iterate over array
while lower <= upper: # O(log(n))
# Calculate midpoint
mid = lower + (upper - lower) // 2
# Check if x is present at mid
if arr[mid] == x:
return mid
# If x is greater, ignore left half
elif arr[mid] < x:
lower = mid + 1
# If x is smaller, ignore right half
else:
upper = mid - 1
# If we reach here, then the element
# was not present
return -1
print(binary_search([1, 2, 4, 6, 8, 10], 6) == 3)
print(binary_search([1, 2, 4, 6, 8, 10], 1) == 0)
print(binary_search([1, 2, 4, 6, 8, 10], 10) == 5)
print(binary_search([1, 2, 4, 6, 8, 10], 3) == -1)