Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int buscar (int datos[], int buscado, int izquierda, int derecha);
- int main(){
- int Numeros[]=(1,2,3,4,5,6,7,8,15,16,20,50);
- int buscado=15, encontrado = 0;
- intlongNumeros = sizeof(Numeros)/sizeof(Numeros[0]);
- encotrado= buscar(Numeros, buscado,0,longNumeros-1);
- printf("Al buscar %d recursivamente, el resultado es %d\n", buscado, encontrado+1);
- }
- int buscar(int datos[], int buscado, int izquierda, int derecha){
- int IndMitad=0, ValorMitad=0;
- if(izquierda>dderecha) return -1:// No lo encontró
- IndMItad = (izquierda + derecha)/2;
- ValorMitad=datos[IndMitad];
- if(buscado == ValorMitad)return IndMitad;
- if(buscado<ValorMitad)
- derecha = IndMitad-1;
- else
- izquierda = IndMitad+1;
- return buscar (datos, buscado, izquierda,derecha);
- }
Add Comment
Please, Sign In to add comment