Heap sort is a comparison-based sorting technique that uses the binary heap data structure. It is similar in spirit to selection sort: repeatedly select the largest (or smallest) remaining element and place it in its final position. Heap sort first builds a heap from the input, then repeatedly removes the root (the maximum for a max-heap) and restores the heap property on the remaining elements.
Complete binary tree: a binary tree in which every level, except possibly the last, is completely filled and all nodes are as far left as possible.
Binary heap: a complete binary tree with a special ordering between parent and children. In a max-heap, every parent node has a value greater than or equal to its children. In a min-heap, every parent node has a value less than or equal to its children. Heaps are most commonly implemented using an array because a complete binary tree can be compactly stored in contiguous memory.
With 0-based array indexing, if a node is at index i then its left child is at index 2*i + 1 and its right child is at index 2*i + 2. The parent of node at index i is at index (i-1)/2 (integer division).
Heapify is a procedure that, given an index i in an array and the size n of the heap, ensures that the subtree rooted at i satisfies the heap property, assuming its children already do. Because heapify relies on children already being heaps, building the heap is done in a bottom-up order: call heapify for indices n/2 - 1 down to 0. The index n/2 - 1 is the last non-leaf node in a complete binary tree of size n.
Input array: 4, 10, 3, 5, 1
Array indices (0-based):
4(0) -- root
/ \
10(1) 3(2)
/ \
5(3) 1(4)
Apply heapify starting from index 1, then index 0.
Heapify at index 1:
Before: 4 10 3 5 1
At index 1, children are at indices 3 and 4 (values 5 and 1).
10 is already larger than its children, so no change.
Heapify at index 0:
Before heapify at 0: 4 10 3 5 1
Compare 4 with children 10 and 3; largest is 10 (index 1).
Swap 4 and 10: 10 4 3 5 1
Now heapify subtree rooted at index 1:
Compare 4 with children 5 and 1; largest is 5 (index 3).
Swap 4 and 5: 10 5 3 4 1
Resulting max-heap: 10 5 3 4 1
Now perform extraction steps to sort:
Swap root and last: 1 5 3 4 10
Heap size becomes 4; heapify root:
Compare 1 with children 5 and 3; swap with 5 -> 5 1 3 4 10
Heapify index 1: compare 1 with child 4; swap -> 5 4 3 1 10
Swap root and last (index 3): 1 4 3 5 10
Heap size becomes 3; heapify root:
Compare 1 with children 4 and 3; swap with 4 -> 4 1 3 5 10
Heapify index 1: no children within heap size 3
Swap root and last (index 2): 3 1 4 5 10
Heap size becomes 2; heapify root:
Compare 3 with child 1; already larger -> 3 1 4 5 10
Swap root and last (index 1): 1 3 4 5 10
Heap size becomes 1 -> sorted array in increasing order: 1 3 4 5 10
Heapify (max-heap) pseudocode idea:
Given array arr, size n, index i:
largest = i
l = 2*i + 1
r = 2*i + 2
if l < n="" and="" arr[l]=""> arr[largest]:
largest = l
if r < n="" and="" arr[r]=""> arr[largest]:
largest = r
if largest != i:
swap arr[i] and arr[largest]
heapify(arr, n, largest)
Build-heap: call heapify(arr, n, i) for i from n/2 - 1 down to 0.
Although heap sort has good worst-case guarantees, in practice quicksort and merge sort are often preferred:
The following implementations perform heap sort by building a max-heap and then repeatedly extracting the maximum. They follow the same logical structure; the detailed syntax varies by language.
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
// main function to do heap sort
void heapSort(int arr[], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
swap(arr[0], arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << "\
";
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
cout << "Sorted array is \
";
printArray(arr, n);
}
// Java program for implementation of Heap Sort
public class HeapSort {
public void sort(int arr[])
{
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver code
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = arr.length;
HeapSort ob = new HeapSort();
ob.sort(arr);
System.out.println("Sorted array is");
printArray(arr);
}
}
# Python program for implementation of heap Sort
# To heapify subtree rooted at index i.
# n is size of heap
def heapify(arr, n, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
# See if left child of root exists and is
# greater than root
if l < n and arr[largest] < arr[l]:
largest = l
# See if right child of root exists and is
# greater than root
if r < n and arr[largest] < arr[r]:
largest = r
# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
# Heapify the root.
heapify(arr, n, largest)
# The main function to sort an array of given size
def heapSort(arr):
n = len(arr)
# Build a maxheap.
for i in range(n//2 - 1, -1, -1):
heapify(arr, n, i)
# One by one extract elements
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)
# Driver code
arr = [12, 11, 13, 5, 6, 7]
heapSort(arr)
print("Sorted array is")
for x in arr:
print(x, end=' ')
# This code is contributed by Mohit Kumra
// C# program for implementation of Heap Sort
using System;
public class HeapSort {
public void sort(int[] arr)
{
int n = arr.Length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
void heapify(int[] arr, int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
static void printArray(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver code
public static void Main()
{
int[] arr = { 12, 11, 13, 5, 6, 7 };
int n = arr.Length;
HeapSort ob = new HeapSort();
ob.sort(arr);
Console.WriteLine("Sorted array is");
printArray(arr);
}
}
// This code is contributed by Akanksha Rai(Abby_akku)
<?php
// PHP program for implementation of Heap Sort
function heapify(&$arr, $n, $i)
{
$largest = $i; // Initialize largest as root
$l = 2*$i + 1; // left = 2*i + 1
$r = 2*$i + 2; // right = 2*i + 2
if ($l < $n && $arr[$l] > $arr[$largest])
$largest = $l;
if ($r < $n && $arr[$r] > $arr[$largest])
$largest = $r;
if ($largest != $i) {
$swap = $arr[$i];
$arr[$i] = $arr[$largest];
$arr[$largest] = $swap;
heapify($arr, $n, $largest);
}
}
function heapSort(&$arr, $n)
{
for ($i = (int)($n / 2) - 1; $i >= 0; $i--)
heapify($arr, $n, $i);
for ($i = $n - 1; $i > 0; $i--) {
$temp = $arr[0];
$arr[0] = $arr[$i];
$arr[$i] = $temp;
heapify($arr, $i, 0);
}
}
function printArray(&$arr, $n)
{
for ($i = 0; $i < $n; ++$i)
echo ($arr[$i]." ");
}
$arr = array(12, 11, 13, 5, 6, 7);
$n = sizeof($arr)/sizeof($arr[0]);
heapSort($arr, $n);
echo 'Sorted array is ' . "\
";
printArray($arr , $n);
?>
<script>
// JavaScript program for implementation of Heap Sort
function heapify(arr, n, i) {
var largest = i; // Initialize largest as root
var l = 2 * i + 1; // left = 2*i + 1
var r = 2 * i + 2; // right = 2*i + 2
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
var swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
function sort(arr) {
var n = arr.length;
for (var i = Math.floor(n / 2) - 1; i >= 0; i--)
heapify(arr, n, i);
for (var i = n - 1; i > 0; i--) {
var temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
function printArray(arr) {
for (var i = 0; i < arr.length; ++i)
document.write(arr[i] + " ");
}
var arr = [ 12, 11, 13, 5, 6, 7 ];
sort(arr);
document.write( "Sorted array is <br>" );
printArray(arr);
</script>
Sorted array is
5 6 7 11 12 13






QuickSort, Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Radix Sort, Counting Sort, Bucket Sort, Shell Sort, Comb Sort, Pigeonhole Sort
|
81 videos|115 docs|33 tests
|