Professional Documents
Culture Documents
21ElementarySorts PDF
21ElementarySorts PDF
Algorithms
F O U R T H
E D I T I O N
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Sorting problem
Ex. Student records in a university.
item
key
Chen
991-878-4944
308 Blair
Rohde
232-343-5555
343 Forbes
Gazsi
766-093-9873
101 Brown
Furia
766-093-9873
101 Brown
Kanaga
898-122-9643
22 Brown
Andrews
664-480-0023
097 Little
Battle
874-088-1212
121 Whitman
664-480-0023
097 Little
Battle
874-088-1212
121 Whitman
Chen
991-878-4944
308 Blair
Furia
766-093-9873
101 Brown
Gazsi
766-093-9873
101 Brown
Kanaga
898-122-9643
22 Brown
Rohde
232-343-5555
343 Forbes
3
% java Experiment 10
0.08614716385210452
0.09054270895414829
0.10708746304898642
0.21166190071646818
0.363292849257276
0.460954145685913
0.5340026311350087
0.7216129793703496
0.9003500354411443
0.9293994908845686
import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}
% java FileSorter .
Insertion.class
Insertion.java
InsertionX.class
InsertionX.java
Selection.class
Selection.java
Shell.class
Shell.java
ShellX.class
ShellX.java
Callbacks
Goal. Sort any type of data.
Q. How can sort() know how to compare data of type Double, String, and
java.io.File
Java: interfaces.
C: function pointers.
C++: class-type functors.
C#: delegates.
Python, Perl, ML, Javascript:
first-class functions.
Callbacks: roadmap
client
object implementation
import java.io.File;
public class FileSorter
{
public static void main(String[] args)
{
File directory = new File(args[0]);
File[] files = directory.listFiles();
Insertion.sort(files);
for (int i = 0; i < files.length; i++)
StdOut.println(files[i].getName());
}
}
sort implementation
public static void sort(Comparable[] a)
{
int N = a.length;
for (int i = 0; i < N; i++)
for (int j = i; j > 0; j--)
if (a[j].compareTo(a[j-1]) < 0)
exch(a, j, j-1);
else break;
}
8
Total order
A total order is a binary relation that satisfies:
Surprising but true. The <= operator for double is not a total order. (!)
9
Comparable API
Implement compareTo() so that v.compareTo(w)
Is a total order.
Returns a negative integer, zero, or positive integer
if v is less than, equal to, or greater than w, respectively.
v
w
less than (return -1)
equal to (return 0)
v
greater than (return +1)
return
return
return
return
return
return
-1;
+1;
-1;
+1;
-1;
+1;
}
11
Exchange. Swap item in array a[] at index i with the one at index j.
12
Testing
Goal. Test if an array is sorted.
Q. If the sorting algorithm passes the test, did it correctly sort the array?
A.
13
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
initial
16
Selection sort
Algorithm. scans from left to right.
Invariants.
in final order
17
in final order
18
19
a[]
4 5 6
9 10
0
1
2
6
4
10
S
A
A
O
O
E
R
R
R
T
T
T
E
E
O
X
X
X
A
S
S
M
M
M
P
P
P
L
L
L
E
E
E
3
4
5
6
7
8
9
10
9
7
7
8
10
8
9
10
A
A
A
A
A
A
A
A
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
T
L
L
L
L
L
L
L
O
O
M
M
M
M
M
M
X
X
X
O
O
O
O
O
S
S
S
S
P
P
P
P
M
M
O
X
X
R
R
R
P
P
P
P
S
S
S
S
L
T
T
T
T
T
T
T
R
R
R
R
R
X
X
X
entries in black
are examined to find
the minimum
entries in red
are a[min]
algorithm position
in final order
not in final order
http://www.sorting-algorithms.com/selection-sort
21
algorithm position
in final order
not in final order
http://www.sorting-algorithms.com/selection-sort
22
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
25
Insertion sort
Algorithm. scans from left to right.
Invariants.
in order
26
in order
in order
28
a[]
4 5 6
9 10
3
4
5
3
0
5
O
E
E
R
O
O
S
R
R
T
S
S
E
T
T
X
X
X
A
A
A
M
M
M
P
P
P
L
L
L
E
E
E
6
7
8
0
2
4
A
A
A
E
E
E
O
M
M
R
O
O
S
R
P
T
S
R
X
T
S
M
X
T
P
P
X
L
L
L
E
E
E
9
10
2
2
A
A
E
E
L
E
M
L
O
M
P
O
R
P
S
R
T
S
X
T
E
X
entries in gray
do not move
entry in red
is a[j]
entries in black
moved one position
right for insertion
29
30
algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
31
32
algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
33
34
algorithm position
in order
not yet seen
http://www.sorting-algorithms.com/insertion-sort
35
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Shellsort overview
Idea. Move entries more than one position at a time by h-sorting the array.
an h-sorted array is h interleaved sorted subsequences
h=4
M
E
P
H
T
S
L
A
S
S
O
E
X
L
h = 13
Shellsort.
P H E L L S
[Shell
P 1959] h-sort
S L E
sequence
S
H
input
of values of h.
L
E
LL L
13-sort P
LL
4-sort
1-sort
h-sorting
How to h-sort an array? Insertion sort, with stride length h.
3-sorting an array
M
E
E
E
A
A
A
A
A
A
O
O
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
E
O
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
P
A
A
A
A
M
M
M
M
M
M
S
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
X
R
R
R
R
R
R
R
R
R
R
T
T
T
T
T
T
T
T
T
T
[stay tuned]
39
1-sort
R
R
R
L
L
T
T
T
T
E
E
E
E
E
E
X
X
X
X
X
A
A
A
A
A
M
S
S
S
S
P
P
P
P
P
L
L
L
R
R
E
E
E
E
T
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
A
A
A
A
M
M
M
M
M
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
R
R
R
R
R
R
R
R
R
T
T
T
T
T
T
T
T
T
7-sort
S
M
M
M
M
O
O
O
O
O
3-sort
M
E
E
E
A
A
A
A
A
O
O
E
E
E
E
E
E
E
A
A
A
A
A
A
A
A
A
A
A
E
E
E
E
E
E
E
E
E
E
E
L
L
L
E
E
E
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
O
O
O
O
O
O
M
M
M
M
M
P
P
P
P
P
P
O
O
O
O
O
M
M
M
M
M
M
P
P
P
P
P
S
S
S
S
S
S
S
S
S
R
R
X
X
X
X
X
X
X
X
X
S
S
R
R
R
R
R
R
R
R
R
X
T
T
T
T
T
T
T
T
T
T
T
X
result
40
Shellsort: intuition
Proposition. A g-sorted array remains g-sorted after h-sorting it.
3-sort
7-sort
S
M
M
M
M
O
O
O
O
O
R
R
R
L
L
T
T
T
T
E
E
E
E
E
E
X
X
X
X
X
A
A
A
A
A
M
S
S
S
S
P
P
P
P
P
L
L
L
R
R
E
E
E
E
T
M
E
E
E
A
A
A
A
A
A
O
O
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
L
L
E
M
M
M
E
E
E
E
E
E
E
E
O
O
O
O
O
O
O
O
X
X
X
X
X
X
P
P
P
P
A
A
A
A
M
M
M
M
M
M
S
S
S
S
S
S
S
S
S
S
P
P
P
P
P
P
X
X
X
X
R
R
R
R
R
R
R
R
R
R
T
T
T
T
T
T
T
T
T
T
still 7-sorted
42
3x+1 increment
sequence
insertion sort
move to next
increment
h = h/3;
}
}
private
{ /* as
private
{ /* as
static
before
static
before
}
43
input
40-sorted
13-sorted
4-sorted
result
44
Shellsort: animation
50 random items
algorithm position
h-sorted
current subsequence
http://www.sorting-algorithms.com/shell-sort
other elements
45
Shellsort: animation
50 partially-sorted items
algorithm position
h-sorted
current subsequence
http://www.sorting-algorithms.com/shell-sort
other elements
46
Shellsort: analysis
Proposition. The worst-case number of compares used by shellsort with
the 3x+1 increments is O(N 3/2).
Property. Number of compares used by shellsort with the 3x+1 increments
is at most by a small multiple of N times the # of increments used.
N
compares
N1.289
2.5 N lg N
5,000
93
58
106
10,000
209
143
230
20,000
467
349
495
40,000
1022
855
1059
80,000
2266
2089
2257
measured in thousands
Useful in practice.
48
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
51
52
Shuffle sort
0.8003
0.9706
0.9157
0.9649
0.1576
0.4854
0.1419
0.4218
0.9572
53
Shuffle sort
0.1419
0.1576
0.4218
0.4854
0.8003
0.9157
0.9572
0.9649
0.9706
54
Shuffle sort
0.1419
0.1576
0.4218
0.4854
0.8003
0.9157
0.9572
0.9649
0.9706
http://www.browserchoice.eu
appeared last
50% of the time
56
browser comparator
(should implement a total order)
57
58
Knuth shuffle
Knuth shuffle
between 0 and i
60
61
for i := 1 to 52 do begin
r := random(51) + 1;
swap := card[r];
card[r] := card[i];
card[i] := swap;
end;
between 1 and 51
Bug 1. Random number r never 52 52nd card can't end up in 52nd place.
Bug 2. Shuffle not uniform (should be between 1 and i).
Bug 3. random() uses 32-bit seed 232 possible shuffles.
Bug 4. Seed = milliseconds since midnight 86.4 million shuffles.
Exploit.
After seeing
5 cards
andis synchronizing
with
clock,
The generation
of random
numbers
too important to be
left server
to chance.
can determine
future cards in real time.
Robert R.allCoveyou
62
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Convex hull
The convex hull of a set of N points is the smallest perimeter fence
enclosing the points.
Equivalent definitions.
Convex hull
The convex hull of a set of N points is the smallest perimeter fence
enclosing the points.
vertex
67
http://www.idlcoyote.com/math_tips/convexhull.html
68
obstacle
70
1
4
8
7
9
3
10
12
11
p
71
p
72
1
4
2
8
7
6
3
10
11
12
0
73
74
Implementing ccw
CCW. Given three points a, b, and c, is a b c a counterclockwise turn?
is c to the left of the ray ab
yes
yes
no
no
no
(-slope)
(collinear)
(collinear)
(collinear)
no
Implementing ccw
CCW. Given three points a, b, and c, is a b c a counterclockwise turn?
ax
ay 1
bx
cx
by 1
cy 1
(bx, by)
=0
>0
(cx, cy)
(bx, by)
(cx, cy)
<0
(ax, ay)
counterclockwise
(ax, ay)
(cx, cy)
clockwise
(ax, ay)
collinear
76
danger of
floating-point
roundoff error
- (b.y-a.y)*(c.x-a.x);
clockwise
counter-clockwise
collinear
}
77
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
insertion sort
shellsort
shuffling
convex hull
Algorithms
Algorithms
F O U R T H
E D I T I O N
insertion sort
shellsort
shuffling
convex hull