Created
May 14, 2016 08:37
-
-
Save Kwpolska/3c8d3e87caef439661c206eca5ac314c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
inp = list(range(1, 1001)) | |
needle = random.randrange(1, 1001) | |
p = 0 | |
q = len(inp) | |
pos = (p + q) // 2 | |
while inp[pos] != needle: | |
print(pos) | |
if inp[pos] > needle: | |
q = pos | |
else: | |
p = pos | |
pos = (p + q) // 2 | |
print(pos, inp[pos], needle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment