Js algorithm in the sorting array to the detailed overview

  • 2020-03-26 21:24:49
  • OfStack

In fact, js array sort, the array sort method is relatively simple:

A, sorting,

Simply implement array sort


var arr = [];  
for(var i=0;i<20;i++){  
    arr.push(Math.floor(Math.random()*100))  
}  
arr.sort(function(a,b){  
    return a>b?1:-1;  
})  
alert(arr)

You can't simply use the sort method, which by default is sorted alphabetically in ASCII rather than numerically as we think,

The sort() method can accept a method as an argument, which has two arguments. Each represents two array items for each sort comparison. Sort () executes this argument every time you compare two array items, and compares two arrays

The term is passed to the function as an argument. The order of the two array items is swapped when the function returns a value of 1, otherwise it is not swapped.

Array sort of algorithm


var arr = [];  
for(var i=0;i<20;i++){  
    arr.push(Math.floor(Math.random()*100))  
}  
//Generates an unordered arr array & NBSP;
function sort(arr,start,end){  
    //The length of the array is 1& PI;
    if(start == end ){  
        return [arr[start]]  
    }else if(start == end-1){  
        //The length of the array is 2, sorted by value & PI;
        if(arr[start]>arr[end]){  
            return [arr[end],arr[start]]  
        }else{  
            return [arr[start],arr[end]]  
        }  
    }  
    //Half the length of the array & NBSP;
    var l = Math.floor((start+end)/2);  
    //The left hand side of the array & NBSP;
    var arrLeft = sort(arr, start,l);  
    //Right hand side array & NBSP;
    var arrRight = sort(arr,l+1,end);  
    //Returns the result & NBSP;
    var result = [];  
    //Divide the two arrays into two parts. The two arrays are only compared to the first number in the array. If the number is small, put the one into the result and delete the small number. Once the left or right array appears, there is no data & NBSP;
    //result The array is merged with the array that still has the data   using  concat, and Returns the result & NBSP;
    while(arrLeft.length>0 || arrRight.length>0){  
        if(arrLeft.length==0){  
            result = result.concat(arrRight);  
            break;  
        }else if(arrRight.length==0){  
            result = result.concat(arrLeft);  
            break;  
        }  
        if(arrLeft[0]<arrRight[0]){  
            result.push(arrLeft.shift())  
        }else{  
            result.push(arrRight.shift());  
        }  
    }  
    return result;  
}  
var arrSort = sort(arr,0,arr.length-1);//Parameter array, starting position, ending position & NBSP;

document.write(arr+'<br/>'+arrSort);

Explanation: array sort one split in two, mainly USES the array until cannot, finally can only be removed inside the array are only one or two, because the length of the array has the branch of t, open at the end of the array only after one or two to sort and returns the results, and the results in comparison to merge one by one. This method may be why you think so complicated, has been using the first no, actually, of course, but in this world there are performance this term, when the data after several dozens of a few hundred, you calculate the result of time is no different, in case of large when the data of hundreds of millions of billions do we have any such confidence with the first method, js algorithm is to divide and rule, actually will be divided into small lots of problems to solve.

Two, the array is removed duplicate

Simple way to get rid of duplicates: declare an empty array first, insert the duplicate array for loop, and repeatedly skip the non-duplicate inserts


var arr = [];  
for(var i=0;i<20;i++){  
    arr.push(parseInt(Math.random()*10));  
}  
Array.prototype.indexOf = function(n){  
    for(var i=0;i<this.length;i++){  
        if(this[i] == n){  
            return i;  
        }  
    }  
    return -1;  
}  
function removeDup(arr){  
    var result = [];  
    for(var i=0;i<arr.length;i++){  
        if(result.indexOf(arr[i]) == -1){  

            result.push(arr[i]);  
        }  
    }  
    return result;   
}  
var arr2 = removeDup(arr)  
document.write(arr+'<br/>'+arr2) 

The algorithm array gets rid of duplicates

var arr = [];  
for(var i=0;i<20;i++){  
    arr.push(parseInt(Math.random()*10));  
}  
Array.prototype.indexOf = function(n){  
    for(var i=0;i<this.length;i++){  
        if(this[i] == n){  
            return i;  
        }  
    }  
    return -1;  
}  
function removeDup(arr,s,e){  
    if(s==e){  
        //The division is left with one & NBSP;
        return [arr[s]]  
    }else if(s==e-1){  
        //You don't have to split the remaining two in order to optimize it & NBSP;
        if(arr[s]==arr[e]){  
            return [arr[s]]  
        }else{  
            return [arr[s],arr[e]];  
        }  
    }  
    //The array is split in two, & NBSP;
    var l = Math.floor((s+e)/2);  
    //The left  
    var arrL = removeDup(arr,s,l);  
    //The right  
    var arrR = removeDup(arr,l+1,e);  
    //The result is a copy of the one on the left & NBSP;
    var result = arrL;  
    //The loop inserts non-repeating data into the result & NBSP;
    for(var i=0;i<arrR.length;i++){  
        if(result.indexOf(arrR[i])== -1 ) result.push(arrR[i])  
    }  
    return result; //Returns the result & NBSP;
}  
var arrDup = removeDup(arr, 0, arr.length-1);  
document.write(arr+'<br/>'+arrDup);

Explanation: Cut the duplicate array, split it into one or two arrays, put the data on the left into the result, and skip the data on the right without repeating the insertion until the loop is over and the result is returned


Related articles: