void HeapAdjust (int* arr, int n, int s) {
int i = s;
for (i=2*s+1;i<n;i=i*2+1) {
if (i+1<n && arr[i+1] > arr[i]) {
i=i+1;
}
if (arr[i] < arr[s]) {
break;
}
Swap(arr, s,i);
s = i;
}
}
void HeapSort(int* arr, int n) {
int i;
for (i = n/2 -1 ; i >= 0; i--) {
HeapAdjust(arr,n,i);
}
Swap(arr,0,n-1);
for (i=n-1;i>1;i--) {
HeapAdjust(arr,i,0);
Swap(arr,0,i-1);
}
}
再写一遍堆排序
最新推荐文章于 2021-02-17 02:02:06 发布