其他分享
首页 > 其他分享> > 堆排序

堆排序

作者:互联网

堆排序

 1 void heapAdjust(int a[],int i,int length)
 2 {
 3     int tmp = a[i];
 4     for(int k=2*i+1;k<length;k = 2*k+1)
 5     {
 6         if(k+1<length&&a[k+1]>a[k])
 7             k++;
 8         if(tmp>a[k])
 9             break;
10         a[i] = a[k];
11         i = k;
12     }
13     a[i] = tmp;
14 }
15 void heapSort(int a[],int length)
16 {
17     for(int i=(length-1)/2;i>=0;i--)   
18     {
19         heapAdjust(a,i,length);
20     }
21     for(int i=length-1;i>=0;i--)
22     {
23         swap(a[0],a[i]);
24         heapAdjust(a,0,i);
25     }
26 }

 

标签:tmp,int,void,堆排序,length,heapAdjust
来源: https://www.cnblogs.com/xyhj/p/14539616.html