Created
October 19, 2020 15:29
-
-
Save jmlon/ee3c3076b644ccad70f30092a1d15131 to your computer and use it in GitHub Desktop.
Una implementación recursiva de la búsqueda binaria
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
public class BusquedaBinaria { | |
public static int busqueda(int[] d, int clave, int primero, int ultimo) { | |
if (primero>ultimo) { | |
return -1; | |
} | |
else { | |
int medio = (primero+ultimo)/2; | |
if (clave<d[medio]) | |
return busqueda(d, clave, primero, medio-1); | |
else if (clave>d[medio]) | |
return busqueda(d, clave, medio+1, ultimo); | |
else | |
return medio; | |
} | |
} | |
public static void main(String[] args) { | |
int[] datos = { 3,5,7,8,12,15,18,29,33,37,42,45,51,72,83,89,90,95,99 }; | |
int pos = busqueda(datos, 72, 0, datos.length-1); | |
System.out.println(pos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment