算法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
    %s is", N, alg1); 
94         StdOut.printf(" %.1f times faster than %s
", 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.

原文地址:https://www.cnblogs.com/shamgod/p/5420813.html