Algorithm/Insights
Heap sort is an in-place comparison sort.
Heap sort is not a stable sort i.e. it does not ensure the order of same array elements after sorting.
Definition:
A max-heap is a complete binary tree in which the value at root is greater than or equal to the values of left and right children of the root and all the heap nodes follow this property.
Since a max heap is a complete binary tree, it can be represented by an array such that:
1. Element at index 0 is the root.
2. For any node of the heap at index i, including the root node (i=0):
Index of Left child = 2*i+1
Index of Right child = 2*i+2
Parent of the node = (i-1)/2
Heap sort algorithm has two steps:
Step 1. Build Max Heap: Convert the array to a max heap.
Call maxHeapify on all internal nodes. For this we start with the last internal node upto the root. The index of the last internal node will be the index of parent of last leaf node which is also the last element in the array.
maxHeapify(array, i, heapSize) - Get larger of current node (element at index i), left child (element at index 2*i+1) and
right child (element at index 2*i+2) and replace it with the current node (index i).
Recurse for left and right nodes to maintain max heap property.
Step 2: Sorting the array using Max Heap
1. Remove the maximum element from the heap (root) and swap it with the last index of the heap (heapSize) and reduce the heap size by 1.
2. Then adjust heap elements (from index 0 to heap size) to maintain max heap property.
3. Repeat this step for second largest element and so on.
For Example, consider the array:
12, 35, 87, 26, 9, 28, 7
Step 1: Build Max Heap:
1. The array can be considered as following complete binary tree:
2. Clearly,it is not a max heap. We convert it to a max heap by calling maxHeapify on all internal nodes.
Last internal node
= index of parent of last element of the array
= index of parent of 7 (Index of 7 is 6)
= (6 - 1)/2
= 2
= index of 87
There will be no change in the array on calling maxHeapify(array, 2, heapSize).
Similarly, subtree with root as 35 is also the largest of its children, i.e. satisfies max heap property.
So, there will be no change in the array on calling maxHeapify(array, 1, heapSize) too.
Next we call, maxHeapify(array, 0, heapSize):
Hence following image shows the max heap created from the array.
Step 2: Sorting the array using Max Heap created in Step 1
1. Swap max element of the heap (i.e. the root) with the element at last index of the heap.
2. Call maxHeapify on the root (index = 0) to maintain max heap property on the remaining heap. Proceeding like before, we get the following heap:
3. Next swap max element of the heap (root) with the element at last index of the heap.
4. Again, call maxHeapify on the root (index = 0) to maintain max heap property on the remaining heap.
In the next step, 28 (root) will be swapped with 9 (last element in heap) moving 28 to its correct position in the sorted array.
Continuing like this, we can see that the array is getting sorted from max element to smallest element using the max heap.
After applying #1 and #2 of step 2 on the array till heapSize becomes 0, we will get the sorted array as:
[7, 9, 12, 26, 28, 35, 87]