Skip to content

Instantly share code, notes, and snippets.

@aliifam
Created September 9, 2021 01:42
Show Gist options
  • Select an option

  • Save aliifam/5ba16f8cc1c41a6a76ab5c3a2075a574 to your computer and use it in GitHub Desktop.

Select an option

Save aliifam/5ba16f8cc1c41a6a76ab5c3a2075a574 to your computer and use it in GitHub Desktop.
simple binary search algorithtm implemented with python language
#binanry searching algorithm
item = int(input())
def binary_search(list, item): #parameter is a item want we search and list where the item located
low = 0 #index of first element in list
high = len(list) - 1 #index of last element in list
while low <= high:
mid = (low + high) // 2
guess = list[mid]
if guess == item:
return mid
if guess > item:
high = mid - 1
else:
low = mid + 1
return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, item), "its index element location in list")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment