#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/* Maintains the max heap property for a subtree */
void heapify(int arr[], int size, int root) {
int largest = root;
int left = 2 * root + 1;
int right = 2 * root + 2;
if (left < size && arr[left] > arr[largest]) {
largest = left;
}
if (right < size && arr[right] > arr[largest]) {
largest = right;
}
if (largest != root) {
std::swap(arr[root], arr[largest]);
heapify(arr, size, largest);
}
}
/* Performs heap sort on the array */
void heapSort(int arr[], int size) {
for (int i = size / 2 - 1; i >= 0; --i) {
heapify(arr, size, i);
}
for (int i = size - 1; i > 0; --i) {
std::swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
int main() {
const int SIZE = 7;
int values[SIZE];
cout << "Enter " << SIZE << " integers: ";
for (int i = 0; i < SIZE; ++i) {
cin >> values[i];
}
heapSort(values, SIZE);
cout << "Sorted array: ";
for (int i = 0; i < SIZE; ++i) {
cout << values[i] << " ";
}
cout << endl;
return 0;
}