package suanfa;
import com.algs4.stdlib.StdOut;
import com.algs4.stdlib.StdRandom;
import com.algs4.stdlib.Stopwatch;
/**
* Created by evan on 16/11/2.
*/
public class SortCompare {
public static double time(String alg,Comparable[] sortList){
Stopwatch timer = new Stopwatch();
if(alg.equals("Insert")){
InsertSorting.sort(sortList);
}
if(alg.equals("Select")){
Selection.sort(sortList);
}
return timer.elapsedTime();
}
public static double timeRandomInput(String alg,int T,int N){
double total = 0.0;
for (int i=0;i<T;i++){
Double[] randomList = new Double[N];
for (int j=0;j<N;j++){
randomList[j] = StdRandom.uniform();
}
total += time(alg,randomList);
}
return total;
}
public static void main(String[] args){
double t1 = timeRandomInput("Insert",10000,1000);
double t2 = timeRandomInput("Select",10000,1000);
StdOut.println(t1);
StdOut.println(t2);
}
}
网友评论