C++ Online Compiler
Example: C++ Heap Sort Implementation in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ Heap Sort Implementation #include <iostream> #include <vector> #include <algorithm> // Required for std::swap // Function to heapify a subtree rooted with node i // n is the size of the heap void heapify(std::vector<int>& arr, int n, int i) { int largest = i; // Initialize largest as root int left = 2 * i + 1; // left child int right = 2 * i + 2; // right child // If left child is larger than root if (left < n && arr[left] > arr[largest]) { largest = left; } // If right child is larger than current largest if (right < n && arr[right] > arr[largest]) { largest = right; } // If largest is not root if (largest != i) { std::swap(arr[i], arr[largest]); // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } // Main function to perform Heap Sort void heapSort(std::vector<int>& arr) { int n = arr.size(); // Step 1: Build a max-heap (rearrange array) // Start from the last non-leaf node and heapify upwards. for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); } // Step 2: Extract elements one by one from heap for (int i = n - 1; i > 0; i--) { // Move current root to end std::swap(arr[0], arr[i]); // Call max heapify on the reduced heap heapify(arr, i, 0); } } // Function to print an array void printArray(const std::vector<int>& arr) { for (int x : arr) { std::cout << x << " "; } std::cout << std::endl; } int main() { // Example 1 std::vector<int> arr1 = {12, 11, 13, 5, 6, 7}; std::cout << "Original array 1: "; printArray(arr1); heapSort(arr1); std::cout << "Sorted array 1: "; printArray(arr1); std::cout << "--------------------" << std::endl; // Example 2 std::vector<int> arr2 = {4, 10, 3, 5, 1}; std::cout << "Original array 2: "; printArray(arr2); heapSort(arr2); std::cout << "Sorted array 2: "; printArray(arr2); return 0; }
Output
Clear
ADVERTISEMENTS