Skip to content

Instantly share code, notes, and snippets.

@jaredgrady
Last active April 10, 2017 15:35
Show Gist options
  • Save jaredgrady/f660ab4fa9ae82778f6156744b86fa8f to your computer and use it in GitHub Desktop.
Save jaredgrady/f660ab4fa9ae82778f6156744b86fa8f to your computer and use it in GitHub Desktop.
Binary search a list of ints. Recursive implementation in Python.
def bin_search(L, a):
if (len(L) == 1):
return "NO"
mid = len(L) // 2
if (a < L[mid]):
return bin_search(L[:mid], a)
elif (a > L[mid]):
return bin_search(L[mid:], a)
else:
return "YES"
def main():
a = int(input('Search for: '))
L = [1,3,5,7,12,18,22,38]
print(bin_search(L, a))
main()
@jaredgrady
Copy link
Author

Nice work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment