Home > Web Front-end > JS Tutorial > js sorting: algorithm principle and code implementation of js bubble sorting

js sorting: algorithm principle and code implementation of js bubble sorting

不言
Release: 2018-08-14 15:47:32
Original
5412 people have browsed it

The content of this article is about js sorting: the algorithm principle and code implementation of js bubble sorting. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Bubble sorting can be said to be the most classic sorting algorithm. It is a sorting algorithm based on comparison. Its advantages are simple implementation and better performance when the number of sorting is small.

Algorithm Principle

Adjacent data are compared in pairs, with the decimal placed in front and the large number placed in the back. If the front data is larger than the back data, the two numbers are exchanged. s position. You can also put large numbers in the front and decimals in the back. If the front data is smaller than the back data, swap the two positions. To implement the above rules, a two-level for loop is required.

js sorting: algorithm principle and code implementation of js bubble sorting

Code implementation

  • Bubble implementation array sorting from small to large

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>
Copy after login
  • Bubbles to implement array sorting from large to small

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>Rewrite the above code using es6 structure assignment </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>Sort in parent-child tiling order</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);
Copy after login
  • Rewrite according to es6 structure assignment The above code

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);
Copy after login
  • The result after sorting the above code

js sorting: algorithm principle and code implementation of js bubble sorting

##Related recommendations:

js sorting: js insertion sort algorithm principle and code implementation

js sorting animation simulation-insertion sort_javascript skills

The above is the detailed content of js sorting: algorithm principle and code implementation of js bubble sorting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template