Skip to content

Instantly share code, notes, and snippets.

@toniesteves
Last active June 15, 2020 05:17
Show Gist options
  • Save toniesteves/0a0eda6792feb7b8de52d2e374bd28c7 to your computer and use it in GitHub Desktop.
Save toniesteves/0a0eda6792feb7b8de52d2e374bd28c7 to your computer and use it in GitHub Desktop.
items = [1, 2, 3, 4, 5, 6 , 7, 8, 9, 10]
def binary_search(alist, item):
first = 0
last = len(alist)-1
found = False
while first <= last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
print(binary_search(items, 19))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment