Created
January 2, 2017 20:45
-
-
Save RatulSaha/a6adc8fc059b7a172fbf173b032c35e3 to your computer and use it in GitHub Desktop.
Binary search in Python
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
arr, target = [1,2,3,4,5], 4 | |
start, end = 0, len(n)-1 | |
while start <= end: | |
mid = (start+end)/2 | |
if arr[mid] == target: | |
return mid # The value is found | |
else: | |
if arr[mid] < target: | |
start = mid+1 | |
else: | |
end = mid-1 | |
return -1 # The value is not found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment