Last active
July 18, 2022 11:07
-
-
Save goddoe/50bbbb2204ac0fb10f4250b6e22e791b to your computer and use it in GitHub Desktop.
iterative 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(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