Skip to content

Instantly share code, notes, and snippets.

@odysseus0
Created August 8, 2022 12:17
Show Gist options
  • Save odysseus0/d33702f279ae5ed64ab8723f5c198122 to your computer and use it in GitHub Desktop.
Save odysseus0/d33702f279ae5ed64ab8723f5c198122 to your computer and use it in GitHub Desktop.
[Binary Search] Classical Binary Search #python #algo
def binarySearch(array, target):
"""
input: int[] array, int target
return: int
"""
l = 0
r = len(array)
while l < r:
m = l + (r - l) // 2
if array[m] == target:
return m
elif target < array[m]:
r = m
else:
l = m + 1
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment