Last active
January 30, 2019 03:56
-
-
Save angelvalay/c810df75a1e5aa806b855c7b95f3a8e0 to your computer and use it in GitHub Desktop.
Template de la busqueda binaria en C++ 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
// | |
// Creado por Angel Valay el 1/29/2019. | |
// | |
#include <iostream> | |
using namespace std; | |
template <class T> | |
T busquedaBinaria(T a[],T clave, int inf, int sup){ | |
T central; | |
if(inf > sup) | |
return -1; | |
else{ | |
central = (inf + sup)/2; | |
if (a[central] == clave) | |
return central; | |
else if (a[central] < clave) | |
return busquedaBinaria(a,clave,central + 1, sup); | |
else | |
return busquedaBinaria(a,clave,inf, central -1); | |
} | |
} | |
int main(){ | |
int a[10] = {1,2,3,4,5,6,7,10,100}; | |
// Parametros: array de elementos, elemento a buscar, posicion inicial a buscar, posicion final a buscar | |
cout<<busquedaBinaria(a,2,0,9); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment