Lecture 2 Sorting
description
Transcript of Lecture 2 Sorting
Lecture 2 Sorting
Sorting Problem
.'''such that sequence
input of }',...,','{n permutatioA :Output
}.,...,,{ numbers of sequenceA :Input
21
21
21
n
n
n
aaa
aaa
aaan
Insertion Sort, Merge Sort
6. 5, 4, 3, 2, 1, :Output
3. 1, 6, 4, 2, 5, :Input
e.g.,
Efficiency
• Running time from receiving the input to producing the output.
)log(
)( 2
nnO
nOInsertion Sort
Merge Sort
Running time
Is array a data structure?
Is array a data structure?
• No!
• A data structure is a standard part in construction of algorithms.
• What data structures do you know on array?
Is array a data structure?
• No!
• A data structure is a standard part in construction of algorithms.
• What data structures do you know on array?
• Stack, queue, list, …, heap.
Heapsort
• Heap, a data structure
• Max-Heapify procedure
• Building a heap
• Heapsort
A Data Structure Heap
• A heap is an array object that can be viewed as a nearly complete binary tree.
35
2 4 1
6
6 5 3 2 4 1
1
2 3
4 5 6
;12return
)(Right
;2return
)(Left
;2/return
)(Parent
ii
i
i
i
i
Max-Heap
].[)](Parent[
:property following thesatisfiesroot
theother than nodeevery heap,-max aIn
iAiA
i
Min-Heap
].[)](Parent[
:property following thesatisfiesroot
theother than nodeevery heap,-min aIn
iAiA
i
Max-Heapify
• Max-Heapify(A,i) is a subroutine.
• When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property.
• Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.
4
714
2 18
7
2 8 1
8 7
12
14
4
14
4
if-end
);g,(Heapify-Max
];g[][ exchangebegin then
g if
;g then
]g[][ and ][ if
;g else
g then
][][ and ][ if
);(Right
);(Left
),(Heapify-Max
estlarA
estlarAiA
iestlar
restlar
estlarArAAsizeheapr
iestlar
lestlar
iAlAAsizeheapl
ir
il
iA
Running time
).(lg in time runsHeapify -Max Hence,
][- wherelg
nO
Asizeheapnnheight
.lg Hence,
.2 is,that
,1221 satisfying
integer largest theis Then .Let 1
nh
n
n
hheighth
h
h
Building a Heap
);,(Heapify-Max do
1 downto 2/][for
];[][-
)(Heap-Max-Build
iA
Alengthi
AlengthAsizeheap
A
e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
4
31
16 10
7
2 9
14 8
.2/ is child a has holocation wlast The n
Proof. .on induction by is proof This n
n
2/n
4
31
16 10
7
2 9
14 8
.2/1)/2(n even. is 1. Case nn
7
n 1n
4
31
16 10
7
2 9
14 8
.12/1)/2(n odd. is 2. Case nn
n 1n
4
31
16 10
7
2 9
14 8
.12/1)/2(n odd. is 2. Case nn
n 1n
Building a Max-Heap
);,(Heapify-Max do
1 downto 2/][for
];[][-
)(Heap-Max-Build
iA
Alengthi
AlengthAsizeheap
A
e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
4
31
16 10
7
2 9
14 8
4
31
16 10
7
2 9
14 8
4
31
16 10
7
14 9
2 8
4
31
16 10
7
14 9
2 8
4
1
16 3
7
14 9
2 8
10
4
1
16 3
7
14 9
2 8
10
4
16
1 3
7
14 9
2 8
10
4
16
7 3
1
14 9
2 8
10
16
4
7 3
1
14 9
2 8
10
16
14
7 3
1
4 9
2 8
10
16
14
7 3
1
8 9
2 4
10
Analysis
).( and levelat
nodes ofnumber theis )(# where
)()(# timerunning0
Aheighthi
inode
ihOinodeh
i
iinode
Asizeheapnnh
2)(#
)(- wherelg
16
14
7 3
1
8 9
2 4
10
0i
1i
hi
Running time
).(2
,2)2/11(
2/1
2
because
)()2
)(2()(2)(2
20
000
nO
l
nOih
OihOihO
h
ll
h
iih
hh
i
ih
i
i
22/11
1
2
1
2
1
2
11
2/11
2/1
2/11
2/1
2/11
2/1
2/11
1/2
2
1
2
1
2
1
2
1
2
32
432
43210
k
kk
kk
kk
kl
l
l
.1for
10
q
q
aaq
k
k
Heapsort
for-end
;)1(Heapify-Max
;1][-][-
];[]1[ exchange
begin do
2 downto ][for
);(Heap-Max-Buid
)(Heapsort
A,
AsizeheapAsizeheap
iAA
Alengthi
A
A
16
14
7 3
1
8 9
2 4
10
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.Build a max-heap
16, 14, 10, 8, 7, 9, 3, 2, 4, 1.
1
14
7 3
16
8 9
2 4
10
1
14
7 3
16
8 9
2 4
10
1, 14, 10, 8, 7, 9, 3, 2, 4, 16.
14
8
7 3
16
4 9
2 1
10
14
8
7 3
16
4 9
2 1
10
14, 8, 10, 4, 7, 9, 3, 2, 1, 16.
1
8
7 3
16
4 9
2 14
10
1
8
7 3
16
4 9
2 14
10
1, 8, 10, 4, 7, 9, 3, 2, 14, 16.
10
8
7 3
16
4 1
2 14
9
10
8
7 3
16
4 1
2 14
9
10, 8, 9, 4, 7, 1, 3, 2, 14, 16.
2
8
7 3
16
4 1
10 14
9
2
8
7 3
16
4 1
10 14
9
2, 8, 9, 4, 7, 1, 3, 10, 14, 16.
9
8
7 2
16
4 1
10 14
3
9
8
7 2
16
4 1
10 14
3
9, 8, 3, 4, 7, 1, 2, 10, 14, 16.
2
8
7 9
16
4 1
10 14
3
8
7
2 9
16
4 1
10 14
3
1
7
2 9
16
4 8
10 14
3
7
4
2 9
16
1 8
10 14
3
2
4
7 9
16
1 8
10 14
3
4
2
7 9
16
1 8
10 14
3
1
2
7 9
16
4 8
10 14
3
3
2
7 9
16
4 8
10 14
1
1
2
7 9
16
4 8
10 14
3
2
1
7 9
16
4 8
10 14
3
1
2
7 9
16
4 8
10 14
3
Running Time
for-end
;)1(Heapify-Max
;1][-][-
];[]1[ exchange
begin do
2 downto ][for
);(Heap-Max-Buid
)(Heapsort
A,
AsizeheapAsizeheap
iAA
Alengthi
A
A
O(lg n)
O(n)
)lg( nnO
Quicksort
• Worst-case running time
• Expected running time
• The best practical choice (why?)
)( 2n
)lg( nnO
Divide and Conquer
• Divide the problem into subproblems.
• Conquer the subproblems by solving them recursively.
• Combine the solutions to subproblems into the solution for original problem.
Idea of Quicksort
them.combine toneeded is work no
],..1[ and ]1..[sort after Then
.1for ][][
1for ][][
such that ]..1[ and ]1..[
subarrays into ]..[array Partition
rqAqpA
riqiAqA
qipqAiA
rqAqpA
rpA
Example
9. 8, 7, 6, ,5̂ 4, 3, 2, 1,
6. 7, 9, 8, ,5̂ 1, 2, 4, 3,
Quicksort
then-end
);,1,(Quicksort
);1,,(Quicksort
);,,(Partition
beginn the
if
),,(Quicksort
rqA
qpA
rpAq
rp
rpA
How to find such a partition?
• Such a partition may not exist.
e.g., 5, 4, 3, 2, 1.
• Hence, we may need to make such a partition.
• Take a A[i].
• Classify other A[j] by comparing it with A[i].
8 6, 5, 7, ,4̂ 3, 1, 2,
4̂ 6, 5, 7, 8, ,3 1, 2,
4̂ ,6 5, 7, 8, ,3 1, 2,
4̂ 6, ,5 7, 8, ,3 1, 2,
4̂ 6, 5, ,3 8, 7, ,1 2,
4̂ 6, 5, 3, ,1 7, 8, ,2
4̂ 6, 5, 3, 1, 7, ,8 ,2
4̂ 6, 5, 3, 1, 7, 8, ,2
4̂ 6, 5, 3, 1, 7, 8, ,2 ,
�
�
�
�
�
�
�
�
;1return
];[]1[ exchange
];[][ exchange and 1 then
][ if do
1 tofor
;1
];[
),,(Partition
i
rAiA
jAiAii
xjA
rpj
pi
rAx
rpA
)( timerunning n
7̂ 6, 5, 8, ,4 ,1 3, 2,
4̂ 6, 5, 8, ,7 ,1 3, 2,
4̂ 6, 5, 8, ,7 ,1 3, 2,
4̂ 6, 5, 8, ,1 ,7 3, 2,
4̂ 6, 5, ,8 1, 7, ,3 2,
4̂ 6, 5, ,3 1, 7, ,8 2,
4̂ ,6 5, 3, 1, 7, 8, ,2
�
�
�
�
�
�
�
Expected Partition
partition. balanced ain iswhich
,2
1)110(
1
is subtreeeach of size
expected theThus, ./1y probabilit thehas
positionlast in the appearsnumber each then
,considered are nspermutatio possible all If
n
nn
n
Expected Running Time
1
11
1
1
1
1
lg2
))]0([E)]1([E(1
))]2([E)]1([E(1
))]1([E)]0([E(1
)](E[
:Induction
log)](E[
Quicksort of timerunning expected the:Guess
partition of timerunning the:Assume
n
i
iin
cnc
ncTnTn
ncnTTn
ncnTTn
nT
ncnnT
nc
).)2/3lg(/ Choose(
))2/3lg((lg 3
2lg
3
12lg)1(
2/)1(
)1(21lg)1(
lg)1(
lg2
1
11
1
222
1
))1(/(21
11
1
11
cc
nccncnn
cnc
nncnc
nn
nncnc
incnc
iin
cnc
nnn
i
i
n
i
Randomized Quicksort
);,,(Partition
];[][ exchange
);,(Random
),,(Partition-Randomized
rpA
rAiA
rpi
rpA
Randomized Quicksort
);,1,(Quichsort-Ramdomized
);1,,(Quichsort-Ramdomized
);,,(Partition-Randomized
),,Quicksort(-Randomized
rqA
qpA
rpAq
rpA
What we learnt in this lecture?
• What is heap, max-heap and min-heap?
• Heapsort and Quicksort.
• What is expected running time?