Skip to content

Instantly share code, notes, and snippets.

@rahulkp220
Created April 28, 2016 17:33
Show Gist options
  • Save rahulkp220/f31868a5f657f46d4d0d01be17207836 to your computer and use it in GitHub Desktop.
Save rahulkp220/f31868a5f657f46d4d0d01be17207836 to your computer and use it in GitHub Desktop.
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