全球网站访问量排名,ru后缀的网站,网站首页设计图,自己动手做导航网站引言#xff1a;归并排序跟快速排序一样#xff0c;都运用到了分治的算法#xff0c;但是归并排序是一种稳定的算法#xff0c;同时也具备高效#xff0c;其时间复杂度为O(N*logN) 算法图解#xff1a; 然后开始归并#xff1a; 就是这个思想#xff0c;拆成最小子问题… 引言归并排序跟快速排序一样都运用到了分治的算法但是归并排序是一种稳定的算法同时也具备高效其时间复杂度为O(N*logN) 算法图解 然后开始归并 就是这个思想拆成最小子问题后再进行归并两个有序数组的排序问题
下面是代码 void merge_sort(int* arry, int size) {//保证接口一致性再调子函数assert(arry);int* tmp (int*)malloc(sizeof(int) * size);_merge(arry, 0, size - 1,tmp);//_merge2(arry, 0, size - 1, tmp);free(tmp);
}
void _merge(int* arry, int left, int right, int* tmp) {if (right - left 0)return;int mid left (right - left 1);//找到中间值//递归拆分子问题_merge(arry, left, mid, tmp);_merge(arry, mid 1, right, tmp);merge_arry(arry, left, mid, mid 1, right, tmp);
}
void merge_arry(int* arry, int begin1, int end1, int begin2, int end2, int* tmp) {int index begin1;int left begin1;int right end2;while (begin1 end1 begin2 end2) {if (arry[begin1] arry[begin2]) {tmp[index] arry[begin1];}else {tmp[index] arry[begin2];}}if (begin1 end1) {for (int i begin1; i end1; i) {tmp[index] arry[i];}}else {for (int i begin2; i end2; i) {tmp[index] arry[i];}}//再拷贝回原数组for (int i left; i right; i) {arry[i] tmp[i];}
}
上面是它的递归实现那么思考如何使用非递归实现呢 同时要控制grap的循环次数grap小于等于数组大小即可
下面是代码
void _merge2(int* arry, int left, int right, int* tmp) {int grap 1;while (grapright1) {for (int i left; i right; i 2 * grap) {int begin1 i, end1 i grap - 1;int begin2 i grap, end2 i 2 * grap - 1;if (end1 right)end1 right;if (end2 right)end2 right;merge_arry(arry, begin1, end1, begin2, end2, tmp);}grap grap * 2;}}
void merge_sort(int* arry, int size) {assert(arry);int* tmp (int*)malloc(sizeof(int) * size);//_merge(arry, 0, size - 1,tmp);_merge2(arry, 0, size - 1, tmp);free(tmp);
}
文章转载自: http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.mwnch.cn.gov.cn.mwnch.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.sldrd.cn.gov.cn.sldrd.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn