7. Making of Heap
• Shift down from middle node
(Sum of node/2) until first node
1 2 3 4
14 11 57
5 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 7
5 6
3 2
7
3
14 11
2
53
14
2
57
14
11
7
8. 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 7
5 6
3 2142 14
3 2
5
14
11
7
2
9. 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 7
5 6
3 2142 14
3 2
5
14
11
7
2
10. Heap Sort 2
Reorganize heap
Middle = 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 7
5 6
3 2142 14
11
27
2
2
11 27 2112 11
11. Heap Sort 3
Reorganize heap
Middle = 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 7
5 6
3 214147 2112 11
2
7
7 2
3
2
3 2
2
72 7
12. Heap Sort 4
Reorganize heap
Middle = 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 7
5 6
3 21414211113 272 7
5
2
5 2
2
52 5
13. Heap Sort 5
Reorganize heap
Middle = 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
2
2
3
1 2 3 4
5 7
5 6
3 21414211113 277252 5
2
3
3 2
2
32 3
14. Heap Sort 6
Because N = 1 then reorganize isn’t
happen.
b. “Fired” root
c. Subtract number of nodes with 1.
Because N = 0 then sorting processes is
finish.
1
2
1 2 3 4
5 7
5 6
3 21414211113 277252 52332
16. Node have 2
Case Example
Sort these name using heap sort method in
descending way. Num. Name
1 Rahmat
2 Didin
3 Ahmad
4 Joned
5 Syahrul
6 Riki
7 Arif
8 Susi
9 Donni
10 Asih
17. Node have 2
Making of CBT
Rahmat
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
18. 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
19. Node have 2
Heap Sort 2
Riki
Susi
Not Heap
SyahrulJoned
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 SyahrulAhmadSyahrul Ahmad
20. Node have 2
Heap Sort 3
Riki
Susi
Heap
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 SyahrulAhmadSyahrul AhmadArif SyahrulRahmat Syahrul
21. Node have 2
Heap Sort 4
Riki
Susi
Not 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 SyahrulAhmadAhmadArif Rahmat SyahrulJoned ArifArif
22. Node have 2
Heap Sort 5
Riki
Susi
Heap
Donni Didin
Asih Rahmat
Syahrul
JonedAsih
JonedDidin
Joned
1 2 3 4 5 6 7 8 9 10
Asih Donni Didin Riki Susi Joned SyahrulAhmadAhmadRahmat SyahrulJoned ArifArifJonedAsih Didin Joned
23. 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 SyahrulAhmadAhmadRahmat Syahrul ArifArifAsih Didin JonedSusi AsihAsih