Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Created April 1, 2020 10:08
Show Gist options
  • Save nhudinhtuan/4c86b6f8a2b93737d0382870eb0fc99a to your computer and use it in GitHub Desktop.
Save nhudinhtuan/4c86b6f8a2b93737d0382870eb0fc99a to your computer and use it in GitHub Desktop.
Binary Search
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment