Question: public class CompareSorts { //code from book public static double time(String alg, int size) { Double[] a = array(size); Stopwatch timer = new Stopwatch(); if
public class CompareSorts {
//code from book public static double time(String alg, int size) {
Double[] a = array(size); Stopwatch timer = new Stopwatch(); if (alg.equals("Insertion")) { Insertion.sort(a); }
if (alg.equals("Selection")) { Selection.sort(a); }
if (alg.equals("Merge")) { Merge.sort(a); }
if (alg.equals("Quick")) { Quick.sort(a); }
if (alg.equals("System")) { Arrays.sort(a); }
return timer.elapsedTime(); } public static Double[] array(int size) { Double[] a = new Double[size]; for (int i = 0; i < size; i++) { a[i] = StdRandom.uniform(); } return a; }
public static double average(String Alg, int size) {
double total = 0.00; for (int t = 1; t < 5; t++) { total += time(Alg, size); } double averageTime = total / 100;
return averageTime; }
public static void Display(String Method) { double prev = average(Method, 1000); for (int i = 1000; i < 50000; i += i) {
double time = average(Method, i); StdOut.printf("%6d %7.7f ", i, time); StdOut.printf("%5.7f ", time / prev); prev = time; } }
/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here StdOut.println(" N Actual Ratio"); // Display("Selection"); //Display("Insertion"); //Display("Merge"); //Display("Quick"); Display("System");
} }
output:
N Actual Ratio 1000 0.0000000 NaN 2000 0.0001600 Infinity 4000 0.0001600 1.0000000 8000 0.0001500 0.9375000 16000 0.0004700 3.1333333 32000 0.0007600 1.6170213
Can anybody help me to fix this code. The assignment is to comapre the time of different sorting method. In the ouput i got NaN. Please help me.Thank you!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
