Answer the question
In order to leave comments, you need to log in
Is this search binary?
# Бинарный поиск
def binary_search(list, item):
low = 0 # начальная граница списка в которой осуществляется поиск
high = len(list) - 1 # позиция последнего элемента списка
while low <= high:# пока диапазон не сузится что они буду равны
mid = (low + high) # проверяем средний элемент
guess = list[mid]
if item == guess:
return mid
if guess > item:
high = mid - 1
else:
high = mid + 1
return none
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 3)) # выводит порядковый номер наденного элемента
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question