JavaScript array sort notes

Keywords: Javascript REST

Several kinds of sorting methods are often used in the work, and they are sorted out and shared for everyone in case of emergency.

1. array sort function

Use the sort method of Array.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
arr.sort((a,b) => {
    return a - b
})
console.log(arr) // Results: [0, 2, 2, 2, 5, 5, 6, 7, 8]

2. Bubble sorting

Compare the adjacent two elements in the array, move the large (small) number to the end (start) of the array through the comparison, execute the inner loop once to determine a maximum (minimum) number, and the outer loop traverses from the end (start) of the array to the beginning (end).

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
for(var i=0;i<arr.length;i++) {
    for(var j=0;j<arr.length-1;j++) {
        if (arr[j]>arr[j+1]) {
            let news = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = news
        }
    }
}
console.log(arr) 
// Results: [0, 2, 2, 2, 5, 5, 6, 7, 8]

3. Select sort

First, find the smallest element from the original array, and put the element in the front of the array, then find the smallest element from the rest of the elements, after the smallest element before, minIndex always keeps the index of the location of the minimum value. With the increase of i, the array length of the calendar becomes shorter and shorter until the sorting is completed.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
for(var i=0;i<arr.length;i++) {
    var minIndex=i;
    for(var j=i+1;j<arr.length;j++) {
        if(arr[j]<arr[minIndex]){
            minIndex=j
        }
    }
    if(minIndex!=i){
        var news = arr[i];
        arr[i]=arr[minIndex]
        arr[minIndex]=news
    }
}
console.log(arr) // Results: [0, 2, 2, 2, 5, 5, 6, 7, 8]

4. Insert sort

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]

//Suppose the first element is an ordered sequence, and the first element after it is an unordered sequence,
//So starting from the first element, insert the elements of the unordered sequence into the ordered sequence
for(var i = 1; i < arr.length; i++){
    //Ascending order
    if(arr[i] < arr[i-1]){
        //Take the i in the disordered sequence as the inserted element
        var guard = arr[i];
        //Remember the last position of the ordered sequence, and expand the position of the ordered sequence by one
        var j = i - 1;
        arr[i] = arr[j];

        //Compare the size to find the location of the inserted element
        while(j >= 0 && guard < arr[j]){
            arr[j+1] = arr[j];
            j--;
        }

        //insert
        arr[j+1] = guard;
    }
}

console.log(arr) // Results: [0, 2, 2, 2, 5, 5, 6, 7, 8]

5. Quick sort

Fast sorting involves recursion. The sorting problem of an array is regarded as the sorting problem of two small arrays, and each small array can continue to be regarded as two smaller arrays, recursion continues until the maximum length of the array is 2.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]

function quickSort(arr){  
    if(arr.length<=1){//If the array has only one number, it will be returned directly;  
        return arr;  
    }  
    var num=Math.floor(arr.length/2);//Find the index value of the middle number. If it is a floating-point number, round it down  
    var newValue=arr.splice(num,1);//Find the value of the middle number  
    var left=[],right=[];  
    for(var i=0;i<arr.length;i++){  
        if(arr[i]<newValue){  
            left.push(arr[i]);//The number on the left of the benchmark is transferred to the left array  
        }else{  
            right.push(arr[i]);//The number to the right of the reference point is transferred to the array to the right  
        }  
    }  
    return quickSort(left).concat(newValue,quickSort(right));//Recursive repeated comparison  
}  
    
console.log(quickSort(arr)) 
// Results: [0, 2, 2, 2, 5, 5, 6, 7, 8]

Posted by aspbyte on Sat, 04 Apr 2020 21:06:27 -0700