Skip to content

Instantly share code, notes, and snippets.

@nuriyevn
Created March 26, 2019 13:04
Show Gist options
  • Save nuriyevn/30a2bdfa3b82e2698acf9dc9f59e4bf9 to your computer and use it in GitHub Desktop.
Save nuriyevn/30a2bdfa3b82e2698acf9dc9f59e4bf9 to your computer and use it in GitHub Desktop.
Binary Search
def binary_search(list, item):
low = 0
high = len(list) - 1
while (low <= high):
mid = int((low + high) / 2)
guess = list[mid]
if guess == item:
return mid
if guess > item:
high = mid - 1
elif guess < item:
low = mid + 1
return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 9))
print(binary_search(my_list, 576))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment