Heap Sort
Heap sort is a sorting algorithm that sorts array elements using a heap data structure, which is similar to a
binary tree data structure. The heap sort algorithm is essential to the data preparation process and can be
used to implement priority queues, which sort items in a data structure based on assigned priority.
Understanding heap sort provides numerous benefits in domains such as data compression, path
planning, robotics and automation.
What Is Heap Sort?
Heap sort is a sorting algorithm that organizes elements in an array using a binary heap. In a max heap,
the algorithm repeatedly swaps the largest element (root node) with the last node from the heap, and re-
inserts the new root into the array into ascending order.
This article will unpack the definition of the heap sort algorithm, including all its operations and how to
implement it in Python.
Binary Trees and Heaps: What Are They?
Heaps are a variation of binary trees with additional structural constraints that make the utilization of
heaps different from that of a typical binary tree, notably in heaps arranged in a particular manner and
pattern. We will explore this and more later in the article. For now, we can understand heaps as binary
trees with some special rules on top of them, which are:
1. Complete binary trees: A binary tree in which all levels are completely filled except the last
one. And if the last level is filled partially, it should be filled from left to right.
2. Max or min: A heap should be either a max or a min heap. In a max heap, every parent,
including the root, should be greater than or equal to its child nodes, so the largest value becomes
the root. In a min heap, each parent node is less than or equal to its child nodes, so the smallest
value of a node is at the root of the tree.
In theory, we could use both a max or a min heap to implement heap sort, but for demonstration
purposes, we’ll only implement it using max heap. You could always play around with the code and try
implementing the algorithm with a min heap as well.
How the Heap Sort Algorithm Works
A max heap provides an efficient way to retrieve the maximum elements from an array. The root of a max
heap is always the largest element since all child nodes must have smaller values. This means retrieving
the maximum element is as simple as popping the root node off the top of the heap. This process can be
repeated until all elements have been removed, resulting in a sorted list.
ER-DSP-HeapSort Page 1
The heap sort algorithm works in six steps. Here’s what you need to know.
6 Steps of a Heap Sort Algorithm
1. Transform the array into a binary tree by inserting each element as a node in a breadth-first
manner.
2. Convert the binary tree into a max heap, ensuring that all parent nodes are greater than or equal
to their child nodes.
3. Swap the root node — the largest element — with the last element in the heap.
4. Call the heapify() function to restore the max heap property.
5. Repeat steps 3 and 4 until the heap is sorted, and exclude the last element from the heap on each
iteration.
6. After each swap and heapify() call, ensure that the max heap property is satisfied.
1. Transform the Array Into a Binary Tree
Suppose we have the following unsorted array of numbers to start with, and we want to sort them using
heap sort:
[12, 11, 31, 3, 5, 7, 9]
To transform the array into a binary tree, we can start by inserting the first element of the array as a node
into the tree and continuing this process in a breadth-first manner until all elements of the array have
been added. This will result in a binary tree with all of the elements of the array, as shown below:
An array sorted into a tree structure . |
ER-DSP-HeapSort Page 2
2. Convert the Binary Tree Into a Max Heap
After inserting all of the elements of the array into a binary tree, the next step is to convert this binary tree
into a max heap. In a max heap, all parent nodes must have values that are greater than or equal to the
values of their children. This will ensure that the highest value is always at the root of the tree. For the tree
above, this means swapping node 12 and node 31 positions in the tree to satisfy the requirements for a
max-heap tree.
Tree converted into max-heap tree . |
max heap = [31, 11, 12, 3, 5, 7, 9]
To verify that the above max heap satisfies all of the necessary properties, you can double-check that it is a
complete binary tree and that there are no parent nodes with values smaller than their child nodes. If
these conditions are met, the max heap is ready to be used in the sorting algorithm.
3. Swap the Root Node With the Last Element in the Heap
The next step in the sorting process is to swap the root node, which contains the largest element, with the
last node in a heap. In other words, you’re swapping the element in the first position of the max-heap
array with the element in the last position of the max-heap array. Whatever method you choose to
understand this step, 31 ends at the end of the array, and nine ends at the first position of the array. This
process is depicted in the image below.
ER-DSP-HeapSort Page 3
Swapping values at last and first position. |
You might have noticed that the last element in the array, holding the value 31, is highlighted in red. This
is because, in the next step, and in future iterations, we will be omitting the last value because it’s in a
sorted position. Therefore, we move forward with the following array into the next step:
[9, 11, 12, 3, 4, 7]
Now, we will transform the array into a tree, then the tree into a max heap. This step is depicted in the
image below:
Binary tree to max heap. |
After swapping the root node with the last element in the heap, the visualized heap will now have one less
element. This is because the largest element has been placed at the end of the array and will be excluded
from future iterations. If it were not excluded, the sorting algorithm would never finish. Therefore, every
time the root node is swapped, the next iteration should exclude the last element in the heap. In the
scenario depicted above, we observe one less node in both the array’s binary tree and max heap
representation.
4. Call the heapify() Function
Let’s now refer to the process of converting the tree or array into a max heap as heapify. This will help with
naming the function in this article’s implementation section.
ER-DSP-HeapSort Page 4
It’s also essential to observe that the tree structure may no longer satisfy the requirements of a max heap
after the root node has been swapped in the example given. The heapify() function should be called again
to restore the max heap property. This will result in the heap being rearranged as shown:
[12, 11, 9, 3, 5, 7]
And again, we swap the values in the first and last position of the max-heap array representation.
Swapping values at last and first position . |
5. Repeat Steps 3 and 4 Until the Heap Is Sorted
You will observe that ‘12’ has now been highlighted red, which implies it has been omitted from future
steps. Another observation is that the elements in red are sorted in ascending order. Once you complete
the steps, all elements should be highlighted in red, indicating omission from the steps and also sorted in
ascending order.
The image below depicts the complete iterations of the sorting process and steps.
ER-DSP-HeapSort Page 5
Heap Sort Step . |
ER-DSP-HeapSort Page 6
Heap sort step.
6. Ensure the Max Heap Property Is Satisfied
The final sorted array:
[3, 5, 7, 9, 11, 12, 31]
Now that you thoroughly understand the heap sort algorithm, we can implement it in Python. There are a
few different ways to do this, but the basic steps will be the same as those outlined in the previous
explanations — transform the array into a binary tree, convert the binary tree into a max heap, swap the
root node with the last element in the heap and repeat the process until the heap is sorted.
ER-DSP-HeapSort Page 7