Ejemplo 1: búsqueda binaria en python
def binary_search(item_list,item):
first = 0
last = len(item_list)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if item_list[mid] == item :
found = True
else:
if item < item_list[mid]:
last = mid - 1
else:
first = mid + 1
return found
Ejemplo 2: Python de búsqueda binaria
# This is real binary search
# this algorithm works very good because it is recursive
def binarySearch(arr, min, max, x):
if max >= min:
i = int(min + (max - min) / 2) # average
if arr[i] == x:
return i
elif arr[i] < x:
return binarySearch(arr, i + 1, max, x)
else:
return binarySearch(arr, min, i - 1, x)
¡Haz clic para puntuar esta entrada!
(Votos: 1 Promedio: 4)