#include<iostream> #include<string> #include<vector> #include<iomanip> #include<stdlib.h> #include <stdio.h> using namespace std; #define MAXITEM 100 typedef struct { int key; int data; }LineList; void Sift(LineList R[], int low, int high) //构建堆 { int i = low, j = 2*i; LineList tmp = R[i]; while(j <= high) { if(j < high && R[j].key < R[j+1].key) j++; if(tmp.key < R[j].key) { R[i] = R[j]; i = j; j = 2*i; } else break; } R[i] = tmp; } void HeapSort(LineList R[], int n) //堆排序 { int i; LineList tmp; for(i = n/2; i >= 1; i--) Sift(R, i, n); for(i = n; i >= 2; i--) { tmp = R[1]; R[1] = R[i]; R[i] = tmp; Sift(R, 1, i-1); } } void main() { LineList R[MAXITEM]; int A[] = {0, 75, 87, 68, 92, 88, 61, 77, 96, 80, 72}; int i, n = 10; for(i = 0; i <= n; i++) R[i].key = A[i]; HeapSort(R, n); for(i = 1; i <= n; i++) cout << setw(3) << R[i].key; cout << endl; }