-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortCompare.java
More file actions
29 lines (26 loc) · 944 Bytes
/
Copy pathSortCompare.java
File metadata and controls
29 lines (26 loc) · 944 Bytes
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
import java.util.Random;
public class SortCompare {
public static double time( Stirng alg, Double[] a ) { /* See text. */ }
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] = Random.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 ); // total for alg1
double t2 - timeRandomInput( alg2, N, T ); // total for agl2
System.out.printf( "For %d random Doubles\n %s is", N, alg1);
System.out.printf( " %.1f times faster than %s\n", t2/t1, alg2);
}
}