/ Published in: Java
Expand |
Embed | Plain Text
class BusquedaAlgoritmo { public static int buscar( int [] arreglo, int dato) { int inicio = 0; int fin = arreglo.length - 1; int pos; while (inicio <= fin) { pos = (inicio+fin) / 2; if ( arreglo[pos] == dato ) return pos; else if ( arreglo[pos] < dato ) { inicio = pos+1; } else { fin = pos-1; } } return -1; } } public class BusquedaBinaria { // Llenar arreglo int [] edades = new int [35]; for (int i = 0; i < edades.length ; i++) edades[i] = i*i ; // Mostrar arreglo. for (int i = 0; i < edades.length ; i++) int resultado = BusquedaAlgoritmo.buscar(edades, 9); if (resultado != -1) { } else { } } }
You need to login to post a comment.
