0% found this document useful (0 votes)
10 views22 pages

Heap Sort

Heap Sort is an efficient sorting algorithm that utilizes a binary heap data structure, specifically a Max-Heap, to sort elements. The process involves building a Max-Heap, repeatedly exchanging the root with the last element, and performing heapify operations to maintain the heap structure until all elements are sorted. Heap Sort has a time complexity of θ(n log n) and is advantageous for its in-place sorting capability.

Uploaded by

protocolpsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views22 pages

Heap Sort

Heap Sort is an efficient sorting algorithm that utilizes a binary heap data structure, specifically a Max-Heap, to sort elements. The process involves building a Max-Heap, repeatedly exchanging the root with the last element, and performing heapify operations to maintain the heap structure until all elements are sorted. Heap Sort has a time complexity of θ(n log n) and is advantageous for its in-place sorting capability.

Uploaded by

protocolpsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

2.5.

Heap Sort

2.5.1. Heap Analogy

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).

2.5.2. Pre requisite:- Complete Binary Tree

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.

Note:- In the above diagram, nodes are


filling from left to right and level via level.
Application: To implement Heap Data structure.

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).

2.5.3.1 Implementation of Heap:

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 root node|i = 1, the first item of the array

A left child node|left(i) = 2*i

A right child node|right(i)=2*i+1

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

1. Construct max Heap:

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:

a) Given an arrays as below

b) First construct a complete binary tree from the array

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.

f) Exchange largest with currentElement(i.e. element at kth 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

//Initialize Largest index

IF L ≤ heap-size(A) and A[L] > A[ k ] THEN

largest = L

ELSE

largest = k

IF R ≤ heap-size(A) and A[ R ] > A[ largest ] THEN

largest ← R

IF largest != k THEN

Exchange A[ k ] with A[ largest ]

MaxHeapify (A, largest,N)

END;
Left child of orange marked node
is 2 and right child is 8.

Steps:

1) Is 4 > 2 , greater is 4 stored


in temp.
2) Is 4 > 8, greater is 8, now
largest is 8. Temp is having
index of 8.
3) Swap the element at index
of marked node and temp
node.

ALGORITHM BuildMaxHeap(A[ ], N)
BEGIN:

FOR i = N/2 TO 1 STEP – 1 DO

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:

To insert an element in Max Heap.


Following are the steps to to insert an element in Max Heap.

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.

Example: To show how insert operation works.


This is the array representation of the given max-heap.

Algorithm: InsertNode( A[ ], N, item)

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:

Time Complexity: θ(log n)

3 Delete Operation:

Method-1 To delete root element from Max Heap.

Following are the steps to delete an element from Max Heap.

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.

Following are the steps to delete an element from Max Heap.

a) First pick the element to be deleted.


b) Now exchange it with the last element.
c) Then remove this element by decreasing the size of the tree by 1.
d) Now perform Heapify operation to make the tree either as max heap or min heap.

Algorithm: DelHeap(A[ ], N)

BEGIN:

lastitem = A[N] // Get the last element

A[1] = lastitem; // Replace root with first element

N = N - 1; // Decrease size of heap by 1

MaxHeapify(A,1,N); // heapify the root node

END;

Analysis:

Time Complexity: θ(log n)

2.5. 4. Heap Sort

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 3: Decrese the size of the heap by 1.

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].

Step3 Decrese the size of the heap by 1 .

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

IF R ≤ heap-size(A) AND A[ R ] > A[ largest ]


THEN

largest = R

IF largest != k THEN

Exchange A[ k ] with A[ largest ]


Now this is the Max-heap after performing first deletion of root element.

Repeat these steps until all the items are sorted.


Now, the last two fields of the array are sorted.
We repeat the process until there is only one element left in the tree:
This last element is the smallest element and remains at the beginning of the array. The algorithm is
finished, as the array is sorted:

ALGORITHM HeapSort(A[ ], N)
BEGIN:

BuildMaxHeap(A, N)

FOR j = N to 2 STEP – 1 DO

Exchange(A[j], A[1]) /*Exchange root and last Node in the Array*/

MaxHeapify(A, 1, j-1) /*Readjust the Heap starting from root node*/

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.

It performs sorting in θ(1) space complexity as it is in-place sorting.

2. 5.5 Uses of Heap Sort

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.

Types: There are two kinds of ternary heaps:


a) Min ternary heap
A min ternary heap has the smallest element as its root. Parent node has to be smaller than or
equal to its children.
b) Max ternary heap
A max ternary heap has the biggest element as its root. Each node has to be greater than or
equal to its children.

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.

A root node|i = 1, the first item of the array

A left child node|left(i) = 3*i-1


A middle node | mid(i) = 3*i

A right child node|right(i)=3*i+1

A parent node|parent(i) = (i+1) / 3

2. Implementation of Priority Queue using Heap

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.

2.5.7 Uses of Priority Queue:

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 ex-Schedulers, timers etc.


Heap data structure is best to implement priority queue because in max-heap largest value is present at
root while in min-heap smallest value is present at root. So Max-Heap can be used to implement Max-
Priority Queue and Min-Heap can be used to implement Min-Priority queue.

2.5.8 Operations of the priority queue

Therefore, with the help of heap following operations of the priority queue can be implemented:

a) Insert(): To insert or add new element in the priority queue.


b) PrintMax/PrintMin(): To print or get maximum or minimum element from the priority queue.
c) DeleteMax/DeleteMin(): To delete maximum or minimum element from the priority queue.
d) UpdatePriority(): To increase or decrease the priority of any element in the priority queue.
a) Implementation of Insert operation:
To insert an element in Max Heap.

Following are the steps to to insert an element in Max Heap.

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.

Time Complexity: O(log N) where N is the number of elements

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)

Time Complexity: O(1)

c) Implementation of DeleteMax/DeleteMin():

To delete root element from Max Heap.

Following are the steps to delete an element from Max Heap.

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.

Time Complexity: O(log N) where N is the number of elements in heap.

Advantages of Using heap to implement Priority Queue:

To maintain the priority queue using array, it take O(Nlog N) time while O(log N) using Heap.

a. Smooth Sort algorithm:- This comparison-based algorithm is a variant of Heapsort


algorithm, which is not a stable sort algorithm and requires O(nlog n) but may come closer
to O(n) time if the input would be sorted to some extent.

You might also like