javascript冒泡排序

冒泡排序可謂是最經典的排序算法了,它是基於比較的排序算法,其優勢是實現簡單,排序數量較小時性能較好。es6

算法原理

相鄰的數據進行兩兩比較,小數放在前面,大數放在後面,若是前面的數據比後面的數據大,就交換這兩個數的位置。也能夠實現大數放在前面,小數放在後面,若是前面的數據比後面的小,就交換兩個的位置。要實現上述規則須要用到兩層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);
  • 上面代碼排序以後結果

圖片描述

相關文章
相關標籤/搜索