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

Sorting Algorithm Part - 7

Uploaded by

Rangita Shah
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)
9 views14 pages

Sorting Algorithm Part - 7

Uploaded by

Rangita Shah
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

Home Whiteboard Graphing Calculator Online Compilers Articles Tool

Home Data_structures_algorithms Heap Sort Algorithm

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?

Heapify Method

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.

[Link] 1/14
Page 2 of 14

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

Building a heap using the BUILD-MAX-HEAP algorithm from the input array −

[Link] 2/14
Page 3 of 14

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

[Link] 3/14
Page 4 of 14

[Link] 4/14
Page 5 of 14

[Link] 5/14
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.

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.

[Link] 6/14
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.

[Link] 7/14
Page 8 of 14

12 is popped and replaced with 10.

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

[Link] 8/14
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.

[Link] 9/14
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

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

[Link] 10/14
Page 11 of 14

C C++ Java Python

#include <stdio.h>
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);
}
printf("Heap array: ");
for (i = 0; i < n; i++)
printf("%d ", 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
heap[j] = temp;
root = 0;
do {
c = 2 * root + 1; // left node of root element
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;

[Link] 11/14
Page 12 of 14

} while (c < j);


}
printf("\nThe sorted array is: ");

for (i = 0; i < n; i++)


printf("%d ", 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);
}

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

PHP Tutorial

R Tutorial
HTML Tutorial

CSS Tutorial

JavaScript Tutorial
SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial

Microsoft Azure Tutorial

[Link] 12/14
Page 13 of 14

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

OnlineChapters
Java Compiler Categories

Online Python Compiler


Online Go Compiler

Online C Compiler

Online C++ Compiler


Online C# Compiler

Online PHP Compiler

Online MATLAB Compiler


Online Bash Terminal

Online SQL Compiler

Online Html Editor

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

[Link] 13/14
Page 14 of 14

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 2026. All Rights Reserved.

[Link] 14/14

You might also like