算法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:    java DoublingTest
 9  *  Dependencies: ThreeSum.java Stopwatch.java StdRandom.java StdOut.java
10  *
11  *  % java DoublingTest
12  *      250   0.0
13  *      500   0.0
14  *     1000   0.1
15  *     2000   0.6
16  *     4000   4.5
17  *     8000  35.7
18  *  ...
19  *
20  ******************************************************************************/
21
22 /**
23  *  The <tt>DoublingTest</tt> class provides a client for measuring
24  *  the running time of a method using a doubling test.
25  *  <p>
26  *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a>
27  *  of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
28  *
29  *  @author Robert Sedgewick
30  *  @author Kevin Wayne
31  */
32 public class DoublingTest {
33     private static final int MAXIMUM_INTEGER = 1000000;
34
35     // This class should not be instantiated.
36     private DoublingTest() { }
37
38     /**
39      * Returns the amount of time to call <tt>ThreeSum.count()</tt> with <em>N</em>
40      * random 6-digit integers.
41      * @param N the number of integers
42      * @return amount of time (in seconds) to call <tt>ThreeSum.count()</tt>
43      *   with <em>N</em> random 6-digit integers
44      */
45     public static double timeTrial(int N) {
46         int[] a = new int[N];
47         for (int i = 0; i < N; i++) {
48             a[i] = StdRandom.uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
49         }
50         Stopwatch timer = new Stopwatch();
51         ThreeSum.count(a);
52         return timer.elapsedTime();
53     }
54
55     /**
56      * Prints table of running times to call <tt>ThreeSum.count()</tt>
57      * for arrays of size 250, 500, 1000, 2000, and so forth.
58      */
59     public static void main(String[] args) {
60         for (int N = 250; true; N += N) {
61             double time = timeTrial(N);
62             StdOut.printf("%7d %5.1f\n", N, time);
63         }
64     }
65 } 

2.

 1 package algorithms.analysis14;
 2
 3 import algorithms.util.StdOut;
 4 import algorithms.util.StdRandom;
 5
 6 /******************************************************************************
 7  *  Compilation:  javac DoublingRatio.java
 8  *  Execution:    java DoublingRatio
 9  *  Dependencies: ThreeSum.java Stopwatch.java StdRandom.java StdOut.java
10  *
11  *
12  *  % java DoublingRatio
13  *      250   0.0    2.7
14  *      500   0.0    4.8
15  *     1000   0.1    6.9
16  *     2000   0.6    7.7
17  *     4000   4.5    8.0
18  *     8000  35.7    8.0
19  *  ...
20  *
21  ******************************************************************************/
22
23 /**
24  *  The <tt>DoublingRatio</tt> class provides a client for measuring
25  *  the running time of a method using a doubling ratio test.
26  *  <p>
27  *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a>
28  *  of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
29  *
30  *  @author Robert Sedgewick
31  *  @author Kevin Wayne
32  */
33 public class DoublingRatio {
34     private static final int MAXIMUM_INTEGER = 1000000;
35
36     // This class should not be instantiated.
37     private DoublingRatio() { }
38
39     /**
40      * Returns the amount of time to call <tt>ThreeSum.count()</tt> with <em>N</em>
41      * random 6-digit integers.
42      * @param N the number of integers
43      * @return amount of time (in seconds) to call <tt>ThreeSum.count()</tt>
44      *   with <em>N</em> random 6-digit integers
45      */
46     public static double timeTrial(int N) {
47         int[] a = new int[N];
48         for (int i = 0; i < N; i++) {
49             a[i] = StdRandom.uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
50         }
51         Stopwatch timer = new Stopwatch();
52         ThreeSum.count(a);
53         return timer.elapsedTime();
54     }
55
56     /**
57      * Prints table of running times to call <tt>ThreeSum.count()</tt>
58      * for arrays of size 250, 500, 1000, 2000, and so forth, along
59      * with ratios of running times between successive array sizes.
60      */
61     public static void main(String[] args) {
62         double prev = timeTrial(125);
63         for (int N = 250; true; N += N) {
64             double time = timeTrial(N);
65             StdOut.printf("%6d %7.1f %5.1f\n", N, time, time/prev);
66             prev = time;
67         }
68     }
69 } 
时间: 2024-10-26 02:34:01

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

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存

1. 2. 3.字符串

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax

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

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-007按位置,找出数组相关最大值

Given an array a[] of N real numbers, design a linear-time algorithm to find the maximum value of a[j] - a[i] where j ≥ i. 1 package algorithms.analysis14; 2 3 public class Best { 4 5 public static void main(String[] args) { 6 double[] a = {5.0, 4.0,

算法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章基础-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章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

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

算法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章基础-1.3Bags, Queues, and Stacks-001可变在小的

1. package algorithms.stacks13; /****************************************************************************** * Compilation: javac ResizingArrayBag.java * Execution: java ResizingArrayBag * Dependencies: StdIn.java StdOut.java * * Bag implementatio