Quic ksor t —

advertisement
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Quicksort — Overview
divide-and-conquer principle
example, basic ideas
quicksort algorithm, top–down
Page 1
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Divide–and–conquer principle
problem
Page 2
subsolution
subproblem
J. Maluszynski, IDA, Linköpings Universitet, 2004.
divide
conquer
combine
partition( A[l:r], p )
r
r
p.
[C.A.R. Hoare 1962]
J. Maluszynski, IDA, Linköpings Universitet, 2004.
subs.
subp.
Divide-and-conquer algorithmic design principle:
1. divide a problem into smaller, independent subproblems
2. conquer: solve the subproblems recursively (or directly if trivial)
3. combine the solutions of the subproblems
d&c
subproblem
subsolution
solution
Page 4
p
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
examples: worst and best case
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Al:r )
Quicksort: Basic algorithm (in–place sorting)
procedure quicksort ( array key
analysis of average running time
8
nothing to sort
Page 3
13
5
1
12
r return;
summary
4
0. if l
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
7
>7
Quicksort: Example, basic idea
A
A
l
partition(A,7)
AR
8
<7
7
AL
13
1. select some element of A, e.g. A l ,
as the so–called pivot element:
p Al;
12
>12
12
1
< 12
13
quicksort
quicksort
4
>5
AR
7
8
2. partition A in–place
A
AR
L
into two disjoint subarrays AL, AR:
<p
>p
A
l
m m+1
m partition( A l : r , p );
determines m, l m r, and reorders A l : r , such that
all elements in A l : m are now p and all in A m 1 : r are now
>13
13
< 13
12
5
A
<5
AL
5
>8
4
<8
8
1
>1
7
quicksort
<1
4
1
quicksort
3. apply the algorithm recursively to AL and AR:
quicksort ( A l : m );
sorts AL
quicksort ( A m 1 : r );
sorts AR
j
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Page 6
k if n
if n
Quicksort: Analysis of running time (1)
n elements
AR
k elements n−k elements
m m+1
r
n
sort AR
AL
l
k
Tq
A
A
c
cn
J. Maluszynski, IDA, Linköpings Universitet, 2004.
r
Tq n
Page 5
>p
p
<
p
Tq
sort AL
0
l
>
p
partition A
0, c
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Quicksort: Partitioning an array in–place
A l : r , key p )
int partition ( array key
i
<p
r
for constants c
p);
p);
j
>p
Tq n
k
1
1
Tq n
Worst–case running time:
Tq n
Page 8
Tq k
i
< p
r
c
k n
max1
cn
l
l
j
Tq n
A j;
1
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
the pivot element p is A l
i
l 1;
j
r 1;
while ( true )
j 1 while (A j
do j
do i
i 1 while (A i
if n
if n
1
1
Quicksort: Analysis of running time (3) — Best case
if (i j)
exchange A i
else
return j;
l
time steps
c’ n
c + c’(n−1)
AL
AR
n/2
n/8
n/4
n/8
n/2
n/4
n/8
n/8
J. Maluszynski, IDA, Linköpings Universitet, 2004.
J. Maluszynski, IDA, Linköpings Universitet, 2004.
nc. 1
= c’n
O n log n
=cn
8 c’(n/8) = c’n
4 c’(n/4) = c’n
2 c’(n/2) = c’n
c’ n
time steps
well–balanced recursion tree
n/8
n elements
n/4
n/8
A 2
if pivot element happens to be the median of A in each quicksort call
depth
1
n/4
J. Maluszynski, IDA, Linköpings Universitet, 2004.
5
5
4
4
3
3
5
2
2
4
n/8
#
Page 7
1
1
3
2
3
depth
1
2
2
c + c’(n−2)
c + c’. 2
3
5
.........
4
.........
Cf. Algorithm 11.6 [L/D]
Running time of partition: Θ r
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
n/8
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Quicksort: Analysis of running time (2) — Worst case
1
3
5
4
.......
n−1
4
c+c
Worst case:
log2n
if pivot element happens to be
the minimum or maximum of A
in each quicksort call
(e.g., for presorted arrays)
AL
1, AR
A
in each call
(or vice versa)
n
O n2
cn
n
!
n c
recursion depth is n,
unbalanced recursion tree
∑i
i 2
1
c
c n log n
Best–case timeTq n
!
Worst–case time Tq n
"
sorting nearly–sorted arrays occurs frequently in practice ...
5
#
/
/
0
4
4
4
1
4
#
1
4
4
-
'
&
O n log n
.
n−k
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
0
#
1
/
Page 10
A r in order in these positions
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Lab 4 Introduction
Page 12
Combine:
Quick Sort median-of-three version
to be implemented
with Insertion Sort (given)
Use Quick Sort for data of size m;
otherwise Insertion Sort
Find empirically optimal m.
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Cf . Algorithm 11.7 [L/D]
J. Maluszynski, IDA, Linköpings Universitet, 2004.
3
J. Maluszynski, IDA, Linköpings Universitet, 2004.
1. put A l A l r 2
p
A l r 2
1
#
/
partition(A p)
/
0
in practice better chance for even partition
2. m
..
3
Page 9
k
k
k
Tq n
k: n k
A
1
2
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Improved Quicksort median-of-three
n
Tq k
Tq n
,
∑
1
Tq k
*
k 1
1
+
n
∑
%
k 1
Analysis of average case running time
$
procedure quicksort ( Array A l : r )
)
average case = add execution time of all cases; divide by number of cases
+
n 1
1
n
*
k
"
∑ Tq k
cn
1
n
2
n
"
average time:
Tq n
cn
cn
Tq n
J. Maluszynski, IDA, Linköpings Universitet, 2004.
(
From this equation one can derive p.392 [L/D]
5
"
Page 11
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Median-of-three Quicksort
"
procedure QuickSort table A l r :
put A l A l r 2 A r in order in these positions
if r l 2 then
Al 1
A l r 2
i l 1
i scans from left the elements the pivot
j r
j scans from right the elements the pivot
v Al 1
while i j do
i i 1
while A i v do i i 1
j 1
j
while A j
v do j
j 1
A j
Ai
Ai
A j
undo extra swap
A j
Al 1
place the pivot at its position
QuickSort A l j 1
QuickSort A j 1 r
0
5
5
TDDB57 DALG – Lecture 10: Sorting II: Comparison-based sorting.
Quicksort: Summary
- divide–and–conquer principle
- in–place sorting
- worst–case running time: O n2
- best–case running time: O n log n
Page 13
J. Maluszynski, IDA, Linköpings Universitet, 2004.
- Quicksort, equally distributed data average–case running time: O n log n
Download