Mergesort and Sorting Stability Explained
Mergesort and Sorting Stability Explained
2 M ERGESORT
‣ mergesort
‣ bottom-up mergesort
‣ sorting complexity
Algorithms
‣ comparators
‣ stability
R OBERT S EDGEWICK | K EVIN W AYNE
[Link]
Stability
76
Stability
76
Stability
76
Stability
A stable sort preserves the relative order of items with equal keys.
76
Stability
1 0 A1 B1 A2 A3 B2
2 1 A1 A2 B1 A3 B2
3 2 A1 A2 A3 B1 B2
4 4 A1 A2 A3 B1 B2
A1 A2 A3 B1 B2
78
Stability: insertion sort
1 0 A1 B1 A2 A3 B2
2 1 A1 A2 B1 A3 B2
3 2 A1 A2 A3 B1 B2
4 4 A1 A2 A3 B1 B2
A1 A2 A3 B1 B2
79
Stability: selection sort
80
Stability: shellsort
1 A1 B2 B3 B4 B1
A1 B2 B3 B4 B1
Pf by counterexample. Long-distance exchanges.
80
Stability: mergesort
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
merge(a, aux, lo, mid, hi);
}
81
Stability: mergesort
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
merge(a, aux, lo, mid, hi);
}
0 1 2 3 4 5 6 7 8 9 10
A1 A2 A3 B D A4 A5 C E F G
82
Stability: mergesort
0 1 2 3 4 5 6 7 8 9 10
A1 A2 A3 B D A4 A5 C E F G
tight code;
shell ✔ N log3 N ? c N 3/2 subquadratic
N log N guarantee;
merge ✔ ½ N lg N N lg N N lg N
stable
improves mergesort
timsort ✔ N N lg N N lg N when preexisting order
83
Algorithms R OBERT S EDGEWICK | K EVIN W AYNE
2.3 Q UICKSORT
‣ quicksort
‣ selection
‣ duplicate keys
Algorithms F O U R T H E D I T I O N
‣ system sorts
[Link]
Two classic sorting algorithms: mergesort and quicksort
...
...
2
2.3 Q UICKSORT
‣ quicksort
‣ selection
‣ duplicate keys
Algorithms
‣ system sorts
[Link]
Quicksort
Basic plan.
input Q U I C K S O R T E X A M P L E
shuffle K R A T E L E P U I M Q C X O S
partitioning item
partition E C A I E K L P U T M Q R X O S
not greater not less
sort left A C E E I K L P U T M Q R X O S
sort right A C E E I K L M O P Q R S T U X
result A C E E I K L M O P Q R S T U X
Quicksort overview
5
Tony Hoare
7
Bob Sedgewick
Implementing ical [2, ll, 13, 21] and analytic [9] studies show that
Quicksort can be expected to be up to twice as fast as its
K R A T E L E P U I M Q C X O S
lo i j
9
Quicksort partitioning demo
E C A I E K L P U T M Q R X O S
lo j hi
partitioned!
10
Quicksort: Java code for partitioning
lo hi i j
lo hi i j lo j hi
12
Quicksort trace
lo j hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
initial values Q U I C K S O R T E X A M P L E
random shuffle K R A T E L E P U I M Q C X O S
0 5 15 E C A I E K L P U T M Q R X O S
0 3 4 E C A E I K L P U T M Q R X O S
0 2 2 A C E E I K L P U T M Q R X O S
0 0 1 A C E E I K L P U T M Q R X O S
1 1 A C E E I K L P U T M Q R X O S
4 4 A C E E I K L P U T M Q R X O S
6 6 15 A C E E I K L P U T M Q R X O S
no partition 7 9 15 A C E E I K L M O P T Q R X U S
for subarrays
of size 1 7 7 8 A C E E I K L M O P T Q R X U S
8 8 A C E E I K L M O P T Q R X U S
10 13 15 A C E E I K L M O P S Q R T U X
10 12 12 A C E E I K L M O P R Q S T U X
10 11 11 A C E E I K L M O P Q R S T U X
10 10 A C E E I K L M O P Q R S T U X
14 14 15 A C E E I K L M O P Q R S T U X
15 15 A C E E I K L M O P Q R S T U X
result A C E E I K L M O P Q R S T U X
13
Quicksort animation
50 random items
algorithm position
in order
current subarray
not in order
[Link]
14
Quicksort: implementation details
15
Quicksort: empirical analysis (1961)
・Algol 60 implementation.
・National-Elliott 405 computer.
16
Quicksort: empirical analysis
computer thousand million billion thousand million billion thousand million billion
317
home instant 2.8 hours instant 1 second 18 min instant 0.6 sec 12 min
years
super instant 1 second 1 week instant instant instant instant instant instant
initial values
random shuffle
18
Quicksort: worst-case analysis
initial values
random shuffle
19
Quicksort: summary of performance characteristics
・Guaranteed to be correct.
・Running time depends on random shuffle.
Average case. Expected number of compares is ~ 1.39 N lg N.
20
Quicksort properties
B1 C1 C2 A1
1 3 B1 C1 C2 A1
1 3 B1 A1 C2 C1
0 1 A1 B1 C2 C1
21
Quicksort: practical improvements
22
Quicksort: practical improvements
Median of sample.
23
2.3 Q UICKSORT
‣ quicksort
‣ selection
‣ duplicate keys
Algorithms
‣ system sorts
[Link]
Selection
Applications.
・Order statistics.
・Find the "top k."
Use theory as a guide.
・Easy N log N upper bound. How?
・Easy N upper bound for k = 1, 2, 3. How?
・Easy N lower bound. Why?
Which is true?
・N log N lower bound? is selection as hard as sorting?
25
Quick-select
k) v
before
public static Comparable select(Comparable[] a, int
{ lo if a[k] is here if a[k] is here
hi
[Link](a);
set hi to j-1 set lo to j+1
int lo = 0, hi = [Link] - 1; during v " v !v
while (hi > lo)
{ i j
26
Quick-select: mathematical analysis
Pf sketch.
27
Theoretical context for selection
bY .
Manuel Blum, Robert W. Floyd, Vaughan Watt,
Ronald L. Rive&, and Robert E. Tarjan
Abstract
% javac [Link]
Note: [Link] uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Q. How to fix?
29
Generic methods
private static <Key extends Comparable<Key>> int partition(Key[] a, int lo, int hi)
{ /* as before */ }
[Link]
[Link]
Duplicate keys
32
Duplicate keys
S T O P O N E Q U A L K E Y S
33
Partitioning an array with all equal keys
35
Duplicate keys: the problem
B A A B A B C C B C B A A A A A A A A A A A
B A A B A B B B C C C A A A A A A A A A A A
A A A B B B B B C C C A A A A A A A A A A A
36
3-way partitioning
lo hi
before v =v >v
during <v
lo hi
lt i gt
during <v =v >v
after <v =v >v
lt i gt
lo lt gt hi
after <v =v >v
3-way partitioning
lo lt gt hi
3-way partitioning
Dutch national flag problem. [Edsger Dijkstra]
tight code;
shell ✔ N log3 N ? c N 3/2 subquadratic
N log N guarantee;
merge ✔ ½ N lg N N lg N N lg N
stable
improves mergesort
timsort ✔ N N lg N N lg N when preexisting order
improves quicksort
3-way quick ✔ N 2 N ln N ½N2 when duplicate keys
44