堆与堆排序 【对下面视频的总结】https://blog.csdn.net/Appleeatingboy/article/details/119607212?spm=1001.2014.3001.5501
堆排序 【代码有问题?】https://www.bilibili.com/video/BV1Eb41147dK?spm_id_from=333.999.0.0
#includeusing 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<
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)