HeapSort implementation in Java/C++ With Examples

Heapsort is a sorting algorithm that uses Binary heaps for the purpose of sorting. Heaps can be of two types, min-heap and max heap.

Heapsort is an improved version of selection sort.

For this tutorial we will be using max heap for implementing Heapsort.

Max heap follows the property that the parent element is always greater than its children. That means in a max heap the root is always the maximum of all the elements. Heapsort uses this property to arrange elements in a sorted manner.

This tutorial assumes that you have a basic understanding of heaps. In case you want to learn about heaps, you can go over this tutorial on heaps and its implementation. (add link)


The algorithm for Heapsort is as follows :

  1. Build a max heap from the input array.
  2. Replace root (maximum) with the last item of the heap.
  3. Reduce the size of the heap by 1.
  4. Heapify the root of the tree by calling DownHeapify
  5. Repeat step 2 to 4 while the size of the heap is greater than 1.

At each iteration, we are taking the maximum element and placing it at the end of the heap. Then we are reducing the size of the heap. As we carry on this process, we are sorting the array from the end.

We will be calling the function downheapify( ) to make sure that the heap property is satisfied after reducing the size.

HeapSort Implementation

To heapify the array we use a recursive function that moves from the root towards the leaf nodes. This function at each node compares the parent to its children. If the value of parent node is less than its children nodes, it swaps the values. This swap makes sure that the max heap property is satisfied.

After performing the swap if further calls the downheapify function on the children.

This function needs to be called every time an element is added or deleted from the heap.

In a heap the position of children are given by :

The code for downheapify is as follows :

To sort an array, we first need to create a heap out of it. This is done by the following lines of code :

We only need to consider non-leaf nodes while calling downheapify, since leaf nodes already satisfy the heap property.

The next step is to extract an element from the root position and swap it with the last position of the heap. Once this is done, we reduce the size of the heap and again call downheapify. The lines of code that do this are :

The complete code for sort function is :

Apart from these two functions, we need a function to print the array as well.

The print function is as follows :

Using these three functions we can successfully implement Heapsort.

Complete Code

The complete code for Heapsort is as follows :

1. Heap Sort in Java

2. Heap Sort in C++


This is how you can implement Heapsort in Java/C++. The time complexity of Heapsort is O(log(n)). Heapsort is a better sorting algorithm as compared to bubble sort, insertion sort, selection sort and even quick sort.

By admin

Leave a Reply

%d bloggers like this: