-
-
Save anonymous/a8ac3c4d94c53bb01dbdef83ae99b7c7 to your computer and use it in GitHub Desktop.
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
package javaapplication4; | |
public class Busqueda{ | |
public static int binariaRecur(int[] arreglo, int valor){ | |
return binariaRecur(arreglo, valor, 0, arreglo.length-1); | |
} | |
public static int binariaRecur(int[] arreglo, int valor, int inf, int sup){ | |
int mitad = (inf+sup)/2; | |
if ((inf >= sup) && (arreglo[inf] != valor)){ | |
return -1; | |
} | |
else if (arreglo[mitad] == valor){ | |
return mitad; | |
} | |
else if (valor > arreglo[mitad]){ | |
return binariaRecur (arreglo, valor, mitad+1, sup); | |
} | |
return binariaRecur (arreglo, valor, inf, mitad-1); | |
} | |
public static int binariaIter(int[] arreglo, int valor){ | |
int inf = 0; | |
int sup = arreglo.length-1; | |
int mitad; | |
while (inf <= sup){ | |
mitad = (inf+sup)/2; | |
if (valor == arreglo[mitad]){ | |
return mitad; | |
} | |
else if (valor < arreglo[mitad]){ | |
inf = mitad+1; | |
} | |
else if (valor > arreglo[mitad]){ | |
sup = mitad-1; | |
} | |
} | |
return -1; | |
} | |
public static int lineal(int[] arreglo, int valor){ | |
for (int i = 0; i < arreglo.length; i++){ | |
if (arreglo[i] == valor){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
} |
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
package javaapplication4; | |
public class JavaApplication4 { | |
public static void main(String[] args) { | |
Busqueda bus = new Busqueda(); | |
int[] arreglo1 = new int[10]; | |
int[] arreglo2 = new int[100]; | |
int[] arreglo3 = new int[10000]; | |
int[] arreglo4 = new int[1000000]; | |
int[] arreglo5 = new int[10000000]; | |
long[] tiemposBin = new long[5]; | |
long[] tiemposLin = new long[5]; | |
long start, end; | |
for (int i = 0; i < 10; i++) arreglo1[i] = i; | |
for (int i = 0; i < 100; i++) arreglo2[i] = i; | |
for (int i = 0; i < 10000; i++) arreglo3[i] = i; | |
for (int i = 0; i < 1000000; i++) arreglo4[i] = i; | |
for (int i = 0; i < 10000000; i++) arreglo5[i] = i; | |
start = System.nanoTime(); | |
bus.binariaRecur(arreglo1, 0); | |
end = System.nanoTime(); | |
tiemposBin[0] = end-start; | |
start = System.nanoTime(); | |
bus.binariaRecur(arreglo2, 70); | |
end = System.nanoTime(); | |
tiemposBin[1] = end-start; | |
start = System.nanoTime(); | |
bus.binariaRecur(arreglo3, 10000); | |
end = System.nanoTime(); | |
tiemposBin[2] = end-start; | |
start = System.nanoTime(); | |
bus.binariaRecur(arreglo4, 999999); | |
end = System.nanoTime(); | |
tiemposBin[3] = end-start; | |
start = System.nanoTime(); | |
bus.binariaRecur(arreglo5, 9999999); | |
end = System.nanoTime(); | |
tiemposBin[4] = end-start; | |
start = System.nanoTime(); | |
bus.lineal(arreglo1, 0); | |
end = System.nanoTime(); | |
tiemposLin[0] = end-start; | |
start = System.nanoTime(); | |
bus.lineal(arreglo2, 70); | |
end = System.nanoTime(); | |
tiemposLin[1] = end-start; | |
start = System.nanoTime(); | |
bus.lineal(arreglo3, 10000); | |
end = System.nanoTime(); | |
tiemposLin[2] = end-start; | |
start = System.nanoTime(); | |
bus.lineal(arreglo4, 999999); | |
end = System.nanoTime(); | |
tiemposLin[3] = end-start; | |
start = System.nanoTime(); | |
bus.lineal(arreglo5, 9999999); | |
end = System.nanoTime(); | |
tiemposLin[4] = end-start; | |
System.out.println("Resultados Busqueda Binaria:"); | |
for (int i = 0; i < 5; i++){ | |
System.out.println("Prueba " + (i+1) + ": " + tiemposBin[i]/1000000000.0 + " segundos"); | |
} | |
System.out.println(); | |
System.out.println("Resultados Busqueda Lineal:"); | |
for (int i = 0; i < 5; i++){ | |
System.out.println("Prueba " + (i+1) + ": " + tiemposLin[i]/1000000000.0 + " segundos"); | |
} | |
System.out.println("\nFin"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment