This is java. Please use Use MaxPQ from the algs4.jar to build this (1) Write a
ID: 3598889 • Letter: T
Question
This is java. Please use Use MaxPQ from the algs4.jar to build this
(1) Write a performance driver client program, PQtester.java, that uses insert to add N elements to a priority queue, then uses remove the maximum to remove half the keys, then uses insert to fill it up again, then uses remove the maximum to remove all the keys. Use MaxPQ from the algs4.jar.
Write your driver to do this multiple times on random sequences of keys, with N varying from small to large. Do a few runs for each value of N, so you can compute an average, and get a sense of reproducibility. Your driver should measure the time taken for each run, and print-out or plot the average running times. I suggest using System.nanoTime() to capture runtime.
Show your table of run times in the README. What are reasonable values of "small" and "large" values of N?
Explanation / Answer
import java.util.Random;
public class PQtester{
public static void main(String[] args) {
MaxPQ<Integer> pq = new MaxPQ<Integer>();
int testCount = 5; // 5 values for N
long time;
long totalTime = 0;
int N = 10;
while(testCount>0){
long start = System.nanoTime(); // get start time
int i = 0;
Random rand = new Random(); //Get random value to insert
while (i<N) {
int item = rand.nextInt(100);
pq.insert(item);
i++;
}
i =0;
while (i<N/2) {
pq.delMax();
i++;
}
long end = System.nanoTime(); //get end time
time = end-start; //Calculate time
totalTime = totalTime+time;
System.out.println(" N ="+N);
System.out.println("time ="+time+" Neno Second");
N = N*10;
testCount--;
}
System.out.println("Average Time ="+totalTime/5+" Neno Second"); // Printing average time
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.