0% found this document useful (0 votes)
18 views14 pages

Heap Sort Algorithm Explained

Uploaded by

sharafatbaluch
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)
18 views14 pages

Heap Sort Algorithm Explained

Uploaded by

sharafatbaluch
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

Page 1 of 14

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

Heap Sort Algorithm

Heap Sort is an efficient sorting technique based on the heap data structure.

The heap is a nearly-complete binary tree where the parent node could either be minimum or
maximum. The heap with minimum root node is called min-heap and the root node with maximum
root node is called max-heap. The elements in the input data of the heap sort algorithm are processed
using these two methods.

The heap sort algorithm follows two main operations in this procedure −

Builds a heap H from the input data using the heapify (explained further into the chapter)
method, based on the way of sorting ascending order or descending order.

Deletes the root element of the root element and repeats until all the input elements are
processed.

The heap sort algorithm heavily depends upon the heapify method of the binary tree. So what is this
heapify method?Powered by:

Heapify Method
Page 2 of 14

The heapify method of a binary tree is to convert the tree into a heap data structure. This method uses
recursion approach to heapify all the nodes of the binary tree.

Note − The binary tree must always be a complete binary tree as it must have two children nodes
always.

The complete binary tree will be converted into either a max-heap or a min-heap by applying the
heapify method.

To know more about the heapify algorithm, please click here.

Heap Sort Algorithm

As described in the algorithm below, the sorting algorithm first constructs the heap ADT by calling the
Build-Max-Heap algorithm and removes the root element to swap it with the minimum valued node at
the leaf. Then the heapify method is applied to rearrange the elements accordingly.

Algorithm: Heapsort(A)
BUILD-MAX-HEAP(A)
for i = [Link] downto 2
exchange A[1] with A[i]
[Link]-size = [Link]-size - 1
MAX-HEAPIFY(A, 1)

Analysis

The heap sort algorithm is the combination of two other sorting algorithms: insertion sort and merge
sort.

The similarities with insertion sort include that only a constant number of array elements are stored
outside the input array at any time.

The time complexity of the heap sort algorithm is O(nlogn), similar to merge sort.

Example

Let us look at an example array to understand the sort algorithm better −

12 3 9 14 10 18 8 23

Powered
Building a heap using by:BUILD-MAX-HEAP algorithm from the input array −
the
Page 3 of 14

Rearrange the obtained binary tree by exchanging the nodes such that a heap data structure is
formed.

Powered by:
Page 4 of 14

Powered by:
Page 5 of 14

Powered by:
Page 6 of 14

The Heapify Algorithm

Applying the heapify method, remove the root node from the heap and replace it with the next
immediate maximum valued child of the root.
Powered by:

The root node is 23, so 23 is popped and 18 is made the next root because it is the next maximum
node in the heap.
Page 7 of 14

Now, 18 is popped after 23 which is replaced by 14.

The current root 14 is popped from the heap and is replaced by 12.

Powered by:
Page 8 of 14

12 is popped and replaced with 10.

Similarly all the other elements are popped using the same process.

Powered by:
Page 9 of 14

Here the current root element 9 is popped and the elements 8 and 3 are remained in the tree.

Then, 8 will be popped leaving 3 in the tree.

Powered by:
Page 10 of 14

After completing the heap sort operation on the given heap, the sorted elements are displayed as
shown below −

Every time an element is popped, it is added at the beginning of the output array since the heap data
structure formed is a max-heap. But if the heapify method converts the binary tree to the min-heap,
add the popped elements are on the end of the output array.

The final sorted list is,

3 8 9 10 12 14 18 23

Powered by:

Implementation
Page 11 of 14

The logic applied on the implementation of the heap sort is: firstly, the heap data structure is built
based on the max-heap property where the parent nodes must have greater values than the child
nodes. Then the root node is popped from the heap and the next maximum node on the heap is
shifted to the root. The process is continued iteratively until the heap is empty.

In this tutorial, we show the heap sort implementation in four different programming languages.

C C++ Java Python

Open Compiler

#include <iostream>
using namespace std;
void heapify(int[], int);
void build_maxheap(int heap[], int n){
int i, j, c, r, t;
for (i = 1; i < n; i++) {
c = i;
do {
r = (c - 1) / 2;
if (heap[r] < heap[c]) { // to create MAX heap array
t = heap[r];
heap[r] = heap[c];
heap[c] = t;
}
c = r;
} while (c != 0);
}
cout << "Heap array: ";
for (i = 0; i < n; i++)
cout <<heap[i]<<" ";
heapify(heap, n);
}
void heapify(int heap[], int n){
int i, j, c, root, temp;
for (j = n - 1; j >= 0; j--) {
temp = heap[0];
heap[0] = heap[j]; // swap max element with rightmost leaf
element
Powered by:
heap[j] = temp;
root = 0;
do {
c = 2 * root + 1; // left node of root element
Page 12 of 14

if ((heap[c] < heap[c + 1]) && c < j-1)


c++;
if (heap[root]<heap[c] && c<j) { // again rearrange to max
heap array
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
cout << "\nThe sorted array is : ";
for (i = 0; i < n; i++)
cout <<heap[i]<<" ";
}
int main(){
int n, i, j, c, root, temp;
n = 5;
int heap[10] = {2, 3, 1, 0, 4}; // initialize the array
build_maxheap(heap, n);
return 0;
}

Output

Heap array: 4 3 1 0 2
The sorted array is : 0 1 2 3 4

TOP TUTORIALS

Python Tutorial

Java Tutorial

C++ Tutorial

C Programming Tutorial

C# Tutorial Powered by:

PHP Tutorial

R Tutorial

HTML Tutorial
Page 13 of 14

CSS Tutorial

JavaScript Tutorial

SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial

Amazon Web Services Tutorial

Microsoft Azure Tutorial

Git Tutorial

Ethical Hacking Tutorial

Docker Tutorial

Kubernetes Tutorial

DSA Tutorial

Spring Boot Tutorial

SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification

Data Science Advanced Certification

Cloud Computing And DevOps

Advanced Certification In Business Analytics

Artificial Intelligence And Machine Learning

DevOps Certification

Game Development Certification

Front-End Developer Certification

AWS Certification Training

Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Powered by:
Online Python Compiler

Online Go Compiler

Online C Compiler

Online C++ Compiler


Page 14 of 14

Online C# Compiler

Online PHP Compiler

Online MATLAB Compiler

Online Bash Compiler

Online SQL Compiler

Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical
and non-technical subjects.

© Copyright 2025. All Rights Reserved.

Powered by:

You might also like