javascript冒泡排序

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

算法原理

相邻的数据进行两两比较,小数放在前面,大数放在后面,如果前面的数据比后面的数据大,就交换这两个数的位置。也可以实现大数放在前面,小数放在后面,如果前面的数据比后面的小,就交换两个的位置。要实现上述规则需要用到两层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; 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: '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) {
        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);
  • 上面代码排序之后结果

图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值