Skip to content

Instantly share code, notes, and snippets.

@jmlon
Created October 22, 2020 20:10
Show Gist options
  • Save jmlon/e2c6e1824f40acaf00d3dbe314de8926 to your computer and use it in GitHub Desktop.
Save jmlon/e2c6e1824f40acaf00d3dbe314de8926 to your computer and use it in GitHub Desktop.
Ejemplo de implementación de la búsqueda binaria de forma recursiva
def busquedaBinaria(lista, dato, inicio, fin):
"""Búsqueda binario de un dato en una lista.
La búsqueda opera recursivamente en el rango entre inicio y fin inclusive
"""
if inicio>fin:
return -1
else:
medio = (inicio+fin)//2
if lista[medio]>dato:
return busquedaBinaria(lista, dato, inicio, medio-1)
elif lista[medio]<dato:
return busquedaBinaria(lista, dato, medio+1, fin)
else:
return medio
x = [ 2,7,9,12,18,21 ]
print( busquedaBinaria(x,3,0,len(x)-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment