Heap Sort: Presented by
Heap Sort: Presented by
Presented By:
P Karteek (210101120061)
Khagendra Samal (210101120063)
Dillip Behera (210101120071)
Harisankar Patra (210101120072)
Karan Bagarti (210101120105)
Debasish Gouda (21010112016)
M Mamata (210101120079)
K Sruti Rani (210101120080
Rashmita Kumari Pradhan (210101120110)
Ankita Pradhan (210101120109)
HEAP
A heap is a data structure that stores a collection
of objects (with keys), and has the following
properties:
Complete Binary tree
Heap Order
19
12 16
1 4 7
19 12 16 1 4 7
Array A
HEAP ORDER PROPERTY
For every node v, other than the root, the key stored in v
is greater or equal (smaller or equal for max heap) than
the key stored in the parent of v.
12 16
1 4 7
19 12 16 1 4 7
Array A
MIN HEAP EXAMPLE
1
4 16
7 12 19
1 4 16 7 12 19
Array A
INSERTION
Algorithm
1)Arrange the nodes in the binary tree form .
2)Node should be arranged as per the specific rules .
3)If the inserted node is bigger than its parent node then replace the
node .
4)If the inserted node is lesser than its parent node then do not change
the position.
5)Do not allow entering nodes on right side until the child nodes of the
left are fulfilled.
6)Do not allow entering nodes on left side until the child nodes of the
right are fulfilled.
7)The procedure from step 3 to step 6 is repeated for each of the node .
19 19
12 16 12 16
4 7 1 4 7 17
1
Insert 17
19
12 17
swap
1 4 7 16
16 4 7 1 12 19
16
4 7
1 12 19
16 16
4 7 4 19
swap
12 19 1 12 7
1
16 19
swap
12 19 12 16
swap
4 7 1 4 7
1
HEAP SORT
The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
19
12 16
1 4 7
EXAMPLE OF HEAP SORT
Take out biggest
19
12 16
Move the last element
to the root
1 4 7
Sorted:
Array A
12 16 1 4 7 19
7
swap
HEAPIFY()
12 16
1 4
Sorted:
Array A
7 12 16 1 4 19
16
12 7
1 4
Sorted:
Array A
16 12 7 1 4 19
Take out biggest
16
Move the last element
to the root
12 7
1 4
Sorted:
Array A
12 7 1 4 16 19
4
12 7
Sorted:
Array A
4 12 7 1 16 19
swap 4
HEAPIFY()
12 7
Sorted:
Array A
4 12 7 1 16 19
12
4 7
Sorted:
Array A
12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root 4 7
Sorted:
Array A
4 7 1 12 16 19
1
swap
4 7
Sorted:
Array A
1 4 7 12 16 19
7
4 1
Sorted:
Array A
7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
4 1 root
Sorted:
Array A
1 4 7 12 16 19
swap 1
HEAPIFY()
4
Sorted:
Array A
4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
1
Sorted:
Array A
1 4 7 12 16 19
Take out biggest
1
Sorted:
Array A
1 4 7 12 16 19
Sorted:
1 4 7 12 16 19
TIME ANALYSIS
• Best Case Complexity - It occurs when there is no sorting required,
i.e. the array is already sorted. The best-case time complexity of
heap sort is O(n logn).
• Average Case Complexity - It occurs when the array elements are
in jumbled order that is not properly ascending and not properly
descending. The average case time complexity of heap sort is O(n
log n).
• Worst Case Complexity - It occurs when the array elements are
required to be sorted in reverse order. That means suppose you have
to sort the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of heap sort
is O(n log n).
ADVANTAGES :
• Efficiency – The time required to perform Heap sort increases
logarithmically while other algorithms may grow exponentially
slower as the number of items to sort increases. This sorting
algorithm is very efficient.