-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortCompare.java
More file actions
43 lines (40 loc) · 1.55 KB
/
Copy pathSortCompare.java
File metadata and controls
43 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Sample usage: java SortCompare Selection Insertion 1000 100
*/
public class SortCompare {
public static double time(String alg, Comparable[] a){
StopWatch timer = new StopWatch();
if (alg.equals("Selection")) Selection.sort(a);
if (alg.equals("Insertion")) Insertion.sort(a);
if (alg.equals("InsertionX")) InsertionX.sort(a);
if (alg.equals("Shell")) Shell.sort(a);
if (alg.equals("Merge")) Merge.sort(a);
if (alg.equals("MergeBU")) MergeBU.sort(a);
if (alg.equals("Quick")) Quick.sort(a);
if (alg.equals("Quick3way")) Quick3way.sort(a);
if (alg.equals("Heap")) Heap.sort(a);
return timer.elapsedTime();
}
public static double timeRandomInput(String alg, int N, int T){
// Use alg to sort T random arrays of length N.
double total = 0.0;
Double[] a = new Double[N];
for (int t=0; t<T; t++){
// Perform one experiment (generate and sort an array).
for (int i=0; i<N; i++)
a[i] = StdRandom.uniform();
total += time(alg, a);
}
return total;
}
public static void main(String[] args){
String alg1 = args[0];
String alg2 = args[1];
int N = Integer.parseInt(args[2]);
int T = Integer.parseInt(args[3]);
double t1 = timeRandomInput(alg1, N, T);
double t2 = timeRandomInput(alg2, N, T);
StdOut.printf("For %d random Doubles\n%s is", N, alg1);
StdOut.printf(" %.1f times faster than %s\n", t2/t1, alg2);
}
}