Download - Data Structure (Heap Sort)
![Page 1: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/1.jpg)
Heap Sort
Adam M.B.
![Page 2: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/2.jpg)
DEFINITION
![Page 3: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/3.jpg)
Heap
HeapTree
Complete Binary Tree
(CBT) Max HeapValue of node >= value of
its child
Min HeapValue of node <= value of
its child
![Page 4: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/4.jpg)
Example of HeapMax Heap Min Heap
24
19
13
15
11 5 8
4
4
5
13
8
11 19 15
24
k
2k 2k+1
2k+1 2k+12k
2k
2k
![Page 5: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/5.jpg)
BASIC PROCESSES
![Page 6: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/6.jpg)
• Making of heap
• Heap sort
Processes in Heap
![Page 7: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/7.jpg)
Making of Heap
• Shift down from middle node (Sum of node/2) until first node
1 2 3 4
14 11 575 6
3 2
1
2 3
54 6• N = 6, Middle = N/2 = 6/2
= 3• Reorganize on third node• Reorganize on second node• Reorganize on first node
1 2 3 4
14 11 5 75 6
3 2
7
3
14
11
2
53
14
2
57
14
11
7
![Page 8: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/8.jpg)
Heap Sort 1
a.Binary tree in Max heap state.
b.“Fired” root and swap with last node.
c. Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
1
2 3
54 6
1 2 3 4
14 11 5 75 6
3 2142 14
3 2
5
14
11
7
2
![Page 9: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/9.jpg)
Heap Sort 1
a.Binary tree in Max heap state.
b.“Fired” root and swap with last node.
c.Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
1
2 3
54 6
1 2 3 4
14 11 5 75 6
3 2142 14
3 2
5
14
11
7
2
![Page 10: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/10.jpg)
Heap Sort 2
Reorganize heapMiddle = N/2 = 5/2 = 2
b.“Fired” root and swap with last node.
c. Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
Reorganize heap on second node.Reorganize heap on first node.
1
2 3
54
3
511
7
2
1 2 3 4
11 5 75 6
3 2142 14
11
27
2
2
11 27 2112 11
![Page 11: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/11.jpg)
Heap Sort 3
Reorganize heapMiddle = N/2 = 4/2 = 2
b.“Fired” root and swap with last node.
c. Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
Reorganize heap on second node.Reorganize heap on first node.
1
2 3
4
3
5
2
7
1 2 3 4
5 75 6
3 214147 2112 11
2
7
7 2
3
2
3 2
2
72 7
![Page 12: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/12.jpg)
Heap Sort 4
Reorganize heapMiddle = N/2 = 3/2 = 1
b.“Fired” root and swap with last node.
c. Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
Reorganize heap on first node.
1
2 3
5
2
3
1 2 3 4
5 75 6
3 21414211113 272 7
5
2
5 2
2
52 5
![Page 13: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/13.jpg)
Heap Sort 5
Reorganize heapMiddle = N/2 = 2/2 = 1
b.“Fired” root and swap with last node.
c. Subtract number of nodes with 1.
d.If N > 1 then reorganize heap again.
e.Repeat step b to d until node is empty (N=0)
Reorganize heap on first node.
1
22
3
1 2 3 4
5 75 6
3 21414211113 277252 5
2
3
3 2
2
32 3
![Page 14: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/14.jpg)
Heap Sort 6
Because N = 1 then reorganize isn’t happen.b.“Fired” rootc. Subtract number of nodes with
1.
Because N = 0 then sorting processes is finish.
1
2
1 2 3 4
5 75 6
3 21414211113 277252 52332
![Page 15: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/15.jpg)
m
CASE
![Page 16: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/16.jpg)
Node have 2
Case ExampleSort these name using heap sort
method in descending way.Num. Name1 Rahmat2 Didin3 Ahmad4 Joned5 Syahrul6 Riki7 Arif8 Susi9 Donni10 Asih
![Page 17: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/17.jpg)
Node have 2
Making of CBTRahmat
AhmadDidin
Joned Syahrul Riki Arif
Susi Donni Asih
Not Heap
Complete Binary Tree
Rahmat
Didin Ahmad Joned Syahrul
Riki Arif Susi Donni Asih
1 2 3 4 5 6 7 8 9 10
![Page 18: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/18.jpg)
Node have 2
Heap Sort 1
1 2 3 4 5 6 7 8 9 10
Rahmat
AhmadDidin
Joned Syahrul Riki Arif
Susi Donni Asih
Heap
Syahrul
Asih
Joned
Donni Didin
Asih Rahmat
Ahmad
Arif
Rahmat
Ahmad Asih Arif Donni Didin Riki Rahmat
Susi Joned Syahrul
![Page 19: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/19.jpg)
Node have 2
Heap Sort 2
Riki
Susi
Not HeapSyahrulJoned
Donni Didin
Asih
Ahmad
Arif
Rahmat
Syahrul
1 2 3 4 5 6 7 8 9 10
Ahmad Asih Arif Donni Didin Riki Rahmat
Susi Joned Syahrul
AhmadSyahrul
Ahmad
![Page 20: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/20.jpg)
Node have 2
Heap Sort 3
Riki
SusiHeap
Joned
Donni Didin
Asih Arif
Rahmat
SyahrulArif
SyahrulRahmat
Syahrul
1 2 3 4 5 6 7 8 9 10
Asih Arif Donni Didin Riki Rahmat
Susi Joned Syahrul
AhmadSyahrul
AhmadArif Syahrul
Rahmat
Syahrul
![Page 21: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/21.jpg)
Node have 2
Heap Sort 4
Riki
SusiNot Heap
Joned
Donni Didin
Asih
Arif
Rahmat
Syahrul
Joned
1 2 3 4 5 6 7 8 9 10
Asih Donni Didin Riki Susi Joned Syahrul
AhmadAhmadArif Rahmat
Syahrul
Joned ArifArif
![Page 22: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/22.jpg)
Node have 2
Heap Sort 5
Riki
SusiHeap
Donni Didin
Asih Rahmat
Syahrul
JonedAsih
JonedDidin
Joned
1 2 3 4 5 6 7 8 9 10
Asih Donni Didin Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
Joned ArifArifJonedAsih Didin Joned
![Page 23: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/23.jpg)
Node have 2
Heap Sort 6
Riki
Susi Not Heap
Donni
Rahmat
Syahrul
Asih
Didin
Joned
Susi
1 2 3 4 5 6 7 8 9 10
Donni Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifAsih Didin JonedSusi AsihAsih
![Page 24: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/24.jpg)
Node have 2
Heap Sort 7
Riki
Heap
Donni
Rahmat
Syahrul
Didin
Joned
SusiDidin
SusiDonni
Susi
1 2 3 4 5 6 7 8 9 10
Donni Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifDidin JonedSusi AsihAsihDidin SusiDonni Susi
![Page 25: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/25.jpg)
Node have 2
Heap Sort 8
Riki
Not Heap
Rahmat
SyahrulJoned
Didin
Donni
Susi
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifJoned AsihAsihDidin Donni SusiSyahrul
DidinDidin
![Page 26: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/26.jpg)
Node have 2
Heap Sort 9
Riki
Heap
Rahmat
Joned
Donni
Susi
SyahrulDonni
SyahrulJoned
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifJoned AsihAsihDonni SusiSyahrul
DidinDidinDonni Syahrul
Joned Syahrul
![Page 27: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/27.jpg)
Node have 2
Heap Sort 10
Riki
Not Heap
Rahmat
Susi
Donni
Joned
Syahrul
Riki
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifAsihAsihSusi DidinDidinDonni Joned Syahrul
Riki DonniDonni
![Page 28: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/28.jpg)
Node have 2
Heap Sort 11
Heap
Rahmat
Susi
Joned
Syahrul
RikiJoned
Riki
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifAsihAsihSusi DidinDidinJoned Syahrul
Riki DonniDonniJoned Riki
![Page 29: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/29.jpg)
Node have 2
Heap Sort 12
Not Heap
Rahmat
Susi Syahrul
Joned
Riki
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJoned RikiSyahrul
JonedJoned
![Page 30: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/30.jpg)
Node have 2
Heap Sort 13
Heap
Rahmat
Susi
Riki
SyahrulRahmat
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadRahmat
Syahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniSyahrul
JonedRiki DonniRahmat
Syahrul
![Page 31: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/31.jpg)
Node have 2
Heap Sort 14
Not Heap
Susi
Riki
Rahmat
Syahrul
Susi
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedRiki DonniRahmat
Syahrul
Susi Rahmat
Rahmat
![Page 32: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/32.jpg)
Node have 2
Heap Sort 15
Heap
Riki Syahrul
SusiRiki
Susi
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedRiki DonniSyahrul
Susi Rahmat
RahmatRiki Susi
![Page 33: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/33.jpg)
Node have 2
Heap Sort 17
Heap
Susi
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedDonniSyahrul
Rahmat
RahmatSyahrul
Susi Riki
Susi
Syahrul
Susi Syahrul
![Page 34: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/34.jpg)
Node have 2
Heap Sort 16
Not Heap
Syahrul
Riki
Susi
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedDonniSyahrul
Rahmat
RahmatRiki Susi Riki
Syahrul
Syahrul
Riki
![Page 35: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/35.jpg)
Node have 2
Heap Sort 18
Heap
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedDonniSyahrul
Rahmat
RahmatRiki
Susi
Syahrul
Susi Syahrul
Syahrul
SusiSyahrul
Susi
![Page 36: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/36.jpg)
Node have 2
Heap Sort 19
Syahrul
1 2 3 4 5 6 7 8 9 10
Riki Susi Joned Syahrul
AhmadAhmadSyahrul
ArifArifAsihAsihSusi DidinDidinSyahrul
DonniDonniJonedDonniSyahrul
Rahmat
RahmatRikiSyahrul
SusiSyahrul
SusiSyahrul
![Page 37: Data Structure (Heap Sort)](https://reader035.vdocuments.us/reader035/viewer/2022062412/58cfd6211a28ab13238b4e71/html5/thumbnails/37.jpg)
Contact Person:Adam Mukharil Bachtiar
Informatics Engineering UNIKOMJalan Dipati Ukur Nomor. 112-114 Bandung 40132
Email: [email protected]: http://adfbipotter.wordpress.com
Copyright © Adam Mukharil Bachtiar 2012
GRACIASTHANK YOU