# Heap Sort for decreasing order using min heap

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}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Prerequisite : Heap sort using min heap.

Algorithm :
1. Build a min heap from the input data.
2. At this point, the smallest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
3. Repeat above steps while size of heap is greater than 1.

Note :Heap Sort using min heap sorts in descending order where as max heap sorts in ascending order

## C++

 `// C++ program for implementation of Heap Sort ` `#include ` `using` `namespace` `std; ` ` `  `// To heapify a subtree rooted with node i which is ` `// an index in arr[]. n is size of heap ` `void` `heapify(``int` `arr[], ``int` `n, ``int` `i) ` `{ ` `    ``int` `smallest = i; ``// Initialize smalles as root ` `    ``int` `l = 2 * i + 1; ``// left = 2*i + 1 ` `    ``int` `r = 2 * i + 2; ``// right = 2*i + 2 ` ` `  `    ``// If left child is smaller than root ` `    ``if` `(l < n && arr[l] < arr[smallest]) ` `        ``smallest = l; ` ` `  `    ``// If right child is smaller than smallest so far ` `    ``if` `(r < n && arr[r] < arr[smallest]) ` `        ``smallest = r; ` ` `  `    ``// If smallest is not root ` `    ``if` `(smallest != i) { ` `        ``swap(arr[i], arr[smallest]); ` ` `  `        ``// Recursively heapify the affected sub-tree ` `        ``heapify(arr, n, smallest); ` `    ``} ` `} ` ` `  `// main function to do heap sort ` `void` `heapSort(``int` `arr[], ``int` `n) ` `{ ` `    ``// Build heap (rearrange array) ` `    ``for` `(``int` `i = n / 2 - 1; i >= 0; i--) ` `        ``heapify(arr, n, i); ` ` `  `    ``// One by one extract an element from heap ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) { ` `        ``// Move current root to end ` `        ``swap(arr, arr[i]); ` ` `  `        ``// call max heapify on the reduced heap ` `        ``heapify(arr, i, 0); ` `    ``} ` `} ` ` `  `/* A utility function to print array of size n */` `void` `printArray(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; ++i) ` `        ``cout << arr[i] << ``" "``; ` `    ``cout << ````" "````; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 6, 3, 2, 9 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``heapSort(arr, n); ` ` `  `    ``cout << ````"Sorted array is "````; ` `    ``printArray(arr, n); ` `} `

## Java

 `// Java program for implementation of Heap Sort ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// To heapify a subtree rooted with node i which is ` `    ``// an index in arr[]. n is size of heap ` `    ``static` `void` `heapify(``int` `arr[], ``int` `n, ``int` `i) ` `    ``{ ` `        ``int` `smallest = i; ``// Initialize smalles as root ` `        ``int` `l = ``2` `* i + ``1``; ``// left = 2*i + 1 ` `        ``int` `r = ``2` `* i + ``2``; ``// right = 2*i + 2 ` ` `  `        ``// If left child is smaller than root ` `        ``if` `(l < n && arr[l] < arr[smallest]) ` `            ``smallest = l; ` ` `  `        ``// If right child is smaller than smallest so far ` `        ``if` `(r < n && arr[r] < arr[smallest]) ` `            ``smallest = r; ` ` `  `        ``// If smallest is not root ` `        ``if` `(smallest != i) { ` `            ``int` `temp = arr[i]; ` `            ``arr[i] = arr[smallest]; ` `            ``arr[smallest] = temp; ` ` `  `            ``// Recursively heapify the affected sub-tree ` `            ``heapify(arr, n, smallest); ` `        ``} ` `    ``} ` ` `  `    ``// main function to do heap sort ` `    ``static` `void` `heapSort(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// Build heap (rearrange array) ` `        ``for` `(``int` `i = n / ``2` `- ``1``; i >= ``0``; i--) ` `            ``heapify(arr, n, i); ` ` `  `        ``// One by one extract an element from heap ` `        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) { ` `             `  `            ``// Move current root to end ` `            ``int` `temp = arr[``0``]; ` `            ``arr[``0``] = arr[i]; ` `            ``arr[i] = temp; ` ` `  `            ``// call max heapify on the reduced heap ` `            ``heapify(arr, i, ``0``); ` `        ``} ` `    ``} ` ` `  `    ``/* A utility function to print array of size n */` `    ``static` `void` `printArray(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < n; ++i) ` `            ``System.out.print(arr[i] + ``" "``); ` `        ``System.out.println(); ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``4``, ``6``, ``3``, ``2``, ``9` `}; ` `        ``int` `n = arr.length; ` ` `  `        ``heapSort(arr, n); ` ` `  `        ``System.out.println(``"Sorted array is "``); ` `        ``printArray(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

/div>

## Python3

# Python3 program for implementation
# of Heap Sort

# To heapify a subtree rooted with
# node i which is an index in arr[].
# n is size of heap
def heapify(arr, n, i):
smallest = i # Initialize smalles as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2

# If left child is smaller than root
if l < n and arr[l] < arr[smallest]: smallest = l # If right child is smaller than # smallest so far if r < n and arr[r] < arr[smallest]: smallest = r # If smallest is not root if smallest != i: (arr[i], arr[smallest]) = (arr[smallest], arr[i]) # Recursively heapify the affected # sub-tree heapify(arr, n, smallest) # main function to do heap sort def heapSort(arr, n): # Build heap (rearrange array) for i in range(int(n / 2) - 1, -1, -1): heapify(arr, n, i) # One by one extract an element # from heap for i in range(n-1, -1, -1): # Move current root to end # arr, arr[i] = arr[i], arr # call max heapify on the reduced heap heapify(arr, i, 0) # A utility function to print # array of size n def printArray(arr, n): for i in range(n): print(arr[i], end = " ") print() # Driver Code if __name__ == '__main__': arr = [4, 6, 3, 2, 9] n = len(arr) heapSort(arr, n) print("Sorted array is ") printArray(arr, n) # This code is contributed by PranchalK [tabby title="C#"]

 `// C# program for implementation of Heap Sort ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// To heapify a subtree rooted with  ` `    ``// node i which is an index in arr[], ` `    ``// n is size of heap ` `    ``static` `void` `heapify(``int``[] arr, ``int` `n, ``int` `i) ` `    ``{ ` `        ``int` `smallest = i; ``// Initialize smalles as root ` `        ``int` `l = 2 * i + 1; ``// left = 2*i + 1 ` `        ``int` `r = 2 * i + 2; ``// right = 2*i + 2 ` ` `  `        ``// If left child is smaller than root ` `        ``if` `(l < n && arr[l] < arr[smallest]) ` `            ``smallest = l; ` ` `  `        ``// If right child is smaller than smallest so far ` `        ``if` `(r < n && arr[r] < arr[smallest]) ` `            ``smallest = r; ` ` `  `        ``// If smallest is not root ` `        ``if` `(smallest != i) { ` `            ``int` `temp = arr[i]; ` `            ``arr[i] = arr[smallest]; ` `            ``arr[smallest] = temp; ` ` `  `            ``// Recursively heapify the affected sub-tree ` `            ``heapify(arr, n, smallest); ` `        ``} ` `    ``} ` ` `  `    ``// main function to do heap sort ` `    ``static` `void` `heapSort(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``// Build heap (rearrange array) ` `        ``for` `(``int` `i = n / 2 - 1; i >= 0; i--) ` `            ``heapify(arr, n, i); ` ` `  `        ``// One by one extract an element from heap ` `        ``for` `(``int` `i = n - 1; i >= 0; i--) { ` `             `  `            ``// Move current root to end ` `            ``int` `temp = arr; ` `            ``arr = arr[i]; ` `            ``arr[i] = temp; ` ` `  `            ``// call max heapify on the reduced heap ` `            ``heapify(arr, i, 0); ` `        ``} ` `    ``} ` ` `  `    ``/* A utility function to print array of size n */` `    ``static` `void` `printArray(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; ++i) ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``Console.WriteLine(); ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 4, 6, 3, 2, 9 }; ` `        ``int` `n = arr.Length; ` ` `  `        ``heapSort(arr, n); ` ` `  `        ``Console.WriteLine(``"Sorted array is "``); ` `        ``printArray(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

Output:

```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)

## tags:

Heap Sorting Heap Sort Sorting Heap