#include#include void swap(int A[],int i,int j){ int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } void Max_Heap(int A[],int heap_size,int i){ int l = 2 * i + 1,r = 2 * i + 2; int largest = i; if(l A[largest]) largest = l; if(r < heap_size && A[r]>A[largest]) largest = r; if(largest!=i){ swap(A,largest,i); Max_Heap(A,heap_size,largest); } } void buildHeap(int A[],int length){ int i = length/2 - 1 , j; for(j = i;j>=0;j--){ Max_Heap(A,length,j); } } void heapSort(int A[],int length){ buildHeap(A,length); int i = length-1; for(i;i>=0;i--){ swap(A,i,0); Max_Heap(A,i,0); } } int main(){ int A[] = {9,8,1,6,5,4,3,2,7,0}; heapSort(A,10); int i = 0; for(i;i<10;i++){ printf("%d\t",A[i]); } printf("\n"); }