Skip to content

Instantly share code, notes, and snippets.

@goddoe
Last active July 18, 2022 11:07
Show Gist options
  • Save goddoe/50bbbb2204ac0fb10f4250b6e22e791b to your computer and use it in GitHub Desktop.
Save goddoe/50bbbb2204ac0fb10f4250b6e22e791b to your computer and use it in GitHub Desktop.
iterative binary_search
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low+high) // 2
if arr[mid] > target:
high = mid -1
elif arr[mid] == target:
return mid
else:
low = mid + 1
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment