首页  >  文章  >  web前端  >  js排序 :js冒泡排序的算法原理及代码实现

js排序 :js冒泡排序的算法原理及代码实现

不言
不言原创
2018-08-14 15:47:325391浏览

本篇文章给大家带来的内容是关于js排序 :js冒泡排序的算法原理及代码实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

冒泡排序可谓是最经典的排序算法了,它是基于比较的排序算法,其优点是实现简单,排序数量较小时性能较好。

算法原理

相邻的数据进行两两比较,小数放在前面,大数放在后面,如果前面的数据比后面的数据大,就交换这两个数的位置。也可以实现大数放在前面,小数放在后面,如果前面的数据比后面的小,就交换两个的位置。要实现上述规则需要用到两层for循环。

1533018188-58e5dd802691a_articlex.gif

代码实现

  • 冒泡实现数组从小到大排序

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; i++){
    for(var j=0; j<length-i-1; j++){
      if(par[j]>par[j+1]){
        var temp=par[j+1];
        par[j+1]=par[j];
        par[j]=temp;
      }
    }
  }
  return par;
}
console.log(mintomax(arr));
  • 冒泡实现数组从大到小排序

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; i++){
        for(var j=0; j<length-i-1; j++){
          if(par[j]<par[j+1]){
            var temp=par[j+1];
            par[j+1]=par[j];
            par[j]=temp;
          }
        }
      }
      return par;
    }
    console.log(maxtomin(arr));
  • 用es6结构赋值的方式重写上面代码

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; i++){
    for(var j=0; j<length-i-1; j++){
      if(par[j]>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; i++){
    for(var j=0; j<length-i-1; j++){
      if(par[j]<par[j+1]){
        [par[j],par[j+1]]=[par[j+1],par[j]]
      }
    }
  }
  return par;
}
console.log(maxtomin(arr));
  • 按照父子平铺顺序排序

var data = [{
  ZDXM_STDCODE: &#39;100101&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;1001&#39;,
},
{
  ZDXM_STDCODE: &#39;1001&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;&#39;,
},
{
  ZDXM_STDCODE: &#39;100102&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;1001&#39;,
},
{
  ZDXM_STDCODE: &#39;100201&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;1002&#39;,
},
{
  ZDXM_STDCODE: &#39;1002&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;&#39;,
},
{
  ZDXM_STDCODE: &#39;100202&#39;,
  ZDXM_STDNAME: &#39;&#39;,
  FINA_YYSR: &#39;&#39;,
  FINA_PGZHSY: &#39;&#39;,
  FINA_SJZHSY: &#39;&#39;,
  FINA_PGZHSYL: &#39;&#39;,
  FINA_SJZHSYL: &#39;&#39;,
  FINA_ZHSYLCE: &#39;&#39;,
  FINA_SRJJL: &#39;&#39;,
  FINA_JSSKL: &#39;&#39;,
  FINA_HTE: &#39;&#39;,
  GLZDXM: &#39;1002&#39;,
},
]
function datatotree(par) {
  var length = par.length;
  for (var i = 0; i < length-1; i++) {
    for (var j = 0; j < length - i - 1; j++) {
      var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE;
      var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE;
      if(par[j].GLZDXM==null){
        str1=par[j].ZDXM_STDCODE;
      }
      if(data[j+1].GLZDXM==null){
        str2=par[j+1].ZDXM_STDCODE;
      }
      if (str1 > 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 < length-1; i++) {
    for (var j = 0; j < length - i - 1; j++) {
      var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE;
      var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE;
      if(par[j].GLZDXM==null){
        str1=par[j].ZDXM_STDCODE;
      }
      if(data[j+1].GLZDXM==null){
        str2=par[j+1].ZDXM_STDCODE;
      }
      if (str1 > str2) {
        [par[j],par[j+1]]=[par[j+1],par[j]];
      }
    }
  }
  return par;
}
var data = datatotree(data)
console.log(data);
  • 上面代码排序之后结果

1097031768-5b724fa06d844_articlex.png

相关推荐:

js排序:js插入排序的算法原理与代码实现

js排序动画模拟-插入排序_javascript技巧

以上是js排序 :js冒泡排序的算法原理及代码实现的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn