Skip to content
This repository was archived by the owner on Aug 26, 2020. It is now read-only.

Commit afbd17a

Browse files
author
BEAST GLATISANT
committed
added more
1 parent 39bb81d commit afbd17a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

binary_search.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Uses python3
2+
import sys
3+
4+
def binary_search(arr, x):
5+
l, r = 0, len(arr)-1
6+
while l <= r:
7+
mid = l + (r - l) // 2
8+
if arr[mid] == x:
9+
return mid
10+
elif arr[mid] < x:
11+
l = mid + 1
12+
else:
13+
r = mid - 1
14+
return -1
15+
16+
def linear_search(a, x):
17+
for i in range(len(a)):
18+
if a[i] == x:
19+
return i
20+
return -1
21+
22+
if __name__ == '__main__':
23+
input = sys.stdin.read()
24+
data = list(map(int, input.split()))
25+
n = data[0]
26+
m = data[n + 1]
27+
a = data[1 : n + 1]
28+
for x in data[n + 2:]:
29+
# replace with the call to binary_search when implemented
30+
print(binary_search(a, x), end = ' ')

0 commit comments

Comments
 (0)