Presentation: Topic: Heap Sort Javaria Zafar Roll # Mscs-sp-007-2021

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Presentation

Topic: Heap Sort


Javaria Zafar
Roll # Mscs-sp-007-2021
Heap Sort
Introduction:
Heap is a data structure that stores collection of objects and has following property
• Structural
It must be almost complete binary tree
(you can go to next level when previous level is not fully filled)

(first make left and right then left node)


• Ordering
In heap tree we can take two order max heap and min heap
(In maximum cases we follow max heap)
Max heap: root node is max element (Parent > child)
Min heap: root node is min element (Parent <child)
• Types of heap tress

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

You might also like