C++ Program for Heap Sort Last Updated : 15 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element. Recommended PracticeHeap SortTry It! Implementation: CPP // C++ program for implementation of Heap Sort #include <iostream> 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 largest = i; // Initialize largest as root Since we are using 0 based indexing int l = 2 * i + 1; // left = 2*i + 1 int r = 2 * i + 2; // right = 2*i + 2 // If left child is larger than root if (l < n && arr[l] > arr[largest]) largest = l; // If right child is larger than largest so far if (r < n && arr[r] > arr[largest]) largest = r; // If largest is not root if (largest != i) { swap(arr[i], arr[largest]); // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } // 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[0], 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 << "\n"; } // Driver program int main() { int arr[] = { 60 ,20 ,40 ,70, 30, 10}; int n = sizeof(arr) / sizeof(arr[0]); //heapify algorithm // the loop must go reverse you will get after analyzing manually // (i=n/2 -1) because other nodes/ ele's are leaf nodes // (i=n/2 -1) for 0 based indexing // (i=n/2) for 1 based indexing for(int i=n/2 -1;i>=0;i--){ heapify(arr,n,i); } cout << "After heapifying array is \n"; printArray(arr, n); heapSort(arr, n); cout << "Sorted array is \n"; printArray(arr, n); return 0; } //code by Prajwal Chougale OutputAfter heapifying array is 70 60 40 20 30 10 Sorted array is 10 20 30 40 60 70 Time complexity : O(N*logN)Auxiliary space: O(1) Approach Name: Heap Sort (Using STL) Steps: Convert the input array to a vector.Convert the vector into a Max Heap using the make_heap function of the STL.Sort the Max Heap using the sort_heap function of the STL. C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; void heapSort(int arr[], int n) { // Convert array to vector vector<int> v(arr, arr + n); // Convert vector to Max Heap make_heap(v.begin(), v.end()); // Sort Max Heap sort_heap(v.begin(), v.end()); // Copy sorted vector back to array copy(v.begin(), v.end(), arr); } int main() { int arr[] = { 60, 20, 40, 70, 30, 10 }; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); cout << "Sorted array is \n"; for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } OutputSorted array is 10 20 30 40 60 70 Time Complexity: O(n*log(n)) in all cases.Auxiliary Space: O(1) in-place sorting algorithm. Please refer complete article on Heap Sort for more details! Comment More infoAdvertise with us Next Article sort_heap function in C++ kartik Follow Improve Article Tags : Sorting Heap C++ Programs DSA Heap Sort +1 More Practice Tags : HeapSorting Similar Reads Heap Sort - Data Structures and Algorithms Tutorials Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use 14 min read Iterative HeapSort HeapSort is a comparison-based sorting technique where we first build Max Heap and then swap the root element with the last element (size times) and maintains the heap property each time to finally make it sorted. Examples: Input : 10 20 15 17 9 21 Output : 9 10 15 17 20 21 Input: 12 11 13 5 6 7 15 11 min read Java Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where first find the maximum element and place it at the end. We repeat the same process for the remaining element. Heap Sort in JavaBelow is the implementation of Heap Sort 3 min read C++ Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element. Recommended PracticeHeap SortTry It! 3 min read sort_heap function in C++ The sort_heap( ) is an STL algorithm which sorts a heap within the range specified by start and end. Sorts the elements in the heap range [start, end) into ascending order. The second form allows you to specify a comparison function that determines when one element is less than another. Defined in h 3 min read Heap Sort - Python Heapsort is a comparison-based sorting technique based on a Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element.Heap Sort AlgorithmFirst convert the array in 4 min read Lexicographical ordering using Heap Sort Given an array arr[] of strings. The task is to sort the array in lexicographical order using Heap Sort.Examples: Input: arr[] = { "banana", "apple", "mango", "pineapple", "orange" } Output: apple banana mango orange pineappleInput: arr[] = { "CAB", "ACB", "ABC", "CBA", "BAC" } Output: ABC, ACB, BAC 10 min read Heap sort for Linked List Given a linked list, the task is to sort the linked list using HeapSort. Examples: Input: list = 7 -> 698147078 -> 1123629290 -> 1849873707 -> 1608878378 -> 140264035 -> -1206302000Output: -1206302000 -> 7 -> 140264035 -> 1123629290 -> 1608878378 -> 1698147078 ->1 14 min read Python Code for time Complexity plot of Heap Sort Prerequisite : HeapSort Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element. We implement Heap Sort he 3 min read Sorting algorithm visualization : Heap Sort An algorithm like Heap sort can be understood easily by visualizing. In this article, a program that visualizes the Heap Sort Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach: Generate random array and fill the pygame window wi 4 min read Like