Skip to main content

Heap Sort

This page provides introduction to Heap Sort.

Overview

Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort elements in ascending or descending order.

In heap sort, we need to sort non-leaf nodes first because the heap property is established and maintained through the non-leaf nodes. The heap property states that for any given node i, the value of i is either greater than (in a max heap) or less than (in a min heap) the values of its children.

Let's sort array below, with heap sort:

[70,45,75,90,80,24,2][70, 45, 75, 90, 80, 24, 2]

To start the heapification process, we need to identify the first non-leaf node. We can find its index using the following formula:

N/21\text{N} / 2 - 1

Where N\text{N} is total number of elements in an array.

Above formula gives us first non-leaf node index as 22. Let's proceed to heapify index 22.

heap-1.svg

Since node at index 22 already satisfies the heap property(i.e. value of node at index 22 is greater than both it's child node's value), we will heapify next index 11.

heap-sort-2.svg

After heapifying index =1=1, tree will look like below, and recurssive heapification process now moves to index 33, where no additional heapification is required.

heap-sort-3.svg

Now, let's move on to heapification of last non-leaf node at index =0=0 as below:

heap-sort-4.svg

After heapifying at index =0=0, tree will look like below and recurssive heapification process now moves to index 11, where additional heapification is required.

heap-sort-5.svg

After recursive heapifying at index =1=1, tree will look like below:

heap-sort-6.svg

This concludes heapification of non-leaf node.

The next step involves recursive for each node starting from last node. Steps are as below:

  • Replacing the value at the root node at index 00 with the value of the last node.
  • Reducing the heap size by 1, i.e. that node is already sorted.
  • Heapifying the reduced heap from the root node at index 00 downwards.

So let's start with replacing last node's value with root node's value and recursive heapification process excluding the last node.

  • Replacing the value at the root node at index 00 with the value of the last node.
  • Reducing the heap size by 1, i.e. that node is already sorted.
heap-sort-7.svg
  • Heapifying the reduced heap from the root node at index 00 downwards.
heap-sort-8.svg
heap-sort-9.svg

Let's continue the same process for index 5,4,3,2,15, 4, 3, 2, 1, below are the snapshots of tree:

  • Running the process for index 55.
heap-sort-10.svg
heap-sort-11.svg
info

In the above image 9090 or 8080 didn't replace 2424 because it is already sorted and not part of the heapification process.

  • Running the process for index 44.
heap-sort-12.svg
heap-sort-13.svg
heap-sort-14.svg
  • Running the process for index 33.
heap-sort-15.svg
heap-sort-16.svg
info

In the above image 7070 or 7575 didn't replace 22 because it is already sorted and not part of the heapification process.

  • Running the process for index 22.
heap-sort-17.svg
  • Running the process for index 11.
heap-sort-18.svg
public void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if(left < n && arr[left] > arr[largest]) {
largest = left;
}
if(right < n && arr[right] > arr[largest]) {
largest = right;
}
if(i != largest) {
int temp = arr[largest];
arr[largest] = arr[i];
arr[i] = temp;
heapify(arr, n, largest);
}
}

public void heapSort(int[] arr) {
int n = arr.length;

// heapify non-leaf node
for(int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}

// sort
for(int i = n - 1; i >= 0; i--) {
heapify(arr, i, 0);
}
}

Complexity

Let's say there are N\text{N} elements in an array.

Time Complexity

Building heap takes O(log N)\text{O(log N)} time for each element, so total time complexity is:

O(N log N)\text{O(N log N)}

Space Complexity

Algorithm takes no additional space.

O(1)\text{O(1)}