![]() |
VOOZH | about |
Given an array of elements, sort the array in decreasing order using min heap.
Examples:
Input : arr[] = {5, 3, 10, 1}
Output : arr[] = {10, 5, 3, 1}
Input : arr[] = {1, 50, 100, 25}
Output : arr[] = {100, 50, 25, 1}
Prerequisite: Heap sort using min heap.
Note: Heap Sort using min heap sorts in descending order where as max heap sorts in ascending order
Sorted array is 9 6 4 3 2
Time complexity:It takes O(logn) for heapify and O(n) for constructing a heap. Hence, the overall time complexity of heap sort using min heap or max heap is O(nlogn)
Space complexity: O(n) for call stack
9 6 4 3 2
Time Complexity: O(n log n), where n is the number of elements in the array.
Auxiliary Space: O(1), because it sort the array in place without using extra space that depends on the input size .