分类:
2006-09-03 15:18:08
#include using namespace std; void MakeHeap (int a[], int n); int main () { int a[100]; int n, i; cout << "Enter the number:" << endl; cin >> n; for (i = 1; i <= n; i++) { cout << "Enter the element of the Heap:" << endl; cin >> a[i]; } MakeHeap (a, n); cout << "The Heap is:" << endl; for (i = 1; i <= n; i++) { cout << a[i] << " "; } cout << endl; return 1; } void MakeHeap (int a[], int n) { int i, j, flag; for (i = 1; i <= n/2; i++) { j = 2 * i; if (a[j] >= a[j + 1]) j++; if (a[i] > a[j]) { flag = a[i]; a[i] = a[j]; a[j] = flag; } } } |