Heap Sort
Heap Sort
Heap Sort
A Knock out tournament is organized where first round is the quarter final in which there are 8 teams CSK,
Mumbai Indians, Delhi Capitals , Kolkata Knight Riders, Punjab Kings, Rajasthan Royals, RCB, Sunrisers
Hyderabad participated.
After the first round four teams will reach in semi final round where two semi finals will be played.
From the semi Final round, two team will reach in final. Now the winner of the final will take
This analogy resemble the concept of Heap(Max-Heap).
It is a type of binary tree in which all levels are completely filled except possibly the last level .
Also last level might or might not be filled completely. If last level is not full then all the nodes should be
filled from the left.
2.5.3.Heap:
A Binary heap is a complete Binary Tree which makes it suitable to be implemented using array.
A Binary Heap is categorized into either Min-Heap or Max-Heap.
In a Min Binary Heap, the value at the root node must be smallest among all the values present in Binary
Heap. This property of Min-Heap must be true repeatedly for all nodes in Binary Tree.
In a Max Binary Heap the value at the root node must be largest among all the values present in Binary
Heap. This property of Max-Heap must be true repeatedly for all nodes in Binary Tree.
As heap is a complete binary tree therefore height of tree having N nodes will always O(log n).
If Heap can be implemented using Array. Assume that Array indexes start from 1.
You can access a parent node or a child nodes in the array with indices below.
A parent node|parent(i) = i / 2
When you look at the node of index 4, the relation of nodes in the tree corresponds to the indices of the
array below. If i = 4, Left Child will be at 2 * 4 that is 8th position and Right Child will be at (2*4 + 1) 9th
position.
Also if the index of left child is 4 then index of its parent will be 4/2 =2.
2.5.3.2 Heap Operations
Following two operations are used to construct a heap from an arbitrary array:
a) MaxHeapify–In a given complete binary tree if a node at index k does not fulfill max-heap property
while its left and right subtree are max heap, MaxHeapify arrange node k and all its subtree to
satisfy maxheap property.
b) BuildMaxHeap–This method builds a Heap from the given array. So BuildMaxHeap use MaxHeapify
function to build a heap from the
MaxHeapify(A,i,N) is a subroutine.
When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the
max-heap property.
MaxHeapify(A,i,N) makes the subtree rooted at A[i] become a max-heap by letting A*i+ “float down”.
Example:
c) Start from the first index of non-leaf node whose index is given by n/2 where n is the size of an array.
d) Set current element having index k as largest.
e) The left child index will be 2*k and the right child index will be 2*k + 1 (when array index starts from 1).
If left Child value is greater than current Element (i.e. element at kth index), set leftChildIndex as largest
index.
If rightChild value is greater than element at largest index, set rightChildIndex as largest index.
g) Repeat the steps from (c) to (f) until the subtrees are also get heapify.
ALGORITHM MaxHeapify(A[ ], k, N)
BEGIN:
L = Left(k) //Left(k)=2*k
R = Right(k) //Right(k)=2*k+1
largest = L
ELSE
largest = k
largest ← R
IF largest != k THEN
END;
Left child of orange marked node
is 2 and right child is 8.
Steps:
ALGORITHM BuildMaxHeap(A[ ], N)
BEGIN:
MaxHeapify(A, i, N)
END;
Analysis
As we know that time complexity of Heapify operation depends on the height of the tree i.e. H and H
should be log N when there are N nodes in the heap.
The height ’h’ increases as we move upwards along the tree. Build-Heap runs a loop from the index of the
last internal node N/2 with height=1, to the index of root(1) with height = lg(n). Hence, Heapify takes
different time for each node, which is θ(h).
Finally, we have got the max-heap in fig.(f).
2. Insert operation:
a) First update the size of the tree by adding 1 in the given size.
b) Then insert the new element at the end of the tree.
c) Now perform Heapify operation to place the new element at its correct position in the tree and
make the tree either as max heap or min heap.
BEGIN:
N = N + 1;
A[ N ] = item;
ReHeapifyUp( A[ ], N, k)
END;
Algorithm: ReHeapifyUp( A[ ], N, k)
BEGIN:
parentindex = k/ 2;
IF parentindex> 0 THEN
IF A[k]>A[parentindex] THEN
Exchnage(A[k], A[parentindex])
ReHeapifyUp(A, N, parentindex)
END;
Analysis:
3 Delete Operation:
a) First exchange theroot element with the last element in the heap..
b) Then remove the last element by decreasing the size of the tree by 1.
c) Now perform Heapify operation to make the tree either as max heap or min heap.
Method-2 To delete an element at any position from Max Heap.
Algorithm: DelHeap(A[ ], N)
BEGIN:
END;
Analysis:
Heap Sort is a popular and efficient sorting algorithm to sort the elements.
Step1: In the Max-Heap largest item is always stored at the root node.
Step 2: Now exchange the root element with the last element in the heap.
Step 4: Now performMax-Heapify operation so that highest element should arrive at root. Repeat these
steps until all the items are sorted.
Example:
Step1: In the Max-Heap largest item is always stored at the root node.
Step 2 Now exchange the root element with the last element.
Step1: Replace
A[1] with A[last].
Step4 Now perform Max-Heapify operation so that highest element should arrive at root.
L = 2*k
R = L+1
Call Max-Heapify()
IF L ≤ heap-size(A) AND A[L] > A[ k ] THEN
largest = L
else
largest = k
largest = R
IF largest != k THEN
ALGORITHM HeapSort(A[ ], N)
BEGIN:
BuildMaxHeap(A, N)
FOR j = N to 2 STEP – 1 DO
END;
Analysis
Heap Sort has θ(nlog n) time complexities for all the cases ( best case, average case, and worst case).
As heap is the complete binary tree so the height of a complete binary tree containing n elements is log n.
During the sorting step, we exchange the root element with the last element and heapify the root element.
For each element, this again takes log n worst time because we might have to bring the element all the way
from the root to the leaf. Since we repeat this n times, the heapsort step is also nlog n.
1. Heapsort: One of the best sorting methods being in-place and log(N) time complexity in all scenarios.
2. Selection algorithms: Finding the min, max, both the min and max, median, or even the kth largest
element can be done in linear time (often constant time) using heaps.
3. Priority Queues: Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm
and Dijkstra’s algorithm. A heap is a useful data structure when you need to remove the object with the
highest (or lowest) priority. Schedulers, timers
4. Graph algorithms: By using heaps as internal traversal data structures, run time will be reduced by
polynomial order. Examples of such problems are Prim's minimal
5. Because of the lack of pointers, the operations are faster than a binary tree. Also, some more
complicated heaps (such as binomial) can be merged efficiently, which is not easy to do for a binary tree.
2.5.6. Variant of Heap Sort
First, we will see some Variant of Heap sort:- Some variants of heapsort are as follows:-
1. Ternary Heap Sort:- Here we use ternary heap instead of binary heap. A ternary heap is one where
each node is having three children. It uses fewer numbers of comparison and swapping as compare
to Binary Heap Sort.
A ternary heap is a data structure which is part of the heap family. It inherits the properties from
ternary tree and the heap data structure.
In both cases, a ternary heap has to satisfy the heap property, which is every level of tree has to be filled, or
if the last level is not filled, the leaves are distributed from left to right.
You can access a parent node or a child nodes in the array with indices below.
Priority queue is a type of queue in which every element has a key associated to it and the queue returns
the element according to these keys.SO this queue does not follow the concept of FCFS(First Come First
Serve).
Thus, a max-priority queue returns the element with maximum key first whereas, a min-priority queue
returns the element with the smallest key first.
Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s
algorithm. A heap is a useful data structure when you need to remove the object with the highest (or
Therefore, with the help of heap following operations of the priority queue can be implemented:
a) First update the size of the tree by adding 1 in the given size.
b) Then insert the new element at the end of the tree.
c) Now perform Heapify operation to place the new element at its correct position in the tree and
make the tree either as max heap or min heap.
b) Implementation of PrintMax()/PrintMin():
As we know that the largest element is present at the root node of max heap and smallest
element is present at root in min heap. So simply print the root node to get either lagest
element(i.e element with highest priority or key) or smallest element (i.e element with lowest
priority or key)
c) Implementation of DeleteMax/DeleteMin():
d) First exchange theroot element with the last element in the heap..
e) Then remove the last element by decreasing the size of the tree by 1.
f) Now perform Heapify operation to make the tree either as max heap or min heap.
Time Complexity: O(log N) where N is the number of elements in heap.
d) Implementation of UpdatePriority():
Using this operation, we can
i) Either decrease or increase the priority or key of any element then
ii) Using the heapify procedure place that update priority element at its correct position.
To maintain the priority queue using array, it take O(Nlog N) time while O(log N) using Heap.