|
#include<iostream>
using namespace std;
int arr[1000000];
void maxhead(int arr[], int size, int i) {
int l = i * 2;
int r = i * 2 + 1;
int x = i;
if (l <= size && arr[l] > arr[x]) {
x = l;
}
if (r <= size && arr[r] > arr[x]) {
x = r;
}
if (x != i) {
swap(arr[x], arr);
maxhead(arr, size, x);
}
}
void buildhead(int arr[], int size) {
for (int i = size / 2; i >= 1; i--) {
maxhead(arr, size, i);
}
}
void sorthead (int arr[], int size) {
for (int i = size; i >= 2; i--) {
swap(arr[1], arr);
maxhead(arr, i - 1, 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr;
}
buildhead(arr, n);
sorthead(arr, n);
for (int i = 1; i <= n; i++ ) {
cout << arr << " ";
}
return 0;
}
|
|