- #include
- using namespace std;
-
- void swap(int &a,int &b)
- {
- int temp=a;a=b;b=temp;
- }
-
- void heapjust(int arr[],int k, int len)
- {
- int i=2*k+1;
- while (i<=len)
- {
- if ((arr[i]
1])&&(i+1<=len)) i++; - if (arr[k]>arr[i]) break;
- swap(arr[k],arr[i]);
- k=i;
- i=2*k+1;
- }
- }
-
- void buildmaxheap(int arr[],int len)
- {
- for (int i=len/2;i>=0;i--) heapjust(arr,i,len);
- }
-
- int main()
- {
- int arr[9] = {6,1,5,9,8,4,7,1,15};
- buildmaxheap(arr,8);
- for (int i=0;i<9;i++){printf("%d",arr[i]);printf(",");}
- }