Created
August 7, 2019 16:00
-
-
Save parzibyte/d33ae718a789f950ab609f3d6a1c0213 to your computer and use it in GitHub Desktop.
Búsqueda binaria recursiva en C - https://parzibyte.me/blog/2018/11/08/c-busqueda-binaria-recursiva-secuencial-arreglos/
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
/** | |
* | |
* Implementación del algoritmo de | |
* búsqueda binaria recursiva en un arreglo usando C | |
* | |
* Funciona con arreglos de números | |
* | |
* Autor: parzibyte | |
* Web: parzibyte.me/blog | |
*/ | |
int busquedaBinariaRecursiva(int arreglo[], int busqueda, int izquierda, int derecha){ | |
if (izquierda > derecha) return -1; | |
int indiceDeLaMitad = floor((izquierda + derecha) / 2); | |
int valorQueEstaEnElMedio = arreglo[indiceDeLaMitad]; | |
if (busqueda == valorQueEstaEnElMedio){ | |
return indiceDeLaMitad; | |
} | |
if (busqueda < valorQueEstaEnElMedio){ | |
// Entonces está hacia la izquierda | |
derecha = indiceDeLaMitad - 1; | |
}else{ | |
// Está hacia la derecha | |
izquierda = indiceDeLaMitad + 1; | |
} | |
return busquedaBinariaRecursiva(arreglo, busqueda, izquierda, derecha); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment