-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathBinary_Search.py
More file actions
31 lines (22 loc) · 763 Bytes
/
Binary_Search.py
File metadata and controls
31 lines (22 loc) · 763 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
# Function for binary search
# inputs: sorted array 'arr', key to be searched 'x'
# returns: index of the occurrence of x found in arr
def binary_search(arr, x):
l = 0
r = len(arr)
# while the left index marker < right index marker
while l < r:
# find the index of the middle element
mid = int(l + ((r - l) / 2))
# if middle element is x, return mid
if arr[mid] == x:
return mid
# if middle element is < x, update l to search to the right of mid
elif arr[mid] < x:
l = mid + 1
# if middle element is > x, update r to search to the left of mid
else:
r = mid - 1
return -1
arr = [1, 4, 5, 7, 8, 10, 13, 15]
print(binary_search(arr, 5))