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

一、介绍

1.

2.

二、代码

1.

 1 package algorithms.elementary21;
 2
 3 /******************************************************************************
 4  *  Compilation:  javac SortCompare.java
 5  *  Execution:    java SortCompare alg1 alg2 N T
 6  *  Dependencies: StdOut.java Stopwatch.java
 7  *
 8  *  Sort N random real numbers, T times using the two
 9  *  algorithms specified on the command line.
10  *
11  *  % java SortCompare Insertion Selection 1000 100
12  *  For 1000 random Doubles
13  *    Insertion is 1.7 times faster than Selection
14  *
15  *  Note: this program is designed to compare two sorting algorithms with
16  *  roughly the same order of growth, e,g., insertion sort vs. selection
17  *  sort or mergesort vs. quicksort. Otherwise, various system effects
18  *  (such as just-in-time compiliation) may have a significant effect.
19  *  One alternative is to execute with "java -Xint", which forces the JVM
20  *  to use interpreted execution mode only.
21  *
22  ******************************************************************************/
23
24 import java.util.Arrays;
25
26 import algorithms.analysis14.Stopwatch;
27 import algorithms.util.StdOut;
28 import algorithms.util.StdRandom;
29
30 public class SortCompare {
31
32     public static double time(String alg, Double[] a) {
33         Stopwatch sw = new Stopwatch();
34         if      (alg.equals("Insertion"))       Insertion.sort(a);
35         else if (alg.equals("InsertionX"))      InsertionX.sort(a);
36         else if (alg.equals("BinaryInsertion")) BinaryInsertion.sort(a);
37         else if (alg.equals("Selection"))       Selection.sort(a);
38         else if (alg.equals("Bubble"))          Bubble.sort(a);
39         else if (alg.equals("Shell"))           Shell.sort(a);
40         else if (alg.equals("Merge"))           Merge.sort(a);
41         else if (alg.equals("MergeX"))          MergeX.sort(a);
42         else if (alg.equals("MergeBU"))         MergeBU.sort(a);
43         else if (alg.equals("Quick"))           Quick.sort(a);
44         else if (alg.equals("Quick3way"))       Quick3way.sort(a);
45         else if (alg.equals("QuickX"))          QuickX.sort(a);
46         else if (alg.equals("Heap"))            Heap.sort(a);
47         else if (alg.equals("System"))          Arrays.sort(a);
48         else throw new IllegalArgumentException("Invalid algorithm: " + alg);
49         return sw.elapsedTime();
50     }
51
52     // Use alg to sort T random arrays of length N.
53     public static double timeRandomInput(String alg, int N, int T)  {
54         double total = 0.0;
55         Double[] a = new Double[N];
56         // Perform one experiment (generate and sort an array).
57         for (int t = 0; t < T; t++) {
58             for (int i = 0; i < N; i++)
59                 a[i] = StdRandom.uniform();
60             total += time(alg, a);
61         }
62         return total;
63     }
64
65     // Use alg to sort T random arrays of length N.
66     public static double timeSortedInput(String alg, int N, int T)  {
67         double total = 0.0;
68         Double[] a = new Double[N];
69         // Perform one experiment (generate and sort an array).
70         for (int t = 0; t < T; t++) {
71             for (int i = 0; i < N; i++)
72                 a[i] = 1.0 * i;
73             total += time(alg, a);
74         }
75         return total;
76     }
77
78     public static void main(String[] args) {
79         String alg1 = args[0];
80         String alg2 = args[1];
81         int N = Integer.parseInt(args[2]);
82         int T = Integer.parseInt(args[3]);
83         double time1, time2;
84         if (args.length == 5 && args[4].equals("sorted")) {
85             time1 = timeSortedInput(alg1, N, T); // Total for alg1.
86             time2 = timeSortedInput(alg2, N, T); // Total for alg2.
87         }
88         else {
89             time1 = timeRandomInput(alg1, N, T); // Total for alg1.
90             time2 = timeRandomInput(alg2, N, T); // Total for alg2.
91         }
92
93         StdOut.printf("For %d random Doubles\n    %s is", N, alg1);
94         StdOut.printf(" %.1f times faster than %s\n", time2/time1, alg2);
95     }
96 } 

2.

 1 package algorithms.elementary21;
 2
 3 import algorithms.util.StdDraw;
 4 import algorithms.util.StdRandom;
 5
 6 /******************************************************************************
 7  *  Compilation:  javac InsertionBars.java
 8  *  Execution:    java InsertionBars N
 9  *  Dependencies: StdDraw.java
10  *
11  *  Insertion sort N random real numbers between 0 and 1, visualizing
12  *  the results by ploting bars with heights proportional to the values.
13  *
14  *  % java InsertionBars 20
15  *
16  ******************************************************************************/
17
18
19 public class InsertionBars {
20     public static void sort(double[] a) {
21         int N = a.length;
22         for (int i = 0; i < N; i++) {
23             int j = i;
24             while (j >= 1 && less(a[j], a[j-1])) {
25                 exch(a, j, j-1);
26                 j--;
27             }
28             show(a, i, j);
29         }
30     }
31
32     private static void show(double[] a, int i, int j) {
33         StdDraw.setYscale(-a.length + i + 1, i);
34         StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
35         for (int k = 0; k < j; k++)
36             StdDraw.line(k, 0, k, a[k]*.6);
37         StdDraw.setPenColor(StdDraw.BOOK_RED);
38         StdDraw.line(j, 0, j, a[j]*.6);
39         StdDraw.setPenColor(StdDraw.BLACK);
40         for (int k = j+1; k <= i; k++)
41             StdDraw.line(k, 0, k, a[k]*.6);
42         StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
43         for (int k = i+1; k < a.length; k++)
44             StdDraw.line(k, 0, k, a[k]*.6);
45     }
46
47     private static boolean less(double v, double w) {
48         return v < w;
49     }
50
51     private static void exch(double[] a, int i, int j) {
52         double t = a[i];
53         a[i] = a[j];
54         a[j] = t;
55     }
56
57     public static void main(String[] args) {
58         int N = Integer.parseInt(args[0]);
59         StdDraw.setCanvasSize(160, 640);
60         StdDraw.setXscale(-1, N+1);
61         StdDraw.setPenRadius(.006);
62         double[] a = new double[N];
63         for (int i = 0; i < N; i++)
64             a[i] = StdRandom.uniform();
65         sort(a);
66     }
67
68 }

3.

 1 package algorithms.elementary21;
 2
 3 import algorithms.util.StdDraw;
 4 import algorithms.util.StdRandom;
 5
 6 /******************************************************************************
 7  *  Compilation:  javac SelectionBars.java
 8  *  Execution:    java SelectionBars N
 9  *  Dependencies: StdDraw.java
10  *
11  *  Selection sort N random real numbers between 0 and 1, visualizing
12  *  the results by ploting bars with heights proportional to the values.
13  *
14  *  % java SelectionBars 20
15  *
16  ******************************************************************************/
17
18 public class SelectionBars {
19
20     public static void sort(double[] a) {
21         int N = a.length;
22         for (int i = 0; i < N; i++) {
23             int min = i;
24             for (int j = i+1; j < N; j++)
25                 if (less(a[j], a[min])) min = j;
26             show(a, i, min);
27             exch(a, i, min);
28         }
29     }
30
31     private static void show(double[] a, int i, int min) {
32         StdDraw.setYscale(-a.length + i + 1, i);
33         StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
34         for (int k = 0; k < i; k++)
35             StdDraw.line(k, 0, k, a[k]*.6);
36         StdDraw.setPenColor(StdDraw.BLACK);
37         for (int k = i; k < a.length; k++)
38             StdDraw.line(k, 0, k, a[k]*.6);
39         StdDraw.setPenColor(StdDraw.BOOK_RED);
40         StdDraw.line(min, 0, min, a[min]*.6);
41     }
42
43     private static boolean less(double v, double w) {
44         return v < w;
45     }
46
47     private static void exch(double[] a, int i, int j) {
48         double t = a[i];
49         a[i] = a[j];
50         a[j] = t;
51     }
52
53     public static void main(String[] args) {
54         int N = Integer.parseInt(args[0]);
55         StdDraw.setCanvasSize(260, 640);
56         StdDraw.setXscale(-1, N+1);
57         StdDraw.setPenRadius(.006);
58         double[] a = new double[N];
59         for (int i = 0; i < N; i++)
60             a[i] = StdRandom.uniform();
61         sort(a);
62     }
63
64 }

4.

时间: 2024-08-07 03:43:11

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

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

1. 1 package algorithms.analysis14; 2 3 import algorithms.util.StdOut; 4 import algorithms.util.StdRandom; 5 6 /****************************************************************************** 7 * Compilation: javac DoublingTest.java 8 * Execution: jav

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

一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac Insertion.java 5 * Execution: java I

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

一. 1. 2. 3. 二.代码 1 package algorithms.mergesort22; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac MergeBU.java 8 * Executio

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

一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursively), and then merge the results. As you will see, one of mergesort’s most attractive properties is that it guarantees to sort any array of N items in t

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

1 package algorithms.elementary21; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac InsertionX.java 8 * Execution: java Inser

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

一. 1. 2.

算法Sedgewick第四版-第1章基础-001递归

一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归,因为递归代码比相应的非递归代码更加简洁优雅.易懂.下面这种实现中的注释就言简意赅地说明了代码的作用.我们可以用数学归纳法证明这段注释所解释的算法的正确性.我们会在 3.1 节中展开这个话题并为二分查找提供一个这样的证明.编写递归代码时最重要的有以下三点.? 递归总有一个最简单的情况——方法的第一条语

算法Sedgewick第四版-第1章基础-008一用数组实现固定长度的栈

1 package algorithms.ADT; 2 3 /****************************************************************************** 4 * Compilation: javac FixedCapacityStackOfStrings.java 5 * Execution: java FixedCapacityStackOfStrings 6 * Dependencies: StdIn.java StdOut.

算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

1. 1 /****************************************************************************** 2 * Compilation: javac ResizingArrayStackWithReflection.java 3 * Execution: java ResizingArrayStackWithReflection < input.txt 4 * Dependencies: StdIn.java StdOut.jav