-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_search.py
47 lines (35 loc) · 1.06 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
# Binary Search
# Author: jerrybelmonte
import sys
def binary_search(num_seq: list, key_val: int):
"""
Binary search algorithm implementation.
:param num_seq: sorted sequence of n pairwise distinct positive integers
:param key_val: key value to search for in the sequence of numbers
:return: the index of the element within the sequence of numbers
>>> binary_search([1, 5, 8, 12, 13], 8)
2
>>> binary_search([1, 5, 8, 12, 13], 23)
-1
>>> binary_search([1, 5, 8, 12, 13], 1)
0
>>> binary_search([1, 5, 8, 12, 13], 11)
-1
"""
left, right = 0, len(num_seq) - 1
while left <= right:
mid = left + (right - left)//2
if num_seq[mid] == key_val:
return mid
elif key_val < num_seq[mid]:
right = mid - 1
else:
left = mid + 1
return -1
if __name__ == '__main__':
data = list(map(int, sys.stdin.read().split()))
n = data[0]
m = data[n + 1]
a = data[1:n + 1]
for num in data[n + 2:]:
print(binary_search(a, num), end=' ')