Last active
May 5, 2021 21:46
-
-
Save runtimee/d929bc63fb5649d703f1ebfad5f33309 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 binary_search_basic(arr, target): | |
low, high = 0, len(arr) | |
while low < high: | |
mid = (low+high) // 2 # low + (high-low)//2 if cares about overflow | |
if target < arr[mid]: | |
high = mid | |
elif target > arr[mid]: | |
low = mid + 1 | |
else: | |
return mid | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment