市政工程建设规范免费下载网站,房产局网站建设方案,有没有可以在网站上做试卷的,昆明网站建设贴吧归并排序#xff08;Merge Sort#xff09;是一种基于分治思想的排序算法#xff0c;它将待排序数组分成两个子数组#xff0c;然后对这两个子数组分别进行排序#xff0c;最后将两个已排序的子数组合并成一个有序数组。归并排序是一种稳定的排序算法#xff0c;具体体现…归并排序Merge Sort是一种基于分治思想的排序算法它将待排序数组分成两个子数组然后对这两个子数组分别进行排序最后将两个已排序的子数组合并成一个有序数组。归并排序是一种稳定的排序算法具体体现在其时间复杂度上。
下面是使用 C 实现的归并排序算法
#include algorithm
#include iostream
#include random
#include vectorusing namespace std;void mergeSortImplementation(vectorint array, int left, int right)
{if (left right){return;}const int mid left ((right - left) 1);mergeSortImplementation(array, left, mid);mergeSortImplementation(array, mid 1, right);vectorint temp(right - left 1);int p1 left;int p2 mid 1;for (unsigned int i 0; i temp.size(); i){if (p1 mid 1){temp[i] array[p2];continue;}if (p2 right 1){temp[i] array[p1];continue;}if (array[p1] array[p2]){temp[i] array[p1];}else{temp[i] array[p2];}}for (unsigned int i left; i right 1; i){array[i] temp[i - left];}
}void mergeSort(vectorint array)
{if (array.size() 2){return;}mergeSortImplementation(array, 0, array.size() - 1);
}int main(int argc, char* argv[])
{// Create an array to test sort methodvectorint array {2, 3, 4, 0, 7};cout Before sorting: ;for (int i 0; i array.size(); i){cout array[i] ;}cout endl;mergeSort(array);cout After sorting: ;for (int i 0; i array.size(); i){cout array[i] ;}cout endl;return 0;
}
归并排序的实现过程可以分为两个步骤
分割子问题将待排序数组分成两个子数组分别对两个子数组进行排序。合并解决方案将两个已排序的子数组合并成一个有序数组。
具体来说归并排序的分割子问题的实现可以使用递归算法每次将待排序数组分成两个子数组然后对两个子数组分别进行排序合并解决方案的实现则需要额外的存储空间将两个已排序的子数组合并成一个有序数组。
归并排序的时间复杂度为 O(nlogn)O(nlogn)O(nlogn)其中 n 是待排序数组的长度。归并排序每次将待排序数组分成两个子数组分别对两个子数组进行排序然后再将两个已排序的子数组合并成一个有序数组。这个过程的时间复杂度可以表示为
T(n)2T(n/2)O(n)T(n) 2T(n/2) O(n)T(n)2T(n/2)O(n)
其中 T(n) 表示对长度为 n 的数组进行归并排序的时间复杂度。通过递归展开可以得到
T(n)2T(n/2)O(n)2[2T(n/4)O(n/2)]O(n)4T(n/4)2O(n)4[2T(n/8)O(n/4)]2O(n)8T(n/8)3O(n)...nT(1)lognO(n)T(n) 2T(n/2) O(n)\\ 2[2T(n/4) O(n/2)] O(n)\\ 4T(n/4) 2O(n)\\ 4[2T(n/8) O(n/4)] 2O(n)\\ 8T(n/8) 3O(n)\\ ...\\ nT(1) lognO(n)T(n)2T(n/2)O(n)2[2T(n/4)O(n/2)]O(n)4T(n/4)2O(n)4[2T(n/8)O(n/4)]2O(n)8T(n/8)3O(n)...nT(1)lognO(n)
故而其最终算法时间复杂度为O(nlogn)O(nlogn)O(nlogn)。 文章转载自: http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn