SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <math.h> | |
4 | int buscar (int datos[], int buscado, int izquierda, int derecha); | |
5 | int main(){ | |
6 | int Numeros[]=(1,2,3,4,5,6,7,8,15,16,20,50); | |
7 | int buscado=15, encontrado = 0; | |
8 | intlongNumeros = sizeof(Numeros)/sizeof(Numeros[0]); | |
9 | encotrado= buscar(Numeros, buscado,0,longNumeros-1); | |
10 | printf("Al buscar %d recursivamente, el resultado es %d\n", buscado, encontrado+1); | |
11 | } | |
12 | int buscar(int datos[], int buscado, int izquierda, int derecha){ | |
13 | int IndMitad=0, ValorMitad=0; | |
14 | if(izquierda>dderecha) return -1:// No lo encontró | |
15 | IndMItad = (izquierda + derecha)/2; | |
16 | ValorMitad=datos[IndMitad]; | |
17 | if(buscado == ValorMitad)return IndMitad; | |
18 | if(buscado<ValorMitad) | |
19 | derecha = IndMitad-1; | |
20 | else | |
21 | izquierda = IndMitad+1; | |
22 | return buscar (datos, buscado, izquierda,derecha); | |
23 | } |