Skip to content

Instantly share code, notes, and snippets.

@runtimee
Last active May 5, 2021 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runtimee/d929bc63fb5649d703f1ebfad5f33309 to your computer and use it in GitHub Desktop.
Save runtimee/d929bc63fb5649d703f1ebfad5f33309 to your computer and use it in GitHub Desktop.
binary search
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