Select sort and bubble sort array

Select sorting rules: successively comparing the first data and all the data back to find the minimum and on the first place
 
function arr ( myarr ) { 
    for ( var I  = 0 ; I  <  myArr . length - . 1 ; I ++) {determine the number of rows of comparison 
        var min = S [ I ]; for a first hypothesis is minimal 
        var minwz = I ; save location of the minimum 
        for ( var J = I + . 1  ; J  <  myArr . length ; J ++) {determines the number of comparisons 
            IF ( min  >  S [ J ]) {, and determines whether the earlier hypothesis correct, incorrect
                min = S [ J ]; save true minimum
                minwz=j;
            }
        }
        S [ minwz ] = S [ I ]; first pushed to the first, the minimum in the first place
        s [ in ] = min
    }
}
where p = [ 12 , 34 , 57 , 23 , 11 ]; 
arr(s);
console.log(s);
 
 
Bubble sort: pairwise comparison, large put back
 
function fn(myarr){
    for(var i =0;i < myarr.length-1;i++){
        for(var j = 0;j <  myarr.length-1-i;j++){
            if(s[j] > s[j + 1]){
                var ls=s[j];
                s[j]=s[j + 1];
                s[j + 1]=ls;
            }
        }
    }
}
var s=[12,34,56,78,34,23];
fn(s);
console.log(s);

Guess you like

Origin www.cnblogs.com/2507148161----com/p/11780725.html