Created
April 28, 2016 17:33
-
-
Save rahulkp220/f31868a5f657f46d4d0d01be17207836 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
def BinarySearch(my_item,my_list): | |
found = False | |
bottom = 0 | |
top = len(my_list)-1 | |
while bottom<=top and not found: | |
middle = (bottom+top)//2 | |
if my_list[middle] == my_item: | |
found = True | |
elif my_list[middle] < my_item: | |
bottom = middle + 1 | |
else: | |
top = middle - 1 | |
return found | |
if __name__ == "__main__": | |
my_list = [23,45,67,89,94] | |
my_item = int(input("enter the number you are looking for? ")) | |
result = BinarySearch(my_item,my_list) | |
if result: | |
print("Yes we found it") | |
else: | |
print("Nopes, we are sorry!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment