安徽网站建设电话,广西住房及城乡建设厅网站,做短视频必备的网站,更新wordpress图片地址在heap堆中#xff0c;大根堆是一种特殊的堆#xff0c;它满足下列性质#xff1a;对于任意一个非叶子节点i#xff0c;其左右子节点的值均小于等于它本身的值。
在大根堆中#xff0c;堆顶元素永远是值最大的元素#xff0c;所以将堆顶元素不断取出来#xff0c;就相当…在heap堆中大根堆是一种特殊的堆它满足下列性质对于任意一个非叶子节点i其左右子节点的值均小于等于它本身的值。
在大根堆中堆顶元素永远是值最大的元素所以将堆顶元素不断取出来就相当于对数组进行了从大到小的排序操作。
相比较于其他排序算法使用大根堆进行数组排序的优点在于
1时间复杂度稳定为O(nlogn)空间复杂度仅为O(1)并且算法实现简洁、易于理解。
2由于大根堆的特殊性质堆排序还具有良好的局部性和稳定性能够保持元素在排序前后的相对位置关系不会改变具有相同值的元素之间的顺序。
下面用图片来梳理我们的思路 代码注解
首先我们先创建一个数组 1 首先我们将数组中的值建立成大根堆
2首尾换位向下调整成为循环 我们将数组中的值建立成大根堆的Adjustup函数的实现
Adjustup函数的实现的空间复杂度(3条消息) 堆的向下调整与向上调整的时间复杂度_biter down的博客-CSDN博客 我们将数组中的值建立成大根堆的Adjustup函数的实现
Adjustdown函数的实现的空间复杂度(3条消息) 堆的向下调整与向上调整的时间复杂度_biter down的博客-CSDN博客 源码提供参考 #includestdio.h void Swap(int* p1, int* p2) { int t *p1; *p1 *p2; *p2 t; }
void AdjustDown(int* a, int size) { int parent 0; //将leftchild作为最大的孩子 int child parent * 2 1; while (child size) { //当leftchild的值小于rightchlid时child的值应该为较大值的右孩子 if (child 1 size a[child 1] a[child]) { child; } Swap(a[child], a[parent]); parent child; child child * 2 1; } }
void AdjustUp(int* a, int child) { int parent (child - 1) / 2; while ( child ! 0 a[child] a[parent]) { Swap(a[child], a[parent]); child parent; parent (parent - 1) / 2; } }
//排升序建大根堆 void HeapSort(int* a,int n) { //将数组中的值建立成大根堆 for (int i 1; i n; i) { AdjustUp(a, i); } //将大根堆数组的元素升序 for(int in;i0;i--) { Swap(a[0], a[i]); //首尾互换 AdjustDown(a, i); //向下调整 } } int main() { int arr[10] { 2,1,5,7,6,8,0,9,4 }; //对数组进行大根堆排序 HeapSort(arr, sizeof(arr) / sizeof(arr[0])); return 0; }
文章转载自: http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.zkpwk.cn.gov.cn.zkpwk.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.zrgdd.cn.gov.cn.zrgdd.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn