欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

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

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

算法原理

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

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

代码实现

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

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);
  • 上面代码排序之后结果

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

相关推荐:

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

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

以上就是js排序 :js冒泡排序的算法原理及代码实现的详细内容,更多请关注其它相关文章!

相关标签: javascript