
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Heap Sort for Decreasing Order Using Min Heap
Heap Sort ? Heap sort is a comparison-based algorithm that used binary tree data structures to sort a list of numbers in increasing or decreasing order. It heap sorts to create a heap data structure where the root is the smallest element and then removes the root and again sorting gives the second smallest number in the list at the root position.
Min Heap ? Min heap is a data structure where the parent node is always smaller than the child node, thus root node is the smallest element among all.
Problem Statement
Given an array of integers. Sort them in decreasing order using min heap.
Sample Example 1
Input: [2, 5, 1, 7, 0]
Output: [7, 5, 2, 1, 0]
Sample Example 2
Input: [55, 1, 23, 10, 1]
Output: [55, 23, 10, 1, 1]
Approach 1
To perform heap sort in decreasing order using min heap, we create a min heap of the elements and extract them one at a time to get an array in decreasing order by reversing the order.
Pseudocode
procedure heapSort (arr[], n) Initialize priority queue: minHeap for i = 1 to n add arr[i] to minHeap i = n - 1 while minHeap is not empty arr[i-] = top element of minHeap Remove the top element of minHeap end procedure
Example: C++ Implementation
In the following program, we use a min heap to sort an array and then reverse the order to get the result.
#include <bits/stdc++.h> using namespace std; // Function to heap sort in decreasing order using min heap void heapSort(int arr[], int n){ // Creating min heap using a priority queue priority_queue<int, vector<int>, greater<int> > minHeap; // Inserting input array to min heap for (int i = 0; i < n; i++){ minHeap.push(arr[i]); } // Iterating backwards in the input array, where each element is replaced by the smallest element extracted from min heap int i = n - 1; while (!minHeap.empty()){ arr[i--] = minHeap.top(); minHeap.pop(); } } int main(){ int arr[6] = {5, 2, 9, 1, 5, 6}; int n = 6; heapSort(arr, n); cout << "Sorted array : "; for (int i = 0; i < n; i++){ cout << arr[i] << " "; } cout << endl; return 0; }
Output
Sorted array : 9 6 5 5 2 1
Time Complexity ? O(nlogn)
Space Complexity ? O(n)
Approach 2
Another solution to the problem can be to build a min heap starting with the last non-leaf root mode and working backwards. Then we can sort the array by swapping the root node with the last leaf node and then restoring the min-heap property.
Pseudocode
procedure heapify (arr[], n , i) smallest = i l = 2i + 1 r = 2i + 2 if l < n and arr[l] < arr[smallest] smallest = l end if if r < n and arr[r] < arr[samllest] smallest = r end if if smallest is not i swap arr[i] to arr[smallest] heapify (arr, n, smallest) end if end procedure procedure heapSort (arr[], n) for i = n/2 - 1 to 0 heapify(arr, n, i) for i = n-1 to 0 swap arr[0] to arr[i] heapify (arr, i, 0) end procedure
Example: C++ Implementation
In the following program, we use heapify() function that restores the min-heap property of the subtree rooted at index i and heapSort() builds min heap in reverse order.
#include <bits/stdc++.h> using namespace std; // Restores the min heap property of subtree rooted at index i void heapify(int arr[], int n, int i){ int smallest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l] < arr[smallest]){ smallest = l; } if (r < n && arr[r] < arr[smallest]){ smallest = r; } if (smallest != i){ swap(arr[i], arr[smallest]); heapify(arr, n, smallest); } } void heapSort(int arr[], int n){ // Build the min heap in reverse order for (int i = n / 2 - 1; i >= 0; i--){ heapify(arr, n, i); } // Sort the array by repeatedly swapping the root node with the last leaf node for (int i = n - 1; i >= 0; i--){ swap(arr[0], arr[i]); heapify(arr, i, 0); } } int main(){ int arr[6] = {5, 2, 9, 1, 5, 6}; int n = 6; heapSort(arr, n); cout << "Sorted array : "; for (int i = 0; i < n; i++){ cout << arr[i] << " "; } cout << endl; return 0; }
Output
Sorted array : 9 6 5 5 2 1
Using the previous approach of using heapSort() function to create a min heap, we can use the same approach in this solution but rather than using heapify to restore property of min heap, we use a traditional hep sort algorithm to crete amin heap and sor telement sin increasing order which are further reversed to get the desired output.
Pseudocode
procedure heapSort (arr[], n) for i = n/2 - 1 to 0 parent = i while parent *2+1 < n child = parent*2+1 if child+1 < n and arr[child] >arr[child+1] child = child + 1 end if if arr[parent] > arr[child] swap arr[parent] to arr[child] parent = child else break end if for i = n-1 to 0 swap arr[0] to arr[i] parent = 0 while parent*2+1 < i child = parent*2+1 if child+1 < n and arr[child] >arr[child+1] child = child + 1 end if if arr[parent] > arr[child] swap arr[parent] to arr[child] parent = child else break end if end procedure
Example: C++ Implentation
In the following program, we modify heap sort algorithm to sort array in decreasing order.
#include <bits/stdc++.h> using namespace std; void heapSort(int arr[], int n){ // Building min heap in reverse order for (int i = n / 2 - 1; i >= 0; i--) { // Starting from last parent node, apply heapify operation int parent = i; while (parent * 2 + 1 < n) { int child = parent * 2 + 1; if (child + 1 < n && arr[child] > arr[child + 1]){ child++; } if (arr[parent] > arr[child]){ swap(arr[parent], arr[child]); parent = child; } else{ break; } } } // Extarct elekemnhts form min heap in decreasing order for (int i = n - 1; i > 0; i--){ swap(arr[0], arr[i]); int parent = 0; // Perform heapify operation at new root node while (parent * 2 + 1 < i){ int child = parent * 2 + 1; if (child + 1 < i && arr[child] > arr[child + 1]){ child++; } if (arr[parent] > arr[child]){ swap(arr[parent], arr[child]); parent = child; } else { break; } } } } int main(){ int arr[6] = {5, 2, 9, 1, 5, 6}; int n = 6; heapSort(arr, n); cout << "Sorted array : "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output
Sorted array : 9 6 5 5 2 1
Conclusion
In conclusion, for performing heap sort for decreasing order using min heap, we can use several approaches, a few of which are mentione dabove having time complexities of O(nlogn) and varying space complexity for each approach.