Algorithm Ch6 Heapsort
Algorithm Ch6 Heapsort
Algorithm Ch6 Heapsort
Chapter6Heapsort
AssistantProfessor:ChingChiLin chingchi.lin@gmail.com
DepartmentofComputerScienceandEngineering NationalTaiwanOceanUniversity
Outline
` ` ` ` `
Thepurposeofthischapter
`
Inthischapter,weintroducetheheapsort algorithm.
` ` `
anusefuldatastructureforheapsort makesanefficientpriorityqueue
Heaps
`
16
14
4 5 6
10
7
1 3
3 10
4 8
5 7
6 9
7 3
8 2
9 4
10 1
8
8 9
7
10
16 14
Binarytreerepresentations
1 2 4 8 9 10 5 11 12 6 13 14 3 7 15 8 4 9 10 2 5
1 3 6 7
Afullbinarytree ofheight3.
Acompletebinarytreewith10nodes andheight3.
AttributesofaHeap
`
AnarrayAthatpresentsaheapwithtwoattributes:
` ` `
16
14
4 5 6
10
7
1 3
3 10
4 8
5 7
6 9
7 3
8 2
9 4
10 1
16 14
8
8 9
7
10
2
6
length[A]=heapsize[A]=10
Basicprocedures1/2
`
16
14
4 5 6
10
7
1 3
3 10
4 8
5 7
6 9
7 3
8 2
9 4
10 1
16 14
8
8 9
7
10
2
7
Basicprocedures2/2
`
TheLEFT procedurecancompute2i inoneinstructionbysimply shiftingthebinaryrepresentationofi leftonebitposition. Similarly,theRIGHT procedurecanquicklycompute2i +1by shiftingthebinaryrepresentationofileftonebitpositionand addingina1astheloworderbit. ThePARENT procedurecancomputei/2 byshiftingirightone bitposition.
Heapproperties
`
Therearetwokindofbinaryheaps:maxheapsandminheaps.
`
thesmallestelementinaminheapisattheroot
Maxandminheaps
14 12 10 8 6 7 5 MaxHeaps 6 9 3 25 30
2 7 10 8 6 4 50 20
10 83 21
11
MinHeaps
10
Theheightofaheap
`
theheightofnode2is2 theheightoftheheapis3
2
16
14
4 5 6
10
7
8
8 9
7
10
11
Theremainder ofthischapter
`
Weshallpresentssomebasicproceduresintheremainder ofthischapter.
` ` ` `
TheMAXHEAPIFY procedure,whichrunsinO(lg n)time,isthe keytomaintainingthemaxheapproperty. TheBUILDMAXHEAP procedure,whichrunsinO(n)time, producesamaxheapfromanunorderedinputarray. TheHEAPSORT procedure,whichrunsinO(n lg n)time,sortsan arrayinplace. TheMAXHEAPINSERT,HEAPEXTRACTMAX,HEAPINCREASEKEY, andHEAPMAXIMUM procedures,whichruninO(lg n)time, allowtheheapdatastructuretobeusedasapriorityqueue.
12
Outline
` ` ` ` `
13
TheMAXHEAPIFY procedure1/2
`
Input:anarrayAandanindexi Output:thesubtree rootedatindexi becomesamaxheap Assume:thebinarytreesrootedatLEFT(i) andRIGHT(i) are maxheaps,butA[i]maybesmallerthanitschildren Method:letthevalueatA[i]floatdown inthemaxheap
1 1 3 2
i
4
16
16
4
5 6
10
7
MAXHEAPIFY
4
14
5 6
10
7
14
8 9
7
10
3
8
8
9
7
10
2
14
TheMAXHEAPIFY procedure2/2
MAXHEAPIFY(A,i) 1. l LEFT(i)
2. 3. 4. 5. 6. 7. 8. 9. 10.
r RIGHT(i) if l heapsize[A]andA[l]>A[i] then largest l else largest i if r heapsize[A]anda[r]>A[largest] then largest r if largest i
A[largest] then exchangeA[i]
MAXHEAPIFY (A,largest)
15
AnexampleofMAXHEAPIFY procedure
1 2 1 3 2
16
16
i
4
4
5 6
10
7 4
14
5 6
10
7
14
8 9
7
10
i
8
4
9
7
10
1
1 2
16
14
4 5 6
10
7
8
8 9
7 i 1
10
2
16
Thetimecomplexity
` ` `
worstcaseoccurswhenthelastrowofthetreeisexactlyhalffull
solveitbycase2ofthemastertheorem
Outline
` ` ` ` `
18
BuildingaHeap
` ` `
WecanusetheMAXHEAPIFY proceduretoconvertanarray A=[1..n]intoamaxheapinabottomupmanner. Theelementsinthesubarray A[(n/2+1)n ]areallleaves of thetree,andsoeachisa1elementheap. TheprocedureBUILDMAXHEAP goesthroughtheremaining nodesofthetreeandrunsMAXHEAPIFY oneachone.
BUILDMAXHEAP(A)
1. 2. 3.
19
Anexample
1 A 4 2 1 3 3 4 2 5 16 6 9 7 10 8 14
1 2
9 8
10 7
1
4
i
9 10
2
8
16
10
14
20
[1] 4 [2] 1 [4] 2 14 [8] 8 [5] 16 7 [3] 3 9 [6] [9] [10] MAXHEAPIFY(A,5) 10 [7] [2] 1 [4] 2 14 [8] 8
[1] 4 [3] 3 9 [6] 7 [9] [10] MAXHEAPIFY(A,4) 10 [7] 2 [8] 8 [2] 1 [4] 14
[5] 16
[5] 16
MAXHEAPIFY(A,1) [1] 4 [2] 16 [4] 14 2 [8] 8 [5] 7 1 [3] 10 9 [6] [9] [10] 3 [7] 2 [8] 8 7 [9] [10] [2] 16 [4] 14 [5] 1 [1] 4 [3] 10 9 [6] 3 [7] 2 [8] 8
MAXHEAPIFY(A,2) [1] 4 [2] 1 [4] 14 [5] 16 7 [3] 10 9 [6] [9] [10] 3 [7]
[1] 16 [2] 4 [4] 14 2 [8] 8 [5] 7 1 [3] 10 9 [6] [9] [10] 3 [7] 2 [8] 8 [2] 14 [4] 4
[5] 7
[5] 7
Correctness1/2
`
Weneedtoshowthat
` ` `
23
Correctness2/2
`
Initialization: Priortothefirstiterationoftheloop,i=n/2.
n/2+1,n isaleafandisthustherootofa
trivialmaxheap.
`
24
Timecomplexity1/2
`
Analysis1:
` `
EachcalltoMAXHEAPIFY costsO(lg n),andthereareO(n)such calls. Thus,therunningtimeisO(n lg n).Thisupperbound,through correct,isnotasymptoticallytight. Forannelementheap,heightislg n andatmostn /2h+1 nodesofanyheighth. ThetimerequiredbyMAXHEAPIFY whencalledonanodeof heighth isO(h). lg n lg n h n Thetotalcostis h +1 O (h ) = O n h .
h =0
Analysis2:
` ` `
h =0
25
Timecomplexity2/2
`
Thelastsummationyields
1/ 2 h = =2 2 h (1 1 / 2) h =0 2
Thus,therunningtimeofBUILDMAXHEAP canbeboundedas
lg n
h =0
h n 2 h +1 O (h) = O n 2 h = O(n) h =0
Wecanbuildamaxheapfromanunorderedarrayinlinear time.
26
Outline
` ` ` ` `
27
Theheapsort algorithm
` ` `
Sincethemaximumelementofthearrayisstoredattheroot, A[1]wecanexchange itwithA[n]. Ifwenowdiscard A[n],weobservethatA[1...(n 1)]caneasily bemadeintoamaxheap. ThechildrenoftherootA[1]remainmaxheaps,butthenew rootA[1]elementmayviolatethemaxheapproperty,sowe needtoreadjust themaxheap.ThatistocallMAXHEAPIFY(A,1).
HEAPSORT(A)
1. 2. 3. 4. 5. 28
Anexample
1
16
14
4 5 6
10
7
8
8 9
10
1 A
29
2 2
3 3
4 4
5 7
6 8
7 9
8 10
9 14
10 16
[1] 16 [2] 14 [4] 8 2 [8] 4 [5] 7 1 Initialheap [3] 10 9 [6] [9] [10] 3 [7] 2 [8] 4 [2] 14 [4] 8
[1] 1 [3] 10 9 [6] 16 Exchange Heapsize=10 Sorted=[16] Exchange Heapsize=9 Sorted=[14,16] [1] 1 [3] 10 9 [6] 3 [7] 2 14 16 [8] [9] [10] [2] 8 [4] 4 [5] 7 [3] 10 9 [6] 3 [7] 2 1 [2] 8 [4] 4 [9] [10] 3 [7] 2 4 [2] 14 [4] 8
[1] 1 [3] 10 9 [6] 16 Discard Heapsize=9 Sorted=[16] Readjust Heapsize=9 Sorted=[16] [1] 14 [3] 10 9 [6] 16 [8] [9] [10] 3 [7] [8] [9] [10] 3 [7]
[5] 7
[5] 7
[5] 7
[1] 10 [2] 8 [4] 4 2 14 [5] 7 16 [3] 9 1 [6] [8] [9] [10] Readjust Heapsize=8 Sorted=[14,16] Heapsize=3 Sorted=[4,7,8,9,10,14,16] [1] 3 [2] 2 [4] 4 10 14 [5] 7 16 [3] 1 8 [6] [8] [9] [10] 9 [7] 10 14 [2] 2 [4] 1 3 [7] 10 [8] 14 [2] 8 [4] 4
[1] 9 [3] 3 1 [6] 16 Heapsize=7 Sorted=[10,14,16] [9] [10] 2 [7] 10 14 [2] 7 [4] 4
[5] 7
[5] 2
Heapsize=5 Sorted=[8,9,10,14,16] [1] 7 [2] 4 [4] 1 14 [5] 2 16 [3] 3 8 [6] [8] [9] [10] 9 [7]
[5] 7
Timecomplexity
`
32
Outline
` ` ` ` `
33
Heapimplementationofpriorityqueues
` ` ` `
34
Priorityqueues
`
Amaxpriorityqueue supportsthefollowingoperations.
` ` `
INSERT(S,x): insertstheelementx intothesetS. MAXIMUM(S): returnstheelementofS withthelargestkey. EXTRACTMAX(S): removesandreturnstheelementofS with thelargestkey. INCREASEKEY(S,x,k):increasesvalueofelementxs keytothe newvaluek.Assumek xs currentkey value.
35
Findingthemaximumelement
` `
returnA[1]
TherunningtimeofHEAPMAXIMUM is(1).
36
Extractingmaxelement
`
if heapsize[A]<1 thenerror heapunderflow max A[1] A[1] A[heapsize[A]] heapsize[A] heapsize[A] 1 MAXHEAPIFY(A,1) return max
` `
Increasingkeyvalue
`
i PARENT(i)
` `
Anexampleofincreasingkeyvalue
1
16
14
4 5 6
10
7
8
8
7
9 10
i
2
4 15
IncreaseKey!
39
[1] 16 [2] 14 [4] 8 i 2 [8] 4 1 [9] [10] [5] 7 [3] 10 9 [6] 3 [7] 2 [8] 15 [2] 14 [4] 8 i
[5] 7
[9] [10]
[5] 7
[4] 15
[5] 7
[9] [10]
Insertingintotheheap
`
HEAPINCREASEKEY(A,heapsize[A],key)
` `
41