Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active Jan 21, 2022
Embed
What would you like to do?
Binary search
def binary_search(target, arr):
low = 0
high = len(arr) - 1 # it starts with index 0
while low <= high:
mid = (low + high) // 2 # it represents the index of the middle element
if arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return low != len(arr) and arr[low] == target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment