if (alg.equals("Insertion")) { Insertion.sort(a); } if (alg.equals("Selection")) { Selection.sort(a); }
return Profiler.end(); }
/** * 使用算法alg将T个长度为N的数组排序 * @param alg 算法 * @param N 数组长度 * @param T T个数组 * @return */ publicstaticdoubletimeRandomInput(String alg,int N, int T){ double total = 0.0; Double[] a = new Double[N]; for (int t = 0; t < T; t++) { //进行一次测试,生成一个数组并排序 for (int i = 0; i < N; i++) { a[i] = uniform(); } total += time(alg,a);
} return total; }
publicstaticvoidmain(String[] args){ String alg1 = args[0]; String alg2 = args[1]; int N = Integer.parseInt(args[2]); int T = Integer.parseInt(args[3]);
System.out.printf("For %d random Doubles\n %s is ",N,alg1); System.out.printf("%.1f times faster than %s\n ",t2/t1,alg2);
}
/** * Returns a random real number uniformly in [0, 1). * * @return a random real number uniformly in [0, 1) */ publicstaticdoubleuniform(){ returnnew Random().nextDouble(); } //程序参数为 Insertion Selection 1000 100 ,运行结果: // For 1000 random Doubles // Insertion is 1.8 times faster than Selection }