Documente Academic
Documente Profesional
Documente Cultură
Calcular ab.
Se debe crear una nica clase llamada Math2, a la cual tenemos que implementar los mtodos
estticos necesarios para resolver los problemas planteados.
Una vez terminada nuestra clase Math2 debemos crear una clase tipo Sistema que nos permita
crear conjuntos de datos de distinto tamao, los cuales nos permitirn obtener los tiempos
resultantes para cada tipo de soluciones y as finalmente construir grficos que nos muestren
los resultados obtenidos.
DESARROLLO
La Clase Math2:
Una vez comprendida nuestra descripcin del problema, comenzamos a definir nuestra clase
Math2. Para solucionar el problema implementamos ocho funciones, estas son:
Pgina 1 de 10
Cdigo:
public class Math2{
public static int BuscaKesimoIneficiente(int []x,int k){
int inicio = 0;
int[]aux;
aux = x;
int tope = aux.length-1;
quickSort(aux,inicio,tope);
return aux[k-1];
}
//esto funciona solo con la mediana del arreglo//
public static int BuscaKesimoEficiente(int [] x,int k){
int tam=x.length;
// creamos el pivote y usamos el metodo math.ceil para
redondear.
int pivote = (int)Math.ceil(tam/2);
int[] uno=x;
int w=0,z=0;
int menores=0;
//creamos el ciclo para identificar el kaesimo
while(true){
if (tam == 1) {
return uno[0];
} else {
w=z=0;
int[] dosMenor=new int[tam-1];
int[] dosMayor=new int[tam-1];
for (int i = 0; i < tam; i++) {
if (i != pivote) {
if (uno[i] < uno[pivote]) {
dosMenor[w] = uno[i];
w++;
}
else {
dosMayor[z] = uno[i];
z++;
}
Pgina 2 de 10
}
}
if (k == w + 1 + menores) {
return uno[pivote];
}
else {
if (k <= w + menores) {
uno=dosMenor;
tam=w;
}
else {
uno=dosMayor;
tam=z;
menores+=z+1;
}
}
pivote=(int)Math.ceil(tam/2);
}
}
public static int PotenciaIneficiente(int base, int Exponente) {
int mult = 1;
for(int i=1;i<=Exponente;i++){
mult = base * mult;
}
return mult;
}
public static int PotenciaEficiente(int base,int exponente){
int i = 0;
int aux = base;
if(exponente==0){
return 1;
}
else{
if(exponente == 1){
return base;
}
else{
while(i<exponente-1){
aux = base*aux;
i++;
}
return aux;
}
}
}
Pgina 3 de 10
return false;
}
}
}
return false;
}
public static boolean BuscarEficiente(int x[], int numero) {
int menor = 0, mayor = x.length - 1;
int pivote;
while (menor <= mayor) {
pivote = (int) (mayor - menor) / 2 + menor;
if (numero == x[pivote]) {
return true;
} else {
if (numero > x[pivote]) {
mayor = pivote - 1;
} else {
menor = pivote + 1;
}
}
}
return false;
}
private
int
int
int
do{
while(a[i]<pivot)i++;
while(a[j]>pivot)j--;
if(i<=j){
int aux = a[i];
a[i] = a[j];
a[j] = aux;
i++;
j--;
}
}while(i<=j);
if(left<j)quickSort(a,left,j);
if(i<right)quickSort(a,i,right);
}
public static void quickSort2( int a[], int left, int right){
int i = left;
int j = right;
int pivot = a[(int)(left+right)/2];
do{
while(a[i]>pivot)i++;
while(a[j]<pivot)j--;
if(i<=j){
int aux = a[i];
a[i] = a[j];
a[j] = aux;
i++;
j--;
}
Pgina 4 de 10
}while(i<=j);
if(left<j)quickSort(a,left,j);
if(i<right)quickSort(a,i,right);
}
La Clase Usar:
En primer lugar creamos el buffered, para leer los datos que solicitar el programa al usuario; el
valor de k para calcular el k-simo, el valor de a y b para calcular ab y un valor a buscar
dentro del arreglo.
Luego implementamos un pequeo men en el cual podemos elegir probar e algoritmo entre
1000 elementos, 10000 elementos, 100000 elementos.
Para cada caso, es decir, probando nuestras soluciones con arreglos de 1000, 10000 y 100000
elementos, haremos los siguientes pasos:
Comenzaremos con encontrar el K-simo elemento del arreglo, donde debemos ingresar el
valor de K, encontrarlo dentro del arreglo de forma eficiente, usando la funcin
BuscaKesimoEficiente de la Clase Math2, y lineal, usando la funcin BuscaKesimoIneficiente
de la clase Math2, y luego desplegar en pantalla los tiempos transcurridos para cada tipo de
solucin.
En seguida debemos ingresar el valor de a (base) y el valor de b (exponente), calcular el
valor de la potencia de forma Ineficiente, usando la funcin PotenciaIneficiente de la clase
Math2, y eficiente, usando la funcin PotenciaEficiente de la clase Math2, y desplegar en
pantalla los tiempos transcurridos para cada tipo de solucin.
Luego se desea buscar un elemento dentro de un arreglo, aqu ingresamos el nmero a buscar,
encontramos el elemento ingresado de manera eficiente, usando la funcin BuscaEficiente de
la clase Math2, y de manera ineficiente, usando la funcin BuscaIneficiente de la Clase Math2,
finalmente desplegamos en pantalla los tiempos para queda tipo de bsqueda.
Codigo Fuente:
Cdigo:
import java.util.Vector;
import java.util.Random;
import java.io.*;
public class Usar{
public static void main(String[]args){
BufferedReader tld = new BufferedReader(new
InputStreamReader(System.in));
int l = 0;
int[]w = new int [1000];
Random rand1 = new Random();
for(int i=0;i<w.length;i++){
w[i] =rand1.nextInt(150000-0);
}
int[]x = new int [10000];
Random rand2 = new Random();
Pgina 5 de 10
for(int f=0;f<x.length;f++){
x[f] =rand2.nextInt(150000-0);
}
int[]y = new int [100000];
Random rand3 = new Random();
for(int g=0;g<y.length;g++){
y[g] =rand3.nextInt(150000-0);
}
do{
try{
System.out.println("");
System.out.println("1. Probar Algoritmos con 1000 elementos");
System.out.println("2. Probar Algoritmos con 10000
elementos");
System.out.println("3. Probar Algoritmos con 100000
elementos");
int m = Integer.parseInt(tld.readLine());
switch(m){
case 1:{
System.out.println("Ingrese el valor de K");
int K = Integer.parseInt(tld.readLine());
long Start = System.nanoTime();
Math2.BuscaKesimoIneficiente(w,K);
long End = System.nanoTime();
long Time = End-Start;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Ineficiente es "+Time );
long Start2 = System.nanoTime();
Math2.BuscaKesimoEficiente(w,K);
long End2 = System.nanoTime();
long Time2 = End2-Start2;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Eficiente es "+Time2 );
System.out.println("Ingrese La Base Para la
potencia");
int a = Integer.parseInt(tld.readLine());
System.out.println("Ingrese El Exponente Para la
potencia");
int b = Integer.parseInt(tld.readLine());
long Start3 = System.nanoTime();
Math2.PotenciaIneficiente(a,b);
long End3 = System.nanoTime();
long Time3 = End3-Start3;
System.out.println("Tiempo en nanosegundos de Potencia
Ineficinete es "+Time3 );
long Start4 = System.nanoTime();
Math2.PotenciaEficiente(a,b);
long End4 = System.nanoTime();
long Time4 = End4-Start4;
System.out.println("Tiempo en nanosegundos de Potencia
Eficiente es "+Time4 );
System.out.println("Ingrese El Numero a Buscar");
int q=Integer.parseInt(tld.readLine());
Pgina 6 de 10
int[]t=new int[w.length];
for(int h=0;h<w.length;h++){
t[h]=w[h];
}
Math2.quickSort2(t,0,t.length-1);
long Start5 = System.nanoTime();
Math2.BuscarIneficiente(t,q);
long End5 = System.nanoTime();
long Time5 = End5-Start5;
System.out.println("Tiempo en nanosegundos de Busqueda
Ineficiente es "+Time5 );
long Start6 = System.nanoTime();
Math2.BuscarEficiente(t,q);
long End6 = System.nanoTime();
long Time6 = End6-Start6;
System.out.println("Tiempo en nanosegundos de Busqueda
Eficiente es "+Time6 );
break;
}
case 2:{
System.out.println("Ingrese el valor de K");
int K = Integer.parseInt(tld.readLine());
long Start = System.nanoTime();
Math2.BuscaKesimoIneficiente(x,K);
long End = System.nanoTime();
long Time = End-Start;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Ineficiente es "+Time );
long Start2 = System.nanoTime();
Math2.BuscaKesimoEficiente(x,K);
long End2 = System.nanoTime();
long Time2 = End2-Start2;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Eficiente es "+Time2 );
System.out.println("Ingrese La Base Para la
potencia");
int a = Integer.parseInt(tld.readLine());
System.out.println("Ingrese El Exponente Para la
potencia");
int b = Integer.parseInt(tld.readLine());
long Start3 = System.nanoTime();
Math2.PotenciaIneficiente(a,b);
long End3 = System.nanoTime();
long Time3 = End3-Start3;
System.out.println("Tiempo en nanosegundos de Potencia
Ineficinete es "+Time3 );
long Start4 = System.nanoTime();
Math2.PotenciaEficiente(a,b);
long End4 = System.nanoTime();
long Time4 = End4-Start4;
System.out.println("Tiempo en nanosegundos de Potencia
Eficiente es "+Time4 );
System.out.println("Ingrese El Numero a Buscar");
int q=Integer.parseInt(tld.readLine());
int[]t=new int[x.length];
for(int h=0;h<x.length;h++){
t[h]=x[h];
}
Pgina 7 de 10
Math2.quickSort2(t,0,t.length-1);
long Start5 = System.nanoTime();
Math2.BuscarIneficiente(t,q);
long End5 = System.nanoTime();
long Time5 = End5-Start5;
System.out.println("Tiempo en nanosegundos de Busqueda
Ineficiente es "+Time5 );
long Start6 = System.nanoTime();
Math2.BuscarEficiente(t,q);
long End6 = System.nanoTime();
long Time6 = End6-Start6;
System.out.println("Tiempo en nanosegundos de Busqueda
Eficiente es "+Time6 );
break;
}
case 3:{
System.out.println("Ingrese el valor de K");
int K = Integer.parseInt(tld.readLine());
long Start = System.nanoTime();
Math2.BuscaKesimoIneficiente(y,K);
long End = System.nanoTime();
long Time = End-Start;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Ineficiente es "+Time );
long Start2 = System.nanoTime();
Math2.BuscaKesimoEficiente(y,K);
long End2 = System.nanoTime();
long Time2 = End2-Start2;
System.out.println("Tiempo en nanosegundos de Busqueda
del K-esimo Eficiente es "+Time2 );
System.out.println("Ingrese La Base Para la
potencia");
int a = Integer.parseInt(tld.readLine());
System.out.println("Ingrese El Exponente Para la
potencia");
int b = Integer.parseInt(tld.readLine());
long Start3 = System.nanoTime();
Math2.PotenciaIneficiente(a,b);
long End3 = System.nanoTime();
long Time3 = End3-Start3;
System.out.println("Tiempo en nanosegundos de Potencia
Ineficinete es "+Time3 );
long Start4 = System.nanoTime();
Math2.PotenciaEficiente(a,b);
long End4 = System.nanoTime();
long Time4 = End4-Start4;
System.out.println("Tiempo en nanosegundos de Potencia
Eficiente es "+Time4 );
System.out.println("Ingrese El Numero a Buscar");
int q=Integer.parseInt(tld.readLine());
int[]t=new int[y.length];
for(int h=0;h<y.length;h++){
t[h]=y[h];
}
Math2.quickSort2(t,0,t.length-1);
long Start5 = System.nanoTime();
Pgina 8 de 10
Math2.BuscarIneficiente(t,q);
long End5 = System.nanoTime();
long Time5 = End5-Start5;
System.out.println("Tiempo en nanosegundos de Busqueda
Ineficiente es "+Time5 );
long Start6 = System.nanoTime();
Math2.BuscarEficiente(t,q);
long End6 = System.nanoTime();
long Time6 = End6-Start6;
System.out.println("Tiempo en nanosegundos de Busqueda
Eficiente es "+Time6 );
break;
}
}
}catch(IOException e){
System.out.println("No Se Pudo Leer El Dato");
}
}while(l==1);
}
}
CONCLUSIONES
Nota: De este grfico de potencias, podemos evidenciar que en el 100% de los casos el
clculo por el mtodo eficiente, este ltimo predomina por sobre el ineficiente.
Nota: De este grfico podemos sealar que para los distintos arreglos de prueba, siempre
resultan ms factibles las soluciones eficientes.
Pgina 9 de 10
Nota: De este grfico podemos recalcar que existe una gran diferencia entre los resultados
de bsquedas eficientes e ineficientes, logrando un menor tiempo las bsquedas eficientes.
Para finalizar podemos sealar que para calcular el K-simo elemento, en promedio los
resultados eficientes son un 76,78354357% ms rpidos que en la bsqueda ineficiente,
tambin podemos ver que para calcular ab, los algoritmos eficientes son en promedio un
37,97545946% ms rpidos que los ineficientes, y finalmente para la bsqueda del elemento
los algoritmos eficientes son un 87,54056779% ms eficientes que los lineales.
Pgina 10 de 10