一、介绍

1.

2.

二、代码

1.

 package algorithms.elementary21;

 /******************************************************************************
* Compilation: javac SortCompare.java
* Execution: java SortCompare alg1 alg2 N T
* Dependencies: StdOut.java Stopwatch.java
*
* Sort N random real numbers, T times using the two
* algorithms specified on the command line.
*
* % java SortCompare Insertion Selection 1000 100
* For 1000 random Doubles
* Insertion is 1.7 times faster than Selection
*
* Note: this program is designed to compare two sorting algorithms with
* roughly the same order of growth, e,g., insertion sort vs. selection
* sort or mergesort vs. quicksort. Otherwise, various system effects
* (such as just-in-time compiliation) may have a significant effect.
* One alternative is to execute with "java -Xint", which forces the JVM
* to use interpreted execution mode only.
*
******************************************************************************/ import java.util.Arrays; import algorithms.analysis14.Stopwatch;
import algorithms.util.StdOut;
import algorithms.util.StdRandom; public class SortCompare { public static double time(String alg, Double[] a) {
Stopwatch sw = new Stopwatch();
if (alg.equals("Insertion")) Insertion.sort(a);
else if (alg.equals("InsertionX")) InsertionX.sort(a);
else if (alg.equals("BinaryInsertion")) BinaryInsertion.sort(a);
else if (alg.equals("Selection")) Selection.sort(a);
else if (alg.equals("Bubble")) Bubble.sort(a);
else if (alg.equals("Shell")) Shell.sort(a);
else if (alg.equals("Merge")) Merge.sort(a);
else if (alg.equals("MergeX")) MergeX.sort(a);
else if (alg.equals("MergeBU")) MergeBU.sort(a);
else if (alg.equals("Quick")) Quick.sort(a);
else if (alg.equals("Quick3way")) Quick3way.sort(a);
else if (alg.equals("QuickX")) QuickX.sort(a);
else if (alg.equals("Heap")) Heap.sort(a);
else if (alg.equals("System")) Arrays.sort(a);
else throw new IllegalArgumentException("Invalid algorithm: " + alg);
return sw.elapsedTime();
} // Use alg to sort T random arrays of length N.
public static double timeRandomInput(String alg, int N, int T) {
double total = 0.0;
Double[] a = new Double[N];
// Perform one experiment (generate and sort an array).
for (int t = 0; t < T; t++) {
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
total += time(alg, a);
}
return total;
} // Use alg to sort T random arrays of length N.
public static double timeSortedInput(String alg, int N, int T) {
double total = 0.0;
Double[] a = new Double[N];
// Perform one experiment (generate and sort an array).
for (int t = 0; t < T; t++) {
for (int i = 0; i < N; i++)
a[i] = 1.0 * i;
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 time1, time2;
if (args.length == 5 && args[4].equals("sorted")) {
time1 = timeSortedInput(alg1, N, T); // Total for alg1.
time2 = timeSortedInput(alg2, N, T); // Total for alg2.
}
else {
time1 = timeRandomInput(alg1, N, T); // Total for alg1.
time2 = timeRandomInput(alg2, N, T); // Total for alg2.
} StdOut.printf("For %d random Doubles\n %s is", N, alg1);
StdOut.printf(" %.1f times faster than %s\n", time2/time1, alg2);
}
}

2.

 package algorithms.elementary21;

 import algorithms.util.StdDraw;
import algorithms.util.StdRandom; /******************************************************************************
* Compilation: javac InsertionBars.java
* Execution: java InsertionBars N
* Dependencies: StdDraw.java
*
* Insertion sort N random real numbers between 0 and 1, visualizing
* the results by ploting bars with heights proportional to the values.
*
* % java InsertionBars 20
*
******************************************************************************/ public class InsertionBars {
public static void sort(double[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int j = i;
while (j >= 1 && less(a[j], a[j-1])) {
exch(a, j, j-1);
j--;
}
show(a, i, j);
}
} private static void show(double[] a, int i, int j) {
StdDraw.setYscale(-a.length + i + 1, i);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = 0; k < j; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.line(j, 0, j, a[j]*.6);
StdDraw.setPenColor(StdDraw.BLACK);
for (int k = j+1; k <= i; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = i+1; k < a.length; k++)
StdDraw.line(k, 0, k, a[k]*.6);
} private static boolean less(double v, double w) {
return v < w;
} private static void exch(double[] a, int i, int j) {
double t = a[i];
a[i] = a[j];
a[j] = t;
} public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
StdDraw.setCanvasSize(160, 640);
StdDraw.setXscale(-1, N+1);
StdDraw.setPenRadius(.006);
double[] a = new double[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
sort(a);
} }

3.

 package algorithms.elementary21;

 import algorithms.util.StdDraw;
import algorithms.util.StdRandom; /******************************************************************************
* Compilation: javac SelectionBars.java
* Execution: java SelectionBars N
* Dependencies: StdDraw.java
*
* Selection sort N random real numbers between 0 and 1, visualizing
* the results by ploting bars with heights proportional to the values.
*
* % java SelectionBars 20
*
******************************************************************************/ public class SelectionBars { public static void sort(double[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
show(a, i, min);
exch(a, i, min);
}
} private static void show(double[] a, int i, int min) {
StdDraw.setYscale(-a.length + i + 1, i);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = 0; k < i; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BLACK);
for (int k = i; k < a.length; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.line(min, 0, min, a[min]*.6);
} private static boolean less(double v, double w) {
return v < w;
} private static void exch(double[] a, int i, int j) {
double t = a[i];
a[i] = a[j];
a[j] = t;
} public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
StdDraw.setCanvasSize(260, 640);
StdDraw.setXscale(-1, N+1);
StdDraw.setPenRadius(.006);
double[] a = new double[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
sort(a);
} }

4.

最新文章

  1. Win8+VMware12+CentOS7网络设置
  2. Java Web基础:JSP基础概念
  3. boost-asio-cpp-network-programming阅读笔记
  4. spring jdbcTemplate query
  5. JVM参数配置的线上教训
  6. JAVA与数据库开发(JDBC-ODBC、SQL Server、MySQL)
  7. java的动态绑定与双分派(规避instanceof)
  8. (原)Mac下Apache添加限制IP线程模块:mod_limitipconn.so
  9. ThinkPHP 3.1.2 模板的使用技巧
  10. C++ 编译报错discards qualifiers [-fpermissive]
  11. LR11 scan correlation 卡死解决方案
  12. Yii2之ListView小部件
  13. 2016移动端Android新技术综合预览--好文不多,这一篇就足够
  14. jvm GC
  15. 基于python的种子搜索网站,你懂得!
  16. f5到底刷新了点什么,你知道吗
  17. python-代理模式
  18. Android开发——Android中常见的4种线程池(保证你能看懂并理解)
  19. vs2013(vs2015) 打开vs2010 找不到此项目类型所基于的应用程序 MVC2 升级 MVC5 不能加载Web项目
  20. robot framework学习笔记1之_环境安装(win7)

热门文章

  1. 关于js序列化时间的方法
  2. (转)android webview用法小结
  3. LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)
  4. memcache应对缓存失效问题
  5. mysql笔记1—安装、配置和基础的数据表操作
  6. 利用python进行数据分析—数据清洗记录3,map,apply,
  7. 团队队列(列和map结合的经典运用)
  8. js1
  9. UI及物体渲染顺序
  10. C# XML反序列化与序列化举例:XmlSerializer