Professional Documents
Culture Documents
Presentation: Topic: Heap Sort Javaria Zafar Roll # Mscs-sp-007-2021
Presentation: Topic: Heap Sort Javaria Zafar Roll # Mscs-sp-007-2021
Presentation: Topic: Heap Sort Javaria Zafar Roll # Mscs-sp-007-2021
Max heap
(Parent>Child) Min heap
(Parent<Child)
10 5
8 7
10 15
5 4 6 3 12 13 17 18
How to make heap tree(Insertion in tree)
There are two methods of constructing heap tree
1. Insert key one by one in the given order
2. Heapify Method
Formulas:
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
One by one key insertion method
Example
14, 24, 12, 11 ,25 ,8, 35
Insert a key into heap takes 0(1) time
Insert a key into already inserted heap
In worst case takes
lg(n) comparison and (lgn) swap
Total n elements so O(nlgn)
Heapify Method
• It takes less time than key by key method
Time Complexity O(n)
In Heapify method make tree randomly
Min heap
Example
145,40,25,65,12,48,18,1,100,27,7,3,45,9,30
Min Heap
Start swapping from right to left
Deletion in heap tree
• We can delete root and right most element in the lowest level