Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/f72ccf554b600d6ba0fbe8cbf4fd6271 to your computer and use it in GitHub Desktop.
Save parzibyte/f72ccf554b600d6ba0fbe8cbf4fd6271 to your computer and use it in GitHub Desktop.
"""
Algoritmo de búsqueda binaria recursiva escrito en Python
Este método funciona con cadenas y números por igual, pues
este lenguaje (justo como JavaScript) trata a las cadenas
lexicográficamente; comparando sus valores en el código ASCII
@author parzibyte
@web parzibyte.me/blog
"""
def busqueda_binaria_recursiva(arreglo, busqueda, izquierda, derecha):
if izquierda > derecha:
return -1
indiceDelElementoDelMedio = (izquierda + derecha) // 2
elementoDelMedio = arreglo[indiceDelElementoDelMedio]
if elementoDelMedio == busqueda:
return indiceDelElementoDelMedio
if busqueda < elementoDelMedio:
return busqueda_binaria_recursiva(arreglo, busqueda, izquierda, indiceDelElementoDelMedio - 1)
else:
return busqueda_binaria_recursiva(arreglo, busqueda, indiceDelElementoDelMedio + 1, derecha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment