background image

   for (i=Length/2-1; i>=0; i--)   {   
      HeapAdjust(SortData, i, Length);   
    }   
    for (i=Length-1; i>0; i--)  {   
       //

  

与最后一个记录交换

        int tmpData =SortData[0];   
       SortData[0] =SortData[i];   
       SortData[i] =tmpData;   
        //将 H.r[0..i]

   

重新调整为大根堆

       HeapAdjust(SortData, 0, i);   
    }   
  
    return;   

}   

//TestCase  
int main()   { 

const int n=8;
int i;  

    int SortData[n] ={12,36,24,85,47,30,53,91}; 

cout<<"Before sorting:\t";

    for ( i=0; i<n; i++)     {   
        cout<<SortData[i]<<" ";   
    }   
    cout<<endl;
   
    HeapSortData(SortData, n);
   
  cout<<"After sorted:\t";
   for ( i=0; i<n; i++)     {   
        cout<<SortData[i]<<" ";   
   }   
   cout<<endl;  
 
    return 0;   

}

2、运行结果

Before sorting: 12 36 24 85 47 30 53 91 

After sorted: 12 24 30 36 47 53 85 91