Documente Academic
Documente Profesional
Documente Cultură
2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
th
century
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
Mergesort
Basic plan.
merge results
Mergesort overview
lo
a[]
sorted
mid
mid+1
hi
sorted
lo
a[]
hi
sorted
copy
merge
a[k]
a[k]
a[k]
a[k]
=
=
=
=
aux[j++];
aux[i++];
aux[j++];
aux[i++];
lo
aux[]
mid
hi
k
a[]
M
7
Assertions
Assertion. Statement to test assumptions about your program.
// enable assertions
// disable assertions (default)
lo
10
mid
11
12
13
14
hi
15
16
17
18
19
Mergesort: trace
lo
hi
merge(a, 0, 0, 1)
merge(a, 2, 2, 3)
merge(a, 0, 1, 3)
merge(a, 4, 4, 5)
merge(a, 6, 6, 7)
merge(a, 4, 5, 7)
merge(a, 0, 3, 7)
merge(a, 8, 8, 9)
merge(a, 10, 10, 11)
merge(a, 8, 9, 11)
merge(a, 12, 12, 13)
merge(a, 14, 14, 15)
merge(a, 12, 13, 15)
merge(a, 8, 11, 15)
merge(a, 0, 7, 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[]
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
10
Mergesort: animation
50 random items
algorithm position
in order
current subarray
not in order
http://www.sorting-algorithms.com/merge-sort
11
Mergesort: animation
50 reverse-sorted items
algorithm position
in order
current subarray
not in order
http://www.sorting-algorithms.com/merge-sort
12
12
mergesort (N log N)
computer
thousand
million
billion
thousand
million
billion
home
instant
2.8 hours
317 years
instant
1 second
18 min
super
instant
1 second
1 week
instant
instant
instant
right half
merge
14
D (N / 2)
D (N / 2)
D (N / 4)
D (N / 4)
D (N / 4)
D (N / 4)
=N
2 (N/2)
=N
4 (N/4)
=N
...
lg N
D (N / 2k)
2k (N/2k)
=N
...
D (2)
D (2)
D (2)
D (2)
D (2)
D (2)
D (2)
D (2)
N/2 (2)
=N
N lg N
15
D (N) = 2 D (N/2) + N
D (N) / N = 2 D (N/2) / N + 1
given
divide both sides by N
= D (N/2) / (N/2) + 1
algebra
= D (N/4) / (N/4) + 1 + 1
= D (N/8) / (N/8) + 1 + 1 + 1
...
= D (N/N) / (N/N) + 1 + 1 + ... + 1
= lg N
16
Base case: N = 1.
Inductive hypothesis: D (N) = N lg N.
Goal: show that D (2N) = (2N) lg (2N).
D (2N) = 2 D (N) + 2N
given
= 2 N lg N + 2N
inductive hypothesis
= 2 N (lg (2N) 1) + 2N
algebra
= 2 N lg (2N)
QED
17
A
A
C
B
D
C
G
D
H
E
I
F
M
G
N
H
B
M
E
N
F
O
J
P
O
Q
P
R
Q
S
R
T
S
U
T
V
merged result
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo + CUTOFF - 1) Insertion.sort(a, lo, hi);
int mid = lo + (hi - lo) / 2;
sort (a, aux, lo, mid);
sort (a, aux, mid+1, hi);
merge(a, aux, lo, mid, hi);
}
19
B
A
C
B
D
C
E
D
F
E
G
F
H
G
I
H
J
I
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);
if (!less(a[mid+1], a[mid])) return;
merge(a, aux, lo, mid, hi);
}
20
a[j++];
a[i++];
a[j++];
a[i++];
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort (aux, a, lo, mid);
sort (aux, a, mid+1, hi);
merge(aux, a, lo, mid, hi);
}
switch roles of aux[] and a[]
21
Mergesort: visualization
3.2
Mergesort
235
first subarray
second subarray
first merge
result
22
Visual trace of top-down mergesort for with cutoff for small subarrays
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
Bottom-up mergesort
Basic plan.
merge(a,
merge(a,
merge(a,
merge(a,
merge(a,
merge(a,
merge(a,
merge(a,
sz = 2
merge(a,
merge(a,
merge(a,
merge(a,
sz = 4
aux, 0, 0, 1)
aux, 2, 2, 3)
aux, 4, 4, 5)
aux, 6, 6, 7)
aux, 8, 8, 9)
aux, 10, 10, 11)
aux, 12, 12, 13)
aux, 14, 14, 15)
aux, 0, 1, 3)
aux, 4, 5, 7)
aux, 8, 9, 11)
aux, 12, 13, 15)
merge(a, aux,
merge(a, aux,
sz = 8
merge(a, aux,
0, 3, 7)
8, 11, 15)
0,
7, 15)
0
M
E
E
E
E
E
E
E
E
1
E
M
M
M
M
M
M
M
M
2
R
R
G
G
G
G
G
G
G
3
G
G
R
R
R
R
R
R
R
4
E
E
E
E
E
E
E
E
E
a[i]
5 6
S O
S O
S O
S O
S O
S O
S O
S O
S O
E
E
E
E
G
G
G
G
M
M
M
M
R
R
R
R
E
E
E
E
S
O
O
O
O
R
R
R
R
S
S
S
E
E
A
A
T
T
E
E
A
A
T
T
X
X
X
X
M
M
M
E
P
P
P
L
E
E
E
M
L
L
L
P
E
E
E
E
G
G
M
M
O
O
R
R
R
R
S
S
A
A
E
E
T
E
X
L
E
M
L
P
M
T
P
X
7
R
R
R
R
R
R
R
R
R
8
T
T
T
T
T
E
E
E
E
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
16
32
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
Complexity of sorting
Computational complexity. Framework to study efficiency of algorithms
for solving a particular problem X.
Model of computation. Allowable operations.
Cost model. Operation count(s).
Upper bound. Cost guarantee provided by some algorithm for X.
Lower bound. Proven limit on cost guarantee of all algorithms for X.
Optimal algorithm. Algorithm with best possible cost guarantee for X.
lower bound ~ upper bound
Example: sorting.
30
a<b
yes
height of tree =
worst-case number
of compares
no
code between compares
(e.g., sequence of exchanges)
b<c
yes
a<c
no
abc
yes
bac
a<c
yes
acb
no
no
cab
b<c
yes
bca
no
cba
at least N! leaves
32
2 h # leaves N !
h lg ( N ! ) ~ N lg N
Stirling's formula
33
Complexity of sorting
Model of computation. Allowable operations.
Cost model. Operation count(s).
Upper bound. Cost guarantee provided by some algorithm for X.
Lower bound. Proven limit on cost guarantee of all algorithms for X.
Optimal algorithm. Algorithm with best possible cost guarantee for X.
Example: sorting.
34
35
36
2.2 M ERGESORT
mergesort
bottom-up mergesort
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
sorting complexity
comparators
stability
2.2 M ERGESORT
mergesort
bottom-up mergesort
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
sorting complexity
comparators
stability
39
40
natural order
-1;
+1;
-1;
+1;
-1;
+1;
}
41
Comparator interface
Comparator interface: sort using an alternate order.
Natural order.
Case insensitive.
Spanish.
British phone book.
. . .
42
String[] a;
Comparator<String> object
...
Arrays.sort(a);
...
Arrays.sort(a, String.CASE_INSENSITIVE_ORDER);
...
Arrays.sort(a, Collator.getInstance(new Locale("es")));
...
Arrays.sort(a, new BritishPhoneBookOrder());
...
Bottom line. Decouples the definition of the data type from the
definition of what it means to compare two objects of that type.
43
44
Arrays.sort(a, Student.BY_NAME);
Arrays.sort(a, Student.BY_SECTION);
Andrews
664-480-0023
097 Little
Furia
766-093-9873
101 Brown
Battle
874-088-1212
121 Whitman
Rohde
232-343-5555
343 Forbes
Chen
991-878-4944
308 Blair
Andrews
664-480-0023
097 Little
Fox
884-232-5341
11 Dickinson
Chen
991-878-4944
308 Blair
Furia
766-093-9873
101 Brown
Fox
884-232-5341
11 Dickinson
Gazsi
766-093-9873
101 Brown
Kanaga
898-122-9643
22 Brown
Kanaga
898-122-9643
22 Brown
Battle
874-088-1212
121 Whitman
Rohde
232-343-5555
343 Forbes
Gazsi
766-093-9873
101 Brown
46
Polar order
Polar order. Given a point p, order points by polar angle they make with p.
0
1
4
5
p
6
Arrays.sort(points, p.POLAR_ORDER);
Application. Graham scan algorithm for convex hull. [see previous lecture]
High-school trig solution. Compute polar angle w.r.t. p using atan2().
Drawback. Evaluating a trigonometric function is expensive.
47
Polar order
Polar order. Given a point p, order points by polar angle they make with p.
0
1
4
5
p
6
Arrays.sort(points, p.POLAR_ORDER);
A ccw-based solution.
48
p, q1, q2 horizontal
q1 above p; q2 below p
q1 below p; q2 above p
both above or below p
}
49
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
Stability
A typical application. First, sort by name; then sort by section.
Selection.sort(a, Student.BY_NAME);
Selection.sort(a, Student.BY_SECTION);
Andrews
664-480-0023
097 Little
Furia
766-093-9873
101 Brown
Battle
874-088-1212
121 Whitman
Rohde
232-343-5555
343 Forbes
Chen
991-878-4944
308 Blair
Chen
991-878-4944
308 Blair
Fox
884-232-5341
11 Dickinson
Fox
884-232-5341
11 Dickinson
Furia
766-093-9873
101 Brown
Andrews
664-480-0023
097 Little
Gazsi
766-093-9873
101 Brown
Kanaga
898-122-9643
22 Brown
Kanaga
898-122-9643
22 Brown
Gazsi
766-093-9873
101 Brown
Rohde
232-343-5555
343 Forbes
Battle
874-088-1212
121 Whitman
Stability
Q. Which sorts are stable?
A. Insertion sort and mergesort (but not selection sort or shellsort).
sorted by time
Chicago
Phoenix
Houston
Chicago
Houston
Chicago
Seattle
Seattle
Phoenix
Chicago
Chicago
Chicago
Seattle
Seattle
Chicago
Chicago
Seattle
Phoenix
09:00:00
09:00:03
09:00:13
09:00:59
09:01:10
09:03:13
09:10:11
09:10:25
09:14:25
09:19:32
09:19:46
09:21:05
09:22:43
09:22:54
09:25:52
09:35:21
09:36:14
09:37:44
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Houston
Houston
Phoenix
Phoenix
Phoenix
Seattle
Seattle
Seattle
Seattle
Seattle
09:25:52
09:03:13
09:21:05
09:19:46
09:19:32
09:00:00
09:35:21
09:00:59
09:01:10
09:00:13
09:37:44
09:00:03
09:14:25
09:10:25
09:36:14
09:22:43
09:10:11
09:22:54
no
longer
sorted
by time
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Chicago
Houston
Houston
Phoenix
Phoenix
Phoenix
Seattle
Seattle
Seattle
Seattle
Seattle
09:00:00
09:00:59
09:03:13
09:19:32
09:19:46
09:21:05
09:25:52
09:35:21
09:00:13
09:01:10
09:00:03
09:14:25
09:37:44
09:10:11
09:10:25
09:22:43
09:22:54
09:36:14
still
sorted
by time
Note. Need to carefully check code ("less than" vs. "less than or equal to").
53
B1
A1
A2
A3
B2
A1
B1
A2
A3
B2
A1
A2
B1
A3
B2
A1
A2
A3
B1
B2
A1
A2
A3
B1
B2
A1
A2
A3
B1
B2
min
B1
B2
B2
B1
B2
B1
B2
B1
Stability: shellsort
Proposition. Shellsort sort is not stable.
public class Shell
{
public static void sort(Comparable[] a)
{
int N = a.length;
int h = 1;
while (h < N/3) h = 3*h + 1;
while (h >= 1)
{
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;
h
0
1
}
B1 B2
}
}
4
A1 B2
1
B3
B4
A1
B3
B4
B1
A1
B2
B3
B4
B1
A1
B2
B3
B4
B1
56
Stability: mergesort
Proposition. Mergesort is stable.
public class Merge
{
private static Comparable[] aux;
private static void merge(Comparable[] a, int lo, int mid, int hi)
{ /* as before */ }
private static void sort(Comparable[] a, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, lo, mid);
sort(a, mid+1, hi);
merge(a, lo, mid, hi);
}
public static void sort(Comparable[] a)
{ /* as before */ }
}
Stability: mergesort
Proposition. Merge operation is stable.
private static void merge(...)
{
for (int k = lo; k <= hi; k++)
aux[k] = a[k];
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++)
{
if
(i > mid)
else if (j > hi)
else if (less(aux[j], aux[i]))
else
}
a[k]
a[k]
a[k]
a[k]
=
=
=
=
aux[j++];
aux[i++];
aux[j++];
aux[i++];
10
A1
A2
A3
A4
A5
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
comparators
stability