堆排序 【代码有问题?】https://www.bilibili.com/video/BV1Eb41147dK?spm_id_from=333.999.0.0
#include <iostream>
using namespace std;
void Heapfi(int heap[],int i,int size){
int c1 = 2*i+1;
int c2 = 2*i+2;
int max = i;
if(c1 < size && heap[c1] > heap[max])
max = c1;
if(c2 < size && heap[c2] > heap[max])
max = c2;
if(max != i){
swap(heap[max],heap[i]);
Heapfi(heap,max,size);
}
}
void HeapBuild(int heap[],int size){
for(int i=(size-2)/2;i>=0;i--){
Heapfi(heap,i,size);
}
}
void HeapSort(int heap[],int size){
HeapBuild(heap,size);
for(int i=size-1;i>=0;i--){
swap(heap[i],heap[0]);
Heapfi(heap,0,i);
}
}
int main(){
int array[] = {7,35,15,10,30};
HeapSort(array,5);
for(int i=0;i<5;i++){
cout<<array[i]<<" ";
}
}