We looked at the construction of the min-heap tree from an already constructed tree. Min-heap can also be constructed directly from an array. If we look at the array that we used in the Creating a Heap from an Array article, we’ll see how easy it is to construct a min-heap from the array. We start by adding the first node, 7. Heaps are built from left to right. The next element that’s added is 2. Since we’re constructing a min-heap, all the children nodes must be smaller than the parent node. Number 2 is smaller than 7, so the two nodes are swapped. Next, element 9 is
Tag: Heap Sort
Visually Explained Algorithms Now that we know how to create a max-heap, we’ll move into sorting an array of values using heap sort. To sort the array using heap sort, we’ll keep creating max-heaps. Each time a max-heap is created, the root is removed and is considered sorted. Let’s start with the following example. The first step is to construct a tree from the array. We’ll follow the same procedure as outlined in my other article, Constructing Max-Heap from a Tree. To begin, 5 is swapped with 9 and then 5 is swapped with 7 to generate a max-heap. Considering that the