1. A total order is a binary relation ≤ that satisfies
a) Antisymmetry: if v ≤ w and w ≤ v, then v = w.
b) Transitivity: if v ≤ w and w ≤ x, then v ≤ x.
c) Totality: either v ≤ w or w ≤ v or both.
2. Comparable API: Implement compareTo() so that v.compareTo(w)
a) Is a total order.
b) Returns a negative integer, zero, or positive integer if v is less than, equal to, or greater than w, respectively.
c) Throws an exception if incompatible types (or either is null).
3. Built-in comparable types. Integer, Double, String, Date, File, ...
4. Test if an array is sorted:
private static boolean isSorted(Comparable[] a) { for (int i = 1; i < a.length; i++) if (less(a[i], a[i-1])) return false; return true; }
If the sorting algorithm passes the test, it doesn't mean that it correctly sort the array, we need to check whether each elements in the original array is still in the sorted array. ( i.e. the algorithm can return an arry with all zero.
5. Selection sort :
a) In iteration i, find index min of the smallest entry in the remaining ones.
b) Swap a[i] and a[min].
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; exch(a, i, min); }
6. Selection sort uses (N – 1) + (N – 2) + ... + 1 + 0 ~ N^2 / 2 compares and N exchanges. Running time is insensitive to input. Quadratic time, even if input is sorted. Data movement is minimal. Linear number of exchanges.
7. Insertion sort :
a) In iteration i, swap a[i] with each larger entry to its left.
int N = a.length; for (int i = 0; i < N; i++) for (int j = i; j > 0; j--) if (less(a[j], a[j-1])) exch(a, j, j-1); else break;
8. Proposition: To sort a randomly-ordered array with distinct keys, insertion sort uses ~ ¼ N^2 compares and ~ ¼ N^2 exchanges on average. Pf: Expect each entry to move halfway back.
9. An inversion is a pair of keys that are out of order. An array is partially sorted if the number of inversions is ≤ c N.
a) Ex 1. A subarray of size 10 appended to a sorted subarray of size N.
b) Ex 2. An array of size N with only 10 entries out of place.
10. Proposition: For partially-sorted arrays, insertion sort runs in linear time. Pf: Number of exchanges equals the number of inversions.
11. Shell sort : h-sort array for decreasing sequence of values of h.
a) Big increments ⇒ small subarray.
b) Small increments ⇒ nearly in order.
12. A g-sorted array remains g-sorted after h-sorting it.
int N = a.length; int h = 1; while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, ... while (h >= 1) { // h-sort the array. for (int i = h; i < N; i++) { for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) exch(a, j, j-h); } h = h/3; }
13. Shuffle sort :
a) Generate a random real number for each array entry.
b) Sort the array.
Shuffle sort produces a uniformly random permutation of the input array, provided no duplicate values.
14. Knuth shuffle :
a) In iteration i, pick integer r between 0 and i uniformly at random.
b) Swap a[i] and a[r].
Knuth shuffling algorithm produces a uniformly random permutation of the input array in linear time.
int N = a.length; for (int i = 0; i < N; i++) { int r = StdRandom.uniform(i + 1); exch(a, i, r); }
15. The convex hull of a set of N points is the smallest perimeter fence enclosing the points. Equivalent definitions:
a) Smallest convex set containing all the points.
b) Smallest area convex polygon enclosing the points.
c) Convex polygon enclosing the points, whose vertices are points in set.
Convex hull output: Sequence of vertices in counterclockwise order.
16. Convex hull application:
a) Find shortest path in the plane from s to t that avoids a polygonal obstacle. Shortest path is either straight line from s to t or it is one of two polygonal chains of convex hull.
b) Given N points in the plane, find a pair of points with the largest Euclidean distance between them. Farthest pair of points are extreme points on convex hull.
17. Fact of convex hull :
a) Can traverse the convex hull by making only counterclockwise turns.
b) The vertices of convex hull appear in increasing order of polar angle with respect to point p with lowest y-coordinate.
18. Graham scan :
a) Choose point p with smallest y-coordinate.
b) Sort points by polar angle with p.
c) Consider points in order; discard unless it create a ccw turn.
19. Given three points a, b, and c, is a → b → c a counterclockwise turn?
a) Determinant (or cross product) gives 2x signed area of planar triangle.
b) If signed area > 0, then a → b → c is counterclockwise.
c) If signed area < 0, then a → b → c is clockwise.
d) If signed area = 0, then a → b → c are collinear.
20. Polar order:
A ccw-based solution.
a) If q1 is above p and q2 is below p, then q1 makes smaller polar angle.
b) If q1 is below p and q2 is above p, then q1 makes larger polar angle.
c) Otherwise, ccw(p, q1, q2) identifies which of q1 or q2 makes larger polar angle.
private class PolarOrder implements Comparator<Point2D> { public int compare(Point2D q1, Point2D q2) { double dx1 = q1.x - x; double dy1 = q1.y - y; if (dy1 == 0 && dy2 == 0) { ... } else if (dy1 >= 0 && dy2 < 0) return -1; else if (dy2 >= 0 && dy1 < 0) return +1; else return -ccw(Point2D.this, q1, q2); } }
相关推荐
2.1 Elementary Sorts 244 2.2 Mergesort 270 2.3 Quicksort 288 2.4 Priority Queues 308 2.5 Applications 336 3 Searching . . . . . . . . . . . . . . . . . . . . . . 361 3.1 Symbol Tables 362 3.2 Binary ...
【创建elementary OS安装器】 elementary OS是一款基于Ubuntu的Linux发行版,以其美观的界面和易用性受到许多用户的喜爱。要创建一个elementary OS的安装器,我们需要了解一些基本概念和技术,包括ISO映像、USB驱动...
本书《Elementary Linear Algebra with Applications》是Howard Anton编写的《Elementary Linear Algebra》第九版的扩展版本。全书共十一章,前十个章节的内容与原版书完全一致,而第十一个章节则由21个线性代数的...
由于给定的文件内容主要是关于《Elementary Differential Geometry.pdf》这本电子书的相关版权信息、出版信息、以及封面上的一些设计说明,这与书中的实际内容关联不大,且根据您的要求,我们需要提取出关于微分几何...
Elementary Differential Geometry presents the main results in the differential geometry of curves and surfaces suitable for a first course on the subject. Prerequisites are kept to an absolute minimum...
《Wavelet Theory:An Elementary Approach with Applications》这本书旨在为读者提供一个从小波理论基础知识到实际应用的全面介绍。下面将基于给定的信息,详细介绍该书中的关键知识点。 #### 二、小波理论的基本...
elementaryos-beta1-i386.20121114.iso.torrent
非常好的讲初等函数实现的书,里面对各个方面的算法做了survey。给出了详细的实现说明。 如果你想自己实现浮点库函数的话,这本书非常有帮助。 2006年第2版。 作者:Jean-Michel Muller
"elementary-1.12.2.tar.gz" 是一个软件发行版本的压缩包,它采用了常见的GNU/Linux软件打包格式。这个文件名暗示了我们正在处理的是 elementary OS 的一个特定版本,这里是1.12.2。elementary OS 是一个基于Ubuntu...
《Elementary Analysis》是由Kenneth A. Ross所著的数学分析教材,其完整标题为《Elementary Analysis: The Theory of Calculus, Second Edition》,它主要面向北美大学三年级和四年级的本科数学专业学生。这本书是...
"Elementary Row Operations" Elementary Row Operations是解决线性系统的一种方法,通过使用一系列简单、可逆的操作来解决线性系统。这些操作可以将线性系统转换为一个更简单的形式,从而易于求解。 在了解...
《Molecular Dynamics Simulation: Elementary Methods》很可能是关于这一主题的一本详细教材或研究资料,以.djvu格式存储在名为"Molecular dynamics simulation Elementary Methods.djvu"的压缩文件中。 分子动力...
Elementary Methods in Number Theory 信息编码的教材,非常经典的一本书。适合信息与计算科学专业及计算机等专业学习。
根据给定文件信息,本节内容涉及数学领域的基本分析学教材《Elementary Analysis 2nd Ed》,由Kenneth A. Ross撰写,并与Jorge M. López合作。这本书是专门为大学本科三年级和四年级的数学专业的学生所编写的,内容...
ElementaryOS(Ubuntu系统)常用的一些命令,安装常用软件方法(微信、Chrome、输入法...),以及常用Linux的一些命令
### Elementary Real Analysis #### 一、书籍概述与适用人群 《Elementary Real Analysis》是一本由Brian S. Thomson、Judith B. Bruckner及Andrew M. Bruckner合著的基础实分析教材,最初于2001年由Prentice Hall...
从给定的文件信息中,我们可以提炼出关于《Elementary Differential Equations and BVP, 9th ed》一书的知识点,以及与WileyPLUS教学平台相关的教学和学习资源内容。 首先,《Elementary Differential Equations ...
书名:Elementary Linear Algebra with Applications (9th Edition) 作者:Bernard Kolman , David Hill Hardcover: 720 pages Publisher: Prentice Hall; 9 edition (May 13, 2007) Language: English 这...