Skip to content

Instantly share code, notes, and snippets.

@RatulSaha
Created January 2, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RatulSaha/a6adc8fc059b7a172fbf173b032c35e3 to your computer and use it in GitHub Desktop.
Save RatulSaha/a6adc8fc059b7a172fbf173b032c35e3 to your computer and use it in GitHub Desktop.
Binary search in Python
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