Skip to content

Instantly share code, notes, and snippets.

@Tlaloc-Es
Created September 20, 2019 13:39
Show Gist options
  • Save Tlaloc-Es/e8f413b668e59413d9b657ad42cf2152 to your computer and use it in GitHub Desktop.
Save Tlaloc-Es/e8f413b668e59413d9b657ad42cf2152 to your computer and use it in GitHub Desktop.
Comparacion
package hackerrang;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Random;
public class Lista {
public static void main(String[] args) {
testArray(100000);
testMonticulo(100000);
}
public static void testArray(int cantidad) {
int[] list = new int[cantidad];
try{
long TInicio, TFin, tiempo;
TInicio = System.currentTimeMillis();
for(int i = 0; i < cantidad; i++) {
list[i] = getRandomNumberInRange(0, 2000000);
Arrays.sort(list);
}
TFin = System.currentTimeMillis();
tiempo = TFin - TInicio;
System.out.println("Tiempo de ejecución en milisegundos de arrays: " + tiempo);
}catch(Exception e){System.out.println(e);
}
}
public static void testMonticulo(int cantidad) {
PriorityQueue pq = new PriorityQueue<Integer>();
int[] list = new int[cantidad];
try{
long TInicio, TFin, tiempo;
TInicio = System.currentTimeMillis();
for(int i = 0; i < cantidad; i++) {
pq.add(i);
}
TFin = System.currentTimeMillis();
tiempo = TFin - TInicio;
System.out.println("Tiempo de ejecución en milisegundos de monticulos: " + tiempo);
}catch(Exception e){System.out.println(e);
}
}
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment