Created
March 26, 2019 13:04
-
-
Save nuriyevn/30a2bdfa3b82e2698acf9dc9f59e4bf9 to your computer and use it in GitHub Desktop.
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(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