Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- [Shivgopal07](https://github.com/Shivgopal07)
- [Shweta-Rashi](https://github.com/Shweta-Rashi)
- [Abhineet Mishra](https://github.com/abhineetmishra64)
- [Yassine Latreche](https://github.com/Yassine-Latreche)
25 changes: 25 additions & 0 deletions Searching Algorithms/BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Uses python3
import sys
import math

# Binary Search Function
def binary_search(a, left, right, x):
if left > right:
return -1
mid = left + math.floor((right - left) / 2)
if x == a[mid]:
return mid
elif x < a[mid]:
return binary_search(a, left, mid - 1, x)
else:
return binary_search(a, mid + 1, right, x)


if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
n = data[0]
m = data[n + 1]
a = data[1: n + 1]
for x in data[n + 2:]:
print(binary_search(a, 0, n - 1, x), end=' ')