标签:
1
2
3
|
10
50 36 41 19 23 4 20 18 12 22
|
1
|
4 12 20 18 22 41 50 36 19 23
|
#include<iostream> using namespace std; void sift(int a[], int low, int high) { int i = low, j = 2 * i; int temp = a[low]; while (j <= high) { while (j < high&&a[j] > a[j + 1]) j++; if (temp > a[j]) { a[i] = a[j]; i = j; j = i * 2; } else break; } a[i] = temp; } void heap(int a[], int n) { int i, temp; for (i = n / 2; i >= 1; i--) { sift(a, i, n); } } int main() { int a[100], i, n; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; heap(a, n); for (i = 1; i <= n; i++){ cout << a[i] << ‘ ‘; } return 0; }
标签:
原文地址:http://www.cnblogs.com/traini13/p/4580809.html