本篇文章给大家带来的内容是关于js排序 :js冒泡排序的算法原理及代码实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
冒泡排序可谓是最经典的排序算法了,它是基于比较的排序算法,其优点是实现简单,排序数量较小时性能较好。
算法原理
相邻的数据进行两两比较,小数放在前面,大数放在后面,如果前面的数据比后面的数据大,就交换这两个数的位置。也可以实现大数放在前面,小数放在后面,如果前面的数据比后面的小,就交换两个的位置。要实现上述规则需要用到两层for循环。
代码实现
冒泡实现数组从小到大排序
var arr=[2,7,1,8,6,4,3,5,9];function mintomax(par){ var length=par.length; for(var i=0; i<length-1>par[j+1]){ var temp=par[j+1]; par[j+1]=par[j]; par[j]=temp; } } } return par;}console.log(mintomax(arr));</length-1>
冒泡实现数组从大到小排序
var arr=[2,7,1,8,6,4,3,5,9];function maxtomin(par){ var length=par.length; for(var i=0; i<length-1><ul class=" list-paddingleft-2"><li><p>用es6结构赋值的方式重写上面代码</p></li></ul><pre class="brush:php;toolbar:false">var arr=[2,7,1,8,6,4,3,5,9];function mintomax(par){ var length=par.length; for(var i=0; i<length-1>par[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]] } } } return par;}console.log(mintomax(arr));function maxtomin(par){ var length=par.length; for(var i=0; i<length-1 console.log><ul class=" list-paddingleft-2"><li><p>按照父子平铺顺序排序</p></li></ul><pre class="brush:php;toolbar:false">var data = [{ ZDXM_STDCODE: '100101', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001',},{ ZDXM_STDCODE: '1001', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '',},{ ZDXM_STDCODE: '100102', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001',},{ ZDXM_STDCODE: '100201', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002',},{ ZDXM_STDCODE: '1002', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '',},{ ZDXM_STDCODE: '100202', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002',},]function datatotree(par) { var length = par.length; for (var i = 0; i str2) { var temp = par[j + 1]; par[j + 1] = par[j] par[j] = temp; } } } return par;}var data = datatotree(data)console.log(data);
按照es6结构赋值重写上面代码
var data = [{ ZDXM_STDCODE: '100101', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001',},{ ZDXM_STDCODE: '1001', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '',},{ ZDXM_STDCODE: '100102', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001',},{ ZDXM_STDCODE: '100201', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002',},{ ZDXM_STDCODE: '1002', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '',},{ ZDXM_STDCODE: '100202', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002',},]function datatotree(par) { var length = par.length; for (var i = 0; i str2) { [par[j],par[j+1]]=[par[j+1],par[j]]; } } } return par;}var data = datatotree(data)console.log(data);
上面代码排序之后结果
相关推荐:
js排序:js插入排序的算法原理与代码实现
js排序动画模拟-插入排序_javascript技巧
标签: js冒泡排序
免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:chuangshanghai#qq.com(把#换成@)