Summary of JS array sorting skills (bubble, sort, quick, hill, etc. sorting)

This article summarizes the JS array sorting skills with examples. Share for your reference, the details are as follows:

1. Bubble sort

var temp = 0;
for (var i = 0; i < array.length; i++)
{
for (var j = 0; j < array.length - i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
console.log(array);

The result is as follows:

picture.png

2, sort sort

  var arrSimple=new Array(1,8,7,6,2,5);
        arrSimple.sort();
       // document.writeln(arrSimple.join());
       console.log(arrSimple.join())

The effect is as follows:

picture.png

3. Quick Sort

function quickSort(arr){  
        if(arr.length<=1){//如果数组只有一个数,就直接返回;  
            return arr;  
        }  
        var num=Math.floor(arr.length/2);//找到中间数的索引值,如果是浮点数,则向下取整  
        var newValue=arr.splice(num,1);//找到中间数的值  
        var left=[],right=[];  
        for(var i=0;i<arr.length;i++){  
            if(arr[i]<newValue){  
                left.push(arr[i]);//基准点的左边的数传到左边数组  
            }else{  
                right.push(arr[i]);//基准点的右边的数传到右边数组  
            }  
        }  
        return quickSort(left).concat(newValue,quickSort(right));//递归不断重复比较  
    }  
    console.log(quickSort([31,4,5,52,1,8])); 

The effect is as shown in the figure:

picture.png

4. Hill sort

function shellSort(nums){//希尔排序
        var gaps=[5,3,1];//定义间隔区间
        for(var g=0;g<gaps.length;g++){//一个一个间隔值开始
            for(var i=gaps[g];i<nums.length;i++){//以间隔值遍历
                var temp=nums[i];//选中元素
                for(var j=i;j>=gaps[g]&&nums[j-gaps[g]]>temp;j-=gaps[g]){//如果前面一个大于后面一个
                    nums[j]=nums[j-gaps[g]];//后移
                }
                nums[j]=temp;//填补
            }
        }
    }
    function show(nums){//显示数组
        for(var i=0;i<nums.length;i++){
            document.write(nums[i]+' ');
        }
        document.write('<br>');
    }
    var nums=[6,0,2,9,3,5,8,0,5,4];
    show(nums);//6 0 2 9 3 5 8 0 5 4
    shellSort(nums);//希尔排序
    show(nums);//0 0 2 3 4 5 5 6 8 9

The effect is as shown in the figure:

picture.png

5. Insertion sort

function sort(elements){
  //假设第0个元素是一个有序的数列,第1个以后的是无序的序列,
  //所以从第1个元素开始将无序数列的元素插入到有序数列中
  for(var i = 1; i < elements.length; i++){
    //升序
    if(elements[i] < elements[i-1]){
      //取出无序数列中的第i个作为被插入元素
      var guard = elements[i];
      //记住有序数列的最后一个位置,并且将有序数列位置扩大一个
      var j = i - 1;
      elements[i] = elements[j];
      
      //比大小,找到被插入元素所在的位置
      while(j >= 0 && guard < elements[j]){
        elements[j+1] = elements[j];
        j--;
      }

      //插入
      elements[j+1] = guard;
    }
  }
}

var elements = [10, 9, 8, 7, 6, 5];
console.log('before: ' + elements);
sort(elements);
console.log(' after: ' + elements);

The effect is as shown in the figure:

picture.png

6. Select Sort:

function order(arry){
    var ary=arry
    function sorrt() {
    length = ary.length;
    for (var i = 0; i < length; i++) {
        _min = ary[i]
        k = i
        for (var j = i + 1; j < length; j++) {
            if (_min > ary[j]) {
                _min = ary[j]
                k = j
            }
        }
        ary[k] = ary[i]
        ary[i] = _min
    }
    return ary;
}
    return {sor:sorrt};
    }
     
var k=order([14,12,6,5,18,0,1,3,2])
console.log(k.sor())

The effect is as follows:

picture.png

Attachment: Notes on sorting (sort) of array (Array) in js

var arrDemo = new Array();
arrDemo[0] = 10;
arrDemo[1] = 50;
arrDemo[2] = 51;
arrDemo[3] = 100;
arrDemo.sort(); //调用sort方法后,数组本身会被改变,即影响原数组
alert(arrDemo);//10,100,50,51 默认情况下sort方法是按ascii字母顺序排序的,而非我们认为是按数字大小排序
arrDemo.sort(function(a,b){return a>b?1:-1});//从小到大排序
alert(arrDemo);//10,50,51,100
arrDemo.sort(function(a,b){return a<b?1:-1});//从大到小排序
alert(arrDemo);//100,51,50,10

===================================================== =================
Note: The original author of this article, Miss Qi Che, is not easy to create. For reprints, please indicate the author and the original link of the article, or go to the WeChat public account for authorization.

Welfare at the end of the sentence:

Benefit 1: A collection of 100G resources such as front-end, Java, product manager, WeChat applet, Python, etc. will be released :

Welfare 2: A full set of detailed video tutorials for WeChat mini-program introduction and actual combat.


【How to receive】

Pay attention to [Programming Micro Journal] WeChat public account:

Reply to [Mini Program Demo] One-click to receive 130 Wechat Mini Program source code demo resources.

Reply [Receive resources] One-click to receive front-end, Java, product manager, WeChat applet, Python and other resource collections 100G resources to be released.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325772557&siteId=291194637