Professional Documents
Culture Documents
Sorting Algorithms: Rules of The Game Shellsort Mergesort Quicksort Animations
Sorting Algorithms: Rules of The Game Shellsort Mergesort Quicksort Animations
Shellsort.
Warmup: easy way to break the N2 barrier.
Embedded systems.
Mergesort.
Java sort for objects.
Perl, Python stable sort.
Quicksort.
Java sort for primitive types.
C qsort, Unix, g++, Visual C++, Python.
Basic terms
Ex: student record in a University.
import java.io.File;
public class Files
{
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++)
System.out.println(files[i]);
}
}
% java Files .
Insertion.class
Insertion.java
InsertionX.class
InsertionX.java
Selection.class
Selection.java
Shell.class
Shell.java
ShellX.class
ShellX.java
index.html
5
Callbacks
Goal. Write robust sorting library method that can sort
any type of data using the data type's natural order.
Callbacks.
Client passes array of objects to sorting routine.
Sorting routine calls back object's comparison function as needed.
Implementing callbacks.
Java: interfaces.
C: function pointers.
C++: functors.
Callbacks
client
import java.io.File;
public class SortFiles
{
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++)
System.out.println(files[i]);
}
}
interface
interface Comparable <Item>
{
public int compareTo(Item);
}
built in to Java
object implementation
public class File
implements Comparable<File>
{
...
public int compareTo(File b)
{
...
return -1;
...
return +1;
...
return 0;
}
}
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]))
exch(a, j, j-1);
else break;
}
Callbacks
Goal. Write robust sorting library that can sort any type of data
into sorted order using the data type's natural order.
Callbacks.
Client passes array of objects to sorting routine.
Sorting routine calls back object's comparison function as needed.
Implementing callbacks.
Java: interfaces.
C: function pointers.
C++: functors.
Consistency.
Implementation must ensure a total order.
if (a < b) and (b < c), then (a < c).
either (a < b) or (b < a) or (a = b).
b)
return
return
return
return
return
return
-1;
+1;
-1;
+1;
-1;
+1;
}
10
unsorted
ee.princeton.edu
cs.princeton.edu
princeton.edu
cnn.com
google.com
apple.com
www.cs.princeton.edu
bolle.cs.princeton.edu
sorted
com.apple
com.cnn
com.google
edu.princeton
edu.princeton.cs
edu.princeton.cs.bolle
edu.princeton.cs.www
edu.princeton.ee
11
Random numbers
public class Experiment
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
Double[] a = new Double[N];
for (int i = 0; i < N; i++)
a[i] = Math.random();
Selection.sort(a);
for (int i = 0; i < N; i++)
System.out.println(a[i]);
}
}
% java Experiment 10
0.08614716385210452
0.09054270895414829
0.10708746304898642
0.21166190071646818
0.363292849257276
0.460954145685913
0.5340026311350087
0.7216129793703496
0.9003500354411443
0.9293994908845686
12
less.
Is v less than w ?
exchange.
insertion sort
14
15
i min
0
1
2
3
4
5
6
7
8
9
10
6
4
10
9
7
7
8
10
8
9
10
0
S
S
A
A
A
A
A
A
A
A
A
A
A
1
O
O
O
E
E
E
E
E
E
E
E
E
E
2
R
R
R
R
E
E
E
E
E
E
E
E
E
Insertion sort
3
T
T
T
T
T
L
L
L
L
L
L
L
L
a[i]
4 5
E X
E X
E X
O X
O X
O X
M X
M O
M O
M O
M O
M O
M O
6
A
A
S
S
S
S
S
S
P
P
P
P
P
7
M
M
M
M
M
M
O
X
X
R
R
R
R
8
P
P
P
P
P
P
P
P
S
S
S
S
S
9 10
L E
L E
L E
L E
L R
T R
T R
T R
T R
T X
T X
T X
T X
1
2
3
4
5
6
7
8
9
10
0
1
3
0
5
0
2
4
2
2
0
S
O
O
O
E
E
A
A
A
A
A
1
O
S
R
R
O
O
E
E
E
E
E
2
R
R
S
S
R
R
O
M
M
L
E
3
T
T
T
T
S
S
R
O
O
M
L
4
E
E
E
E
T
T
S
R
P
O
M
a[i]
5 6
X A
X A
X A
X A
X A
X A
T X
S T
R S
P R
O P
O
7
M
M
M
M
M
M
M
X
T
S
R
8
P
P
P
P
P
P
P
P
X
T
S
9 10
L E
L E
L E
L E
L E
L E
L E
L E
L E
X E
T X
Exception:
Exception:
expensive exchanges
(could be linear)
Bottom line: both are quadratic (too slow) for large randomly ordered files
16
17
a[i]
i
18
Shellsort
Idea: move elements more than one position at a time
by h-sorting the file for a decreasing sequence of values of h
input
7-sort
3-sort
M
M
M
M
O
O
O
O
R
R
L
L
T
T
T
E
E
E
E
E
X
X
X
X
A
A
A
A
S
S
S
S
P
P
P
P
L
L
R
R
E
E
E
T
E
E
E
A
A
A
A
A
O
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
M
M
M
E
E
E
E
E
E
O
O
O
O
O
O
O
X
X
X
X
X
P
P
P
A
A
A
M
M
M
M
M
S
S
S
S
S
S
S
S
P
P
P
P
P
X
X
X
R
R
R
R
R
R
R
R
T
T
T
T
T
T
T
T
a 3-sorted file is
3 interleaved sorted files
1-sort
A
A
A
A
A
A
A
A
A
A
A
A
A
E
E
E
E
E
E
E
E
E
E
E
L
L
E
E
E
E
E
E
E
E
E
E
E
L
L
L
L
L
L
L
L
L
O
O
O
O
O
M
M
M
M
M
M
P
P
P
P
P
O
O
O
O
O
O
M
M
M
M
M
P
P
P
P
P
P
S
S
S
S
S
S
S
S
R
R
R
X
X
X
X
X
X
X
X
S
S
S
R
R
R
R
R
R
R
R
X
T
T
T
T
T
T
T
T
T
T
T
X
X
result A
M
M
R
R
E
E
O
L
S
P
T
X
19
Shellsort
Idea: move elements more than one position at a time
by h-sorting the file for a decreasing sequence of values of h
Use insertion sort, modified to h-sort
magic increment
sequence
big increments:
small subfiles
small increments:
subfiles nearly in order
method of choice for both
small subfiles
subfiles nearly in order
insertion sort!
small increment
21
Analysis of shellsort
Model has not yet been discovered (!)
comparisons
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
22
23
24
plan
M
E
E
A
E
E
E
E
R
G
G
E
G
M
M
E
E
O
O
E
S
R
R
G
O
R
R
L
R
S
S
M
T
T
A
M
E
E
E
O
X
X
E
P
A
A
L
R
M
M
M
R
P
P
P
S
L
L
T
T
E
E
X
X
trace
lo hi
0
2
0
4
6
4
0
8
10
8
12
14
12
8
0
1
3
3
5
7
7
7
9
11
11
13
15
15
15
15
0
M
E
E
E
E
E
E
E
E
E
E
E
E
E
E
A
1
E
M
M
G
G
G
G
E
E
E
E
E
E
E
E
E
2
R
R
G
M
M
M
M
G
G
G
G
G
G
G
G
E
3
G
G
R
R
R
R
R
M
M
M
M
M
M
M
M
E
4
E
E
E
E
E
E
E
O
O
O
O
O
O
O
O
E
5
S
S
S
S
S
S
O
R
R
R
R
R
R
R
R
G
6
O
O
O
O
O
O
R
R
R
R
R
R
R
R
R
L
a[i]
7 8
R T
R T
R T
R T
R T
R T
S T
S T
S E
S E
S A
S A
S A
S A
S A
M M
9 10 11 12 13 14 15
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
T X A M P L E
T A X M P L E
E T X M P L E
E T X M P L E
E T X M P E L
E T X E L M P
E E L M P T X
O P R R S T X
25
Merging
Merging. Combine two pre-sorted lists into a sorted whole.
How to merge efficiently? Use an auxiliary array.
aux[]
k
a[]
copy
merge
}
26
lo
10
m
11
12
13
14
15
hi
16
17
18
19
27
Mergesort analysis
Def. T(N) number of array stores to mergesort an input of size N
= T(N/2)
left half
Mergesort recurrence
T(N/2)
right half
N
merge
T(N) = 2 T(N/2) + N
T(N) ~ N lg N
lg N log2 N
T(N) = 2 T(N/2) + N
+
T(N/2)
T(N/2)
T(N/4)
T(N/4)
T(N/4)
T(N/4)
= N
2(N/2)
= N
...
lg N
2k(N/2k)
T(N / 2k)
= N
...
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
N/2 (2)
= N
N lg N
T(N) = N lg N
30
T(N) = 2 T(N/2) + N
for N > 1, with T(1) = 0
Pf.
T(N) = 2 T(N/2) + N
T(N)/N = 2 T(N/2)/N + 1
given
divide both sides by N
= T(N/2)/(N/2) + 1
algebra
= T(N/4)/(N/4) + 1 + 1
= T(N/8)/(N/8) + 1 + 1 + 1
telescope again
...
= T(N/N)/(N/N) + 1 + 1 +. . .+ 1
= lg N
T(N) = N lg N
31
T(N) = 2 T(N/2) + N
for N > 1, with T(1) = 0
T(2N) =
=
=
=
2 T(N) + 2N
2 N lg N + 2 N
2 N (lg (2N) - 1) + 2N
2 N lg (2N)
given
inductive hypothesis
algebra
QED
Ex. (for COS 340). Extend to show that T(N) ~ N lg N for general N
32
Bottom-up mergesort
Basic plan:
Pass through file, merging to double size of sorted subarrays.
proof 4 that mergesort
Do so for subarray sizes 1, 2, 4, 8, . . . , N/2, N.
uses N lgN compares
lo hi
0
2
4
6
8
10
12
14
0
4
8
12
0
8
0
1
3
5
7
9
11
13
15
3
7
11
15
7
15
15
0
M
E
E
E
E
E
E
E
E
E
E
E
E
E
E
A
1
E
M
M
M
M
M
M
M
M
G
G
E
E
E
E
E
2
R
R
G
G
G
G
G
G
G
M
M
G
G
G
G
E
3
G
G
R
R
R
R
R
R
R
R
R
M
M
M
M
E
4
E
E
E
E
E
E
E
E
E
E
E
O
O
O
O
E
5
S
S
S
S
S
S
S
S
S
S
O
R
R
R
R
G
a[i]
6 7
O R
O R
O R
O R
O R
O R
O R
O R
O R
O R
R S
R S
R S
R S
R S
L M
8
T
T
T
T
T
E
E
E
E
E
E
A
A
A
A
M
9 10 11 12 13 14 15
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
E X A M P L E
T X A M P L E
T A X M P L E
T A X M P L E
T A X M P E L
T A X M P E L
T A X M P E L
E T X M P E L
E T X E L M P
E T X E L M P
E E L M P T X
O P R R S T X
No recursion needed!
33
34
Eliminate the copy to the auxiliary array. Save time (but not space) by
switching the role of the input and auxiliary array in each recursive call.
Mergesort (N log N)
computer
thousand
million
billion
thousand
million
billion
home
instant
2.8 hours
317 years
instant
1 sec
18 min
super
instant
1 second
1.6 weeks
instant
instant
instant
36
37
input
Q
randomize
partition
result
38
39
Quicksort trace
input
randomize
partition
no partition for
subfiles of size 1
0
0
0
0
3
4
6
6
6
6
8
8
11
13
14
14
15
4
1
0
4
4
15
11
9
6
9
8
11
15
15
14
5
2
1
3
12
10
7
9
13
15
0
Q
E
E
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
1
U
R
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
2
I
A
A
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
3
C
T
I
I
I
I
E
E
E
E
E
E
E
E
E
E
E
E
E
4
K
E
E
E
E
E
I
I
I
I
I
I
I
I
I
I
I
I
I
a[i]
5 6
S O
S L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
K L
7
R
P
P
P
P
P
P
P
P
P
M
M
M
M
M
M
M
M
M
8
T
U
U
U
U
U
U
U
O
O
O
O
O
O
O
O
O
O
O
9 10 11 12 13 14 15
E X A M P L E
I M Q C X O K
T M Q R X O S
T M Q R X O S
T M Q R X O S
T M Q R X O S
T M Q R X O S
T M Q R X O S
R M Q S X U T
M Q R S X U T
P Q R S X U T
P Q R S X U T
P Q R S X U T
P Q R S X U T
P Q R S X U T
P Q R S T U X
P Q R S T U X
P Q R S T U X
P Q R S T U X
Quicksort partitioning
Basic plan:
scan from left for an item that belongs on the right
scan from right for item item that belongs on the left
exchange
continue until pointers cross
a[i]
i
9 10 11 12 13 14 15
-1
15
15
scans
12
15
exchange
12
15
15
15
15
15
>= v
<= v
if (i >= j) break;
exch(a, i, j);
swap
}
exch(a, i, r);
return i;
}
<= v
>= v
i
42
43
= C1 = 0 and for N 2:
left
right
partitioning
probability
Simplify:
NCN = (N + 1)CN-1 + 2N
44
dx/x
= 2(N + 1) HN 2(N + 1)
CN 2(N + 1) ln N 1.39 N lg N
45
Random shuffle
probabilistic guarantee against worst case
basis for math model that can be validated with experiments
46
Mergesort (N log N)
computer
thousand
million
billion
thousand
million
billion
home
instant
2.8 hours
317 years
instant
1 sec
18 min
super
instant
1 second
1.6 weeks
instant
instant
instant
Quicksort (N log N)
thousand
million
billion
instant
0.3 sec
6 min
instant
instant
instant
Non-recursive version.
Use explicit stack.
Always sort smaller half first.
48
49
Mergesort animation
done
merge in progress
output
untouched
merge in progress
input
auxiliary array
50
merge in progress
output
last pass
merge in progress
input
auxiliary array
51
Quicksort animation
i
first partition
second partition
done
j
52