Skip to content

Instantly share code, notes, and snippets.

@stepheweffie
Created March 8, 2018 11:32
Show Gist options
  • Save stepheweffie/890a2532e8861c64138beea2d2dfe288 to your computer and use it in GitHub Desktop.
Save stepheweffie/890a2532e8861c64138beea2d2dfe288 to your computer and use it in GitHub Desktop.
a simple binary search
def binary_search(array, target):
lower = 0
upper = len(array)
while lower < upper:
x = lower + (upper - lower) // 2
value = array[x]
if target == value:
return x
elif target > value:
if lower == x:
break
elif target < value:
upper = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment