Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created May 9, 2017 06:59
Show Gist options
  • Select an option

  • Save monkey-codes/d43816c846785bb691f9062f963037fd to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/d43816c846785bb691f9062f963037fd to your computer and use it in GitHub Desktop.
Python simple binary search
def binary_search(input_array, value):
low = 0
high = len(input_array) -1
while low <= high:
middle_idx = (low + high)/2
middle = input_array[middle_idx]
if value < middle:
high = middle_idx - 1
elif value > middle:
low = middle_idx + 1
else:
return middle_idx
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment