一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归,因为递归代码比相应的非递归代码更加简洁优雅.易懂.下面这种实现中的注释就言简意赅地说明了代码的作用.我们可以用数学归纳法证明这段注释所解释的算法的正确性.我们会在 3.1 节中展开这个话题并为二分查找提供一个这样的证明.编写递归代码时最重要的有以下三点.‰ 递归总有一个最简单的情况——方法的第一条语…
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest item on onepass through the array does not give much information about where the smallest itemmight be on the next pass. This property can be disadvant…
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac MergeBU.java * Execution: java MergeBU…
一. 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…
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac InsertionX.java * Execution: java InsertionX < input.tx…
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为插入排序元素只能一位一位地交换.基于插入排序的这个缺点,希尔排序是以一个区间来的交换元素,然后不断缩小区间,直到为1.它的理论是,每次经过一个区间的排序后,元素就会更有序些,所以下一个区间排序时,要交换的元素次数会变少. 2.详细的数据排序交换过程 3. Proposition. The numbe…
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementary21; /****************************************************************************** * Compilation: javac Insertion.java * Execution: java Insertion <…
1. package algorithms.stacks13; /****************************************************************************** * Compilation: javac ResizingArrayBag.java * Execution: java ResizingArrayBag * Dependencies: StdIn.java StdOut.java * * Bag implementatio…
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /****************************************************************************** * Compilation: javac DoublingTest.java * Execution: java DoublingTest *…
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac TwoSum.java * Execution: java TwoSum input.txt * Dependen…
1. /****************************************************************************** * Compilation: javac ResizingArrayStackWithReflection.java * Execution: java ResizingArrayStackWithReflection < input.txt * Dependencies: StdIn.java StdOut.java * Data…
1. package algorithms.ADT; /****************************************************************************** * Compilation: javac Bag.java * Execution: java Bag < input.txt * Dependencies: StdIn.java StdOut.java * * A generic bag or multiset, implement…
package algorithms.ADT; /****************************************************************************** * Compilation: javac FixedCapacityStackOfStrings.java * Execution: java FixedCapacityStackOfStrings * Dependencies: StdIn.java StdOut.java * * Sta…
1. package algorithms.util; /****************************************************************************** * Compilation: javac Out.java * Execution: java Out * Dependencies: none * * Writes data of various types to: stdout, file, or socket. * *****…
1. package algorithms.util; /****************************************************************************** * Compilation: javac In.java * Execution: java In (basic test --- see source for required files) * Dependencies: none * * Reads in data of var…
1. package ADT; /****************************************************************************** * Compilation: javac Transaction.java * Execution: java Transaction * Dependencies: StdOut.java * * Data type for commercial transactions. * *************…
1. package ADT; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac Date.java * Execution: java Date * Dependencies: StdOut.java * * An immutable data type for dates. * *…
1. //Euclid’s algorithm public static int gcd(int p, int q) { if (q == 0) return p; int r = p % q; return gcd(q, r); } public static boolean isPrime(int N) { if (N < 2) return false; for (int i = 2; i * i <= N; i++) if (N % i == 0) return false; ret…
一. 1.特点 (1)The quicksort algorithm’s desirable features are that it is in-place (uses only a small auxiliary stack) and thatit requires time proportional to N log N on the average to sort an array of length N. (2)quicksort has a shorter inner loop th…
一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /****************************************************************************** * Compilation: javac SortCompare.java * Execution: java SortCompare alg1 alg2 N T * Dependencies: StdOut.java Stopwatc…
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. package algorithms.analysis14; public class Best { public static void main(String[] args) { double[] a = {5.0, 4.0, 3.0 ,6.0,1.…
package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /****************************************************************************** * Compilation: javac BitonicMax.java * Execution: java BitonicMax N * Depe…
For many programs, developing a mathematical model of running timereduces to the following steps:■Develop an input model, including a definition of the problem size.■ Identify the inner loop.■ Define a cost model that includes operations in the inner…
package algorithms.util; /****************************************************************************** * Compilation: javac Directory.java * Execution: java Directory directory-name * Dependencies: Queue.java StdOut.java * * Prints out all of the f…
/****************************************************************************** * Compilation: javac MM1Queue.java * Execution: java MM1Queue lambda mu * Dependencies: Queue.java Histogram.java * * Simulate an M/M/1 queue where arrivals and departure…
Multi-word search. Program MultiwordSearch.java reads a sequence of query words q[1], ..., q[k] from the command line and a sequence of documents words d[1], ..., d[N] from standard input and finds the shortest interval in which the k words appear in…