os métodos de pesquisa mais eficazes em Python com exemplo
def BinarySearch(lys, val):
first = 0
last = len(lys)-1
index = -1
while (first <= last) and (index == -1):
mid = (first+last)//2
if lys[mid] == val:
index = mid
else:
if val<lys[mid]:
last = mid -1
else:
first = mid +1
return index
If we use the function to compute:
>>> BinarySearch([10,20,30,40,50], 20)
Glorious Guanaco