一、介绍

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.

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  7. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  8. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)

    一. 1. 2.

随机推荐

  1. Navicat中MySQL server has gone away错误怎么办【转载】

    转载链接:http://www.111cn.net/database/mysql/64073.htm mysql数据库出现MySQL server has gone away错误一般是sql语句太大导 ...

  2. Nginx简介及配置介绍

    一.什么是Nginx Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发 ...

  3. 如何让 PADS Layout 识别到板框

    如何让 PADS Layout 识别到板框 在很久很久以前 PADS Laout 还是 PowerPCB 的时候,铺铜是不认识板框的. 当有铺铜时必须复制一份板框再设置为铺铜才可以. 但到了 PADS ...

  4. untra edit 显示文件函数列表

    UltraEdit的函数列表竟然不显示函数,那这功能要它何用,应该如何才能让函数显示出来呢? 1:先查看一下UE的菜单:视图-->查看方式(语法高亮类型)-->选择相应的语言(我们用的是C ...

  5. MySQL的瑞士军刀(转)

    这里主要讲mysql运维中的一些主要工具,这些工具可能大家都用过,特别是系统管理员或者做linux服务器维护的同学可能都知道这些小工具,这 里讲得会比较多一些,除了系统监控的小工具,还包括一些mysq ...

  6. Azure ARM模式下VNet配置中需要注意的几点事项

    虚拟网络的配置是所有公有云中非常重要的环节.把虚拟网络配置好,对整个系统的管理.维护,以及安全性都非常重要. 本文将介绍Azure在ARM模式下VNet配置中需要特别注意的几点. 一 Azure的VN ...

  7. navicat链接远程数据库

    1.之前使用的是常规的连接方式 学习源头: https://jingyan.baidu.com/article/0aa2237573c1e688cc0d6427.html 这里的ip地址是服务器的ip ...

  8. Python学习笔记 - PostgreSQL的使用

    一.安装PostgreSQL模块 pip install psycopg2 有时候会失败,多安装2次就好了(我是第二次成功了). 二.数据库连接接口 由于Python统一了数据库连接的接口,所以psy ...

  9. [转载]rmmod: can't change directory to '/lib/modules': No such file or directory

    转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...

  10. JDK 8 - java.util.HashMap 实现机制分析

    官方文档对 HashMap 的定义: public class HashMap<K,V> extends AbstractMap<K,V> implements Map< ...