Created
December 12, 2019 19:54
-
-
Save Chetan-Goyal/37fb7e1807f5db5349bc5592da9d1b5b to your computer and use it in GitHub Desktop.
binary_search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def BS(arr, n): | |
l, u = 0, len(arr)-1 | |
while l <= u: | |
m = (l+u)//2 | |
if n == arr[m]: | |
return m | |
elif n < arr[m]: | |
u = m-1 | |
else: | |
l = m+1 | |
return -1 | |
print(BS([1,2,3,4,5,6,7,8,9], 7)) | |
print(BS([1,2,3,4,5,6,7,8,9], 10)) |
Author
Chetan-Goyal
commented
Dec 12, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment