void HeapSort(int a[],int n)
{
int i;
for(i = n/2; i > 0; i--){//构建初始堆
HeapAdjust(a,i,n);
}
for(i = n; i > 1; i--){//不断输出最大元素进行排序,输出后继续调整
swap(a,1,i);
HeapAdjust(a,1,i-1);
}
}
void HeapAdjust(int a[],int s,int n){
int i;
int temp = a[s];
for(i = 2 * s; i <= n; i++){
if(i < n && a[i+1] > a[i]){
i++;
}
if(temp > a[i]) break;
a[s] = a[i];
s = i;
}
a[s] = temp;
}
利用数组实现的堆排序
最新推荐文章于 2022-09-27 21:09:18 发布