Created
March 8, 2018 11:32
-
-
Save stepheweffie/890a2532e8861c64138beea2d2dfe288 to your computer and use it in GitHub Desktop.
a simple 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(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