Created
October 22, 2020 20:10
-
-
Save jmlon/e2c6e1824f40acaf00d3dbe314de8926 to your computer and use it in GitHub Desktop.
Ejemplo de implementación de la búsqueda binaria de forma recursiva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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