Lecture 2 Sorting
description
Transcript of Lecture 2 Sorting
![Page 1: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/1.jpg)
Lecture 2 Sorting
![Page 2: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/2.jpg)
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.,
![Page 3: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/3.jpg)
Efficiency
• Running time from receiving the input to producing the output.
)log(
)( 2
nnO
nOInsertion Sort
Merge Sort
Running time
![Page 4: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/4.jpg)
Is array a data structure?
![Page 5: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/5.jpg)
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?
![Page 6: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/6.jpg)
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.
![Page 7: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/7.jpg)
Heapsort
• Heap, a data structure
• Max-Heapify procedure
• Building a heap
• Heapsort
![Page 8: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/8.jpg)
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
![Page 9: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/9.jpg)
;12return
)(Right
;2return
)(Left
;2/return
)(Parent
ii
i
i
i
i
![Page 10: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/10.jpg)
Max-Heap
].[)](Parent[
:property following thesatisfiesroot
theother than nodeevery heap,-max aIn
iAiA
i
![Page 11: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/11.jpg)
Min-Heap
].[)](Parent[
:property following thesatisfiesroot
theother than nodeevery heap,-min aIn
iAiA
i
![Page 12: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/12.jpg)
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”.
![Page 13: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/13.jpg)
4
714
2 18
7
2 8 1
8 7
12
14
4
14
4
![Page 14: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/14.jpg)
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
![Page 15: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/15.jpg)
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
![Page 16: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/16.jpg)
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.
![Page 17: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/17.jpg)
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
![Page 18: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/18.jpg)
4
31
16 10
7
2 9
14 8
.2/1)/2(n even. is 1. Case nn
7
n 1n
![Page 19: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/19.jpg)
4
31
16 10
7
2 9
14 8
.12/1)/2(n odd. is 2. Case nn
n 1n
![Page 20: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/20.jpg)
4
31
16 10
7
2 9
14 8
.12/1)/2(n odd. is 2. Case nn
n 1n
![Page 21: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/21.jpg)
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.
![Page 22: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/22.jpg)
4
31
16 10
7
2 9
14 8
![Page 23: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/23.jpg)
4
31
16 10
7
2 9
14 8
![Page 24: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/24.jpg)
4
31
16 10
7
14 9
2 8
![Page 25: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/25.jpg)
4
31
16 10
7
14 9
2 8
![Page 26: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/26.jpg)
4
1
16 3
7
14 9
2 8
10
![Page 27: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/27.jpg)
4
1
16 3
7
14 9
2 8
10
![Page 28: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/28.jpg)
4
16
1 3
7
14 9
2 8
10
![Page 29: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/29.jpg)
4
16
7 3
1
14 9
2 8
10
![Page 30: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/30.jpg)
16
4
7 3
1
14 9
2 8
10
![Page 31: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/31.jpg)
16
14
7 3
1
4 9
2 8
10
![Page 32: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/32.jpg)
16
14
7 3
1
8 9
2 4
10
![Page 33: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/33.jpg)
Analysis
).( and levelat
nodes ofnumber theis )(# where
)()(# timerunning0
Aheighthi
inode
ihOinodeh
i
iinode
Asizeheapnnh
2)(#
)(- wherelg
![Page 34: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/34.jpg)
16
14
7 3
1
8 9
2 4
10
0i
1i
hi
![Page 35: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/35.jpg)
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
![Page 36: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/36.jpg)
Heapsort
for-end
;)1(Heapify-Max
;1][-][-
];[]1[ exchange
begin do
2 downto ][for
);(Heap-Max-Buid
)(Heapsort
A,
AsizeheapAsizeheap
iAA
Alengthi
A
A
![Page 37: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/37.jpg)
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.
![Page 38: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/38.jpg)
1
14
7 3
16
8 9
2 4
10
![Page 39: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/39.jpg)
1
14
7 3
16
8 9
2 4
10
1, 14, 10, 8, 7, 9, 3, 2, 4, 16.
![Page 40: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/40.jpg)
14
8
7 3
16
4 9
2 1
10
![Page 41: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/41.jpg)
14
8
7 3
16
4 9
2 1
10
14, 8, 10, 4, 7, 9, 3, 2, 1, 16.
![Page 42: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/42.jpg)
1
8
7 3
16
4 9
2 14
10
![Page 43: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/43.jpg)
1
8
7 3
16
4 9
2 14
10
1, 8, 10, 4, 7, 9, 3, 2, 14, 16.
![Page 44: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/44.jpg)
10
8
7 3
16
4 1
2 14
9
![Page 45: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/45.jpg)
10
8
7 3
16
4 1
2 14
9
10, 8, 9, 4, 7, 1, 3, 2, 14, 16.
![Page 46: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/46.jpg)
2
8
7 3
16
4 1
10 14
9
![Page 47: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/47.jpg)
2
8
7 3
16
4 1
10 14
9
2, 8, 9, 4, 7, 1, 3, 10, 14, 16.
![Page 48: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/48.jpg)
9
8
7 2
16
4 1
10 14
3
![Page 49: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/49.jpg)
9
8
7 2
16
4 1
10 14
3
9, 8, 3, 4, 7, 1, 2, 10, 14, 16.
![Page 50: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/50.jpg)
2
8
7 9
16
4 1
10 14
3
![Page 51: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/51.jpg)
8
7
2 9
16
4 1
10 14
3
![Page 52: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/52.jpg)
1
7
2 9
16
4 8
10 14
3
![Page 53: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/53.jpg)
7
4
2 9
16
1 8
10 14
3
![Page 54: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/54.jpg)
2
4
7 9
16
1 8
10 14
3
![Page 55: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/55.jpg)
4
2
7 9
16
1 8
10 14
3
![Page 56: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/56.jpg)
1
2
7 9
16
4 8
10 14
3
![Page 57: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/57.jpg)
3
2
7 9
16
4 8
10 14
1
![Page 58: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/58.jpg)
1
2
7 9
16
4 8
10 14
3
![Page 59: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/59.jpg)
2
1
7 9
16
4 8
10 14
3
![Page 60: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/60.jpg)
1
2
7 9
16
4 8
10 14
3
![Page 61: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/61.jpg)
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
![Page 62: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/62.jpg)
Quicksort
• Worst-case running time
• Expected running time
• The best practical choice (why?)
)( 2n
)lg( nnO
![Page 63: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/63.jpg)
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.
![Page 64: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/64.jpg)
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
![Page 65: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/65.jpg)
Example
9. 8, 7, 6, ,5̂ 4, 3, 2, 1,
6. 7, 9, 8, ,5̂ 1, 2, 4, 3,
![Page 66: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/66.jpg)
Quicksort
then-end
);,1,(Quicksort
);1,,(Quicksort
);,,(Partition
beginn the
if
),,(Quicksort
rqA
qpA
rpAq
rp
rpA
![Page 67: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/67.jpg)
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].
![Page 68: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/68.jpg)
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 ,
�
�
�
�
�
�
�
�
![Page 69: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/69.jpg)
;1return
];[]1[ exchange
];[][ exchange and 1 then
][ if do
1 tofor
;1
];[
),,(Partition
i
rAiA
jAiAii
xjA
rpj
pi
rAx
rpA
)( timerunning n
![Page 70: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/70.jpg)
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
�
�
�
�
�
�
�
![Page 71: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/71.jpg)
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
![Page 72: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/72.jpg)
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
![Page 73: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/73.jpg)
).)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
![Page 74: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/74.jpg)
Randomized Quicksort
);,,(Partition
];[][ exchange
);,(Random
),,(Partition-Randomized
rpA
rAiA
rpi
rpA
![Page 75: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/75.jpg)
Randomized Quicksort
);,1,(Quichsort-Ramdomized
);1,,(Quichsort-Ramdomized
);,,(Partition-Randomized
),,Quicksort(-Randomized
rqA
qpA
rpAq
rpA
![Page 76: Lecture 2 Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062305/56815aca550346895dc89b9c/html5/thumbnails/76.jpg)
What we learnt in this lecture?
• What is heap, max-heap and min-heap?
• Heapsort and Quicksort.
• What is expected running time?