Home > Web Front-end > JS Tutorial > body text

JS sorting method (sort, bubble, select, insert) code summary_basic knowledge

WBOY
Release: 2016-05-16 15:16:49
Original
1751 people have browsed it

I recently started learning data structures.

I will sort it out bit by bit, hope I can stick to it.

Because the direction is front-end, it is implemented in JavaScript.

//sort排序
var testArr1=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
var testArr2=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
var testArr3=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
testArr1.sort();//排序结果:[15, 19, 2, 26, 27, 3, 36, 38, 4, 44, 46, 47, 48, 5, 50]
testArr2.sort(function(a,b){return a>b});//排序结果:[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
testArr3.sort(function(a,b){return a-b});//排序结果:[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
Copy after login

//Bubble sorting

var testArr1=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
var testArr2=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
function bubbleSort1(array){
  for(i=array.length-1;i>0;i--){
    for(j=0;j<i;j++){
      if(array[j+1]<array[j]){
        var temp=array[j+1];
        array[j+1]=array[j];
        array[j]=temp;
      }
    }
  }
}
function bubbleSort2(array){
  for(i=array.length-1;i>0;i--){
    for(j=0;j<i;j++){
      if((array[j+1]-array[j])<0){
        var temp=array[j+1];
        array[j+1]=array[j];
        array[j]=temp;
      }
    }
  }
}
bubbleSort1(testArr1);//排序结果:[2,3,4,5,15,19,26,27,36,38,44,46,47,48,50]
bubbleSort2(testArr2);//排序结果:[2,3,4,5,15,19,26,27,36,38,44,46,47,48,50]
Copy after login

//Select sort

var testArr=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
function selectSort(array){
  for(i=0;i<array.length;i++){
     var slc=array[i];//初始时设未排序的第一个值为选中值
     var slcIdx;//记录一次循环后作为选中值的index
     for(j=i;j<array.length;j++){  
      if(array[j]<slc){
       slc=array[j];
       slcIdx=j; 
      }
     }
   if(slc!=test[i]){//如果最后作为选中值的值和初始slc值不相等
    var temp=array[i];
    array[i]=array[slcIdx];
    array[slcIdx]=temp;
   }
  }
}
selectSort(testArr);//排序结果是:[2,3,4,5,15,19,26,27,36,38,44,46,47,48,50]
Copy after login

//Insert sort

var testArr=[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
function insertSort(array){
  for (var i = 0 ; i < array.length-1; i++) {//注意i小于数组的长度-1,否则会造成数组越界,形成死循环
    var curElement=array[i+1];
    for (var j = i; j >= 0; j--) {
      if(curElement<array[j]){
        array[j+1]=array[j];
        if(j==0){//当j==0时,说明已经排到了数组的最开头
          array[0]=curElement;
        }
      }else{
        array[j+1]=curElement;
         break;
      }
    };
  };
}
 insertSort(testArr);//排序结果是:[2,3,4,5,15,19,26,27,36,38,44,46,47,48,50]
Copy after login

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