#include <iostream>
using namespace std;
void Sift(int r[], int i, int n){
int j = 2*i;
int temp;
while (j <= n){
if(j<n && r[j]<r[j+1]){
j++;
}
if(r[i] > r[j]){
break;
}
else{
temp = r[i];
r[i] = r[j];
r[j] = temp;
i = j;
j = 2*i;
}
}
}
void HeapSort(int r[], int n){
int temp;
int i;
for(i=n/2; i>=1; i--){
Sift(r, i, n);
}
for(i=1; i<n; i++){
temp = r[1];
r[1] = r[n-i+1];
r[n-i+1] = temp;
Sift(r, 1, n-i);
}
}
int main ()
{
int array[] = {0, 36, 30, 18, 40, 32, 45, 22, 50};
HeapSort(array, 8);
for (int i=1; i<=8; i++){
cout << array[i] << " " ;
}
return 0;
}
堆排序二
最新推荐文章于 2022-03-15 18:23:12 发布