九江网站建设多少钱,手机网站建设与制作,游戏如何制作,网站首页的psd怎么做排序
排序#xff1a;把某个乱序的数组变成升序或降序的数组 (这里用数组来做举例)
归并排序
该排序属于 分治 策略将一个问题分解为两个问题来计算#xff0c;计算完成之后#xff0c;就会得到子任务的解#xff0c;这些解不是最终问题的解#xff0c;还需要merge起来…排序
排序把某个乱序的数组变成升序或降序的数组 (这里用数组来做举例)
归并排序
该排序属于 分治 策略将一个问题分解为两个问题来计算计算完成之后就会得到子任务的解这些解不是最终问题的解还需要merge起来
算法实现
// 归并排序
Array.prototype.mergeSort function() {// 递归自身拆分const rec (arr) {let len arr.length;if(len 1) {return arr;}let m Math.floor(len / 2); // 取中值let l arr.slice(0, mid);let r arr.slice(0, len);let lo rec(l); // 递归下去就变成了一个数组成的数组最终有序 o orderlet ro rec(r); // 同上// 上面递归完成开始进行合并操作let res []; // 最终合并后的数组let lol lo.length; // 左边数组长度let lor ro.length; // 右边数组长度while(lol || lor) {if(lol lor) {res.push(lo[0] ro[0] ? lo.shift() : ro.shift())} else if(lol) {res.push(lo.shift())} else if(rol) {res.push(ro.shift())}}return res;}// 获取递归结果const r rec(this);// 将有序数组拷贝到this上r.forEach((n, i) {this[i] n;});
}let arr [5,4,3,2,1]
arr.insertionSort()
console.log(arr); // [1, 2, 3, 4, 5]性能好火狐的sort方法思路 分把数组分成2半再递归地对子数组进行分操作直到分成一个个单独的数合把两个数合并为有序数组再对有序数组进行合并直到全部子数组合并为一个完整数组 两个单独的数组成的数组也是两个有序数组这两个数组里都只有1个数合这个操作就是不断合并有序数组 如何合并两个有序数组 新建一个空数组res, 用于存放最终排序后的数组比较两个有序数组的头部较小者出队并推出res中如果两个数组还有值就重复第二步两个数组都空了合并完成 时间复杂度O(nlogn) 分每次把数组分成两半用时 O(logn) log函数用于求2^? n, 自然要?log_2 n, 即 O(logn) 注意凡是分的操作基本都是logn 合O(n), 一个while循环体 文章转载自: http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.spftz.cn.gov.cn.spftz.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.ktxd.cn.gov.cn.ktxd.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.fksxs.cn.gov.cn.fksxs.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.baohum.com.gov.cn.baohum.com http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.lqzhj.cn.gov.cn.lqzhj.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn