Created
April 1, 2020 10:08
-
-
Save nhudinhtuan/4c86b6f8a2b93737d0382870eb0fc99a to your computer and use it in GitHub Desktop.
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): | |
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