





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






CS 3343 Analysis of Algorithms
1
10/9/
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 3343 Analysis of Algorithms
2
10/9/
All the sorting algorithms we have seen so farare
comparison sorts
: only use comparisons to
determine the relative order of elements.•
E.g
., insertion sort, merge sort, quicksort,
heapsort.
The best worst-case running time that we’veseen for comparison sorting is
n
log
n
Is O(n log n) the best we can do?
Decision trees
can help us answer this question.
CS 3343 Analysis of Algorithms
3
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
3
a
2
a
1
a
3
a
2
a
1
Each internal node is labeled
a
i
:
a
j
for
i
,
j
∈
{1, 2,…,
n
}.
a
i
≤
a
j
.
a
i
≥
a
j
.
Sort
a
1
a
2
a
n
≤
≤
≤
≤
≤
≥
≥
≥
≥
≥
CS 3343 Analysis of Algorithms
4
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
3
a
2
a
1
a
3
a
2
a
1
Each internal node is labeled
a
i
:
a
j
for
i
,
j
∈
{1, 2,…,
n
}.
a
i
≤
a
j
.
a
i
≥
a
j
.
≤
≤
≤
≤
≤
≥
≥
≥
≥
≥
Sort
a
1
a
2
a
3
CS 3343 Analysis of Algorithms
5
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
3
a
2
a
1
a
3
a
2
a
1
Each internal node is labeled
a
i
:
a
j
for
i
,
j
∈
{1, 2,…,
n
}.
a
i
≤
a
j
.
a
i
≥
a
j
.
≤
≤
≤
≤
≤
≥
≥
≥
≥
Sort
a
1
a
2
a
3
9
≥
4
CS 3343 Analysis of Algorithms
6
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
3
a
2
a
1
a
3
a
2
a
1
Each internal node is labeled
a
i
:
a
j
for
i
,
j
∈
{1, 2,…,
n
}.
a
i
≤
a
j
.
a
i
≥
a
j
.
≤
≤
≤
≤
≤
≥
≥
≥
≥
9
≥
6
Sort
a
1
a
2
a
3
CS 3343 Analysis of Algorithms
7
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
3
a
2
a
1
a
3
a
2
a
1
Each internal node is labeled
a
i
:
a
j
for
i
,
j
∈
{1, 2,…,
n
}.
a
i
≤
a
j
.
a
i
≥
a
j
.
≤
≤
≤
≤
≥
≥
≥
≥
≥
4
≤
6
Sort
a
1
a
2
a
3
CS 3343 Analysis of Algorithms
8
10/9/
a
1
a
2
a
1
a
2
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
2
a
1
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
a
3
a
2
a
3
a
2
a
3
a
2
a
3
a
1
a
2
a
3
a
1
a
3
a
2
a
1
a
3
a
2
a
1
≤
≤
≤
≤
≤
≥
≥
≥
≥
≥
Each leaf contains a permutation
〈π(1)
,
π(2)
,…,
π
(
n
)
〉
to
indicate that the ordering
a
π
(1)
≤
a
π
(2)
≤
L
≤
a
π
(n)
has been
established.
4
≤
6
≤
9
Sort
a
1
a
2
a
3
CS 3343 Analysis of Algorithms
13
10/9/
for
i
to
k
do
i
for
j
to
n
do
j
j
i
] = |{key =
i
for
i
to
k
do
i
i
i
i
] = |{key
i
for
j
n
downto
do
j
j
j
j
1. 2. 3. 4.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
CS 3343 Analysis of Algorithms
15
10/9/
1
2
3
4
5
1
2
3
4
for
i
to
k
do
i
1.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
to
n
do
j
j
i
] = |{key =
i
2.
CS 3343 Analysis of Algorithms
17
10/9/
1
2
3
4
5
1
2
3
4
for
j
to
n
do
j
j
i
] = |{key =
i
2.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
to
n
do
j
j
i
] = |{key =
i
2.
CS 3343 Analysis of Algorithms
19
10/9/
1
2
3
4
5
1
2
3
4
for
j
to
n
do
j
j
i
] = |{key =
i
2.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
to
n
do
j
j
i
] = |{key =
i
2.
CS 3343 Analysis of Algorithms
25
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
27
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
29
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
31
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.
CS 3343 Analysis of Algorithms
10/9/
1
2
3
4
5
1
2
3
4
for
j
n
downto
do
j
j
j
j
4.