Skip to content

Instantly share code, notes, and snippets.

@juniorUsca
Created February 3, 2021 22:30
Show Gist options
  • Save juniorUsca/a00e7ed4404fb439294e39e7e45f2925 to your computer and use it in GitHub Desktop.
Save juniorUsca/a00e7ed4404fb439294e39e7e45f2925 to your computer and use it in GitHub Desktop.
busqueda_binaria
datos = [100,1,2,3,4,10,20,45,67,32]
buscar = 32
datos.sort()
print(datos)
inicio = 0
fin = len(datos) - 1
encontrado = False
while encontrado == False and inicio <= fin:
mitad = inicio + (fin-inicio)//2
print("inicio, fin", inicio, fin)
print("posicion mitad", mitad)
print("elemento mitad", datos[mitad])
print("--------")
if datos[mitad] == buscar:
encontrado = True
else:
if datos[mitad] > buscar:
fin = mitad - 1
else:
inicio = mitad + 1
if encontrado == True:
print("siiii 😎")
else:
print("no esta en la lista 🥲")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment