Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Practica n4
Practica n4
Practica N2
Apellidos y Nombre: Arias Surco Mario C.I: 7063375 Docente: Jhonny Felpez Andrade
19 3 - 2013
Problema 1. Utilizando el algoritmo del Insercin obtenga los tiempos de ejecucin f(n), para ordenar n = 10.00.000 elementos del vector, luego para 900.000, 800.000, etc. Tome en cuenta para cada n el f(n) mximo, una estos puntos mediante una lnea. Esta grfica muestra la ejecucin de este algoritmo en el peor caso (si tarda demasiado reduzca los nmeros). Luego obtenga el t(n) del algoritmo. 2. Utilizando el algoritmo de la bsqueda binaria obtenga los tiempos de ejecucin f(n), para n = 10.00.000 elementos del vector, luego para 900.000, 800.000, etc. Tome en cuenta para cada n el f(n) mximo, una estos puntos mediante una lnea. Esta grfica muestra la ejecucin de este algoritmo en el peor caso (si tarda demasiado reduzca los nmeros). Luego obtenga el t(n) del algoritmo.
import java.util.*; public class Tarea4 { public static void main(String[] args) { int n = 1000000; int v[] = new int[n]; for (int i = 1; i < v.length; i++) { Random r = new Random(); int x = r.nextInt(n); v[i] = x; } Insercion(v); BusquedaBinaria(v, 1,n); } public static int BusquedaBinaria(int[] x, int t, int h) { int l = 0, u = x.length - 1, m = 0, p = -1; long tiempoInicio, tiempoFin, tiempoTotal; tiempoInicio = System.currentTimeMillis(); for(int j=1;j<=h;j++){ while (l <= u) { m = (l + u) / 2; if (x[m] < t) l = m + 1; else if (x[m] == t) { p = m; break; } else u = m - 1; } } tiempoFin = System.currentTimeMillis(); tiempoTotal = tiempoFin - tiempoInicio; System.out.println("busqueda binaria " + tiempoTotal); return p; }
static void Insercion(int[] x) { int i, j, temp; int n = x.length; long tiempoInicio, tiempoFin, tiempoTotal; tiempoInicio = System.currentTimeMillis(); for (i = 1; i < n; i++) { temp = x[i]; for (j = i; j > 0 && x[j - 1] > temp; j--) { x[j] = x[j - 1]; } x[j] = temp; } tiempoFin = System.currentTimeMillis(); tiempoTotal = tiempoFin - tiempoInicio; System.out.println("insercion " + tiempoTotal); } }
Grficas Insercin
14000 12000 10000 8000 6000 4000 2000 0 0 20000 40000 60000 80000 100000 120000
f(n) 13265 10578 8734 6391 4500 3110 2016 1110 516 125
n 100000 90000 80000 70000 60000 50000 40000 30000 20000 10000
Bsqueda binaria
350 300 250 200 150 100 50 0 0 200000 400000 600000 800000 1000000 1200000
n 1000000 900000 800000 700000 600000 500000 400000 300000 200000 100000 1000 1