Bubble sort to achieve high efficiency

// realize his bubble sort easiest way is to use a double loop 
var ARR = [10,2,5,7,8,10,10,20,10,18,20,89,6,7,52 ]
 // the outer loop is that we need to sort each column is a number, then the number of columns to be sorted is arr.length - 1 times 
for ( var I = 0; I <arr.length - 1; I ++ ) {
     for ( var J 0 =; J <arr.length -. 1 -i; J ++ ) {
         IF (ARR [J]> ARR [J +. 1 ]) {
             var TEMP = ARR [J];
            arr [j] = arr [j + 1 ];
            arr[j+1] = temp
        }
    }
}
console.log(arr)
// The above efficiency is relatively low, and reference may be binary search recursively our ideas to realize a high efficiency method of sorting 
function QUICKSORT (Start, End) {
     IF (Start> = End) {
         return
    }
    // binary search flag 
    var Pivot = ARR [Start];
     var left = Start;
     var right = End;
     // to circle around position as not to overlap when we exchanged left and right of 
    the while (left =! Right) {
         // If the cycle is stopped right smaller than the record mark and the current position of 
        the while (right> left && ARR [right]> = Pivot) {
            right--
        }
        // cycle is stopped if the left large ratio flag and record the current position of 
        the while (left <right && ARR [left] <= Pivot) {
            left++
        }
        // -switched position 
        IF (left < right) {
         var TEMP = ARR [left]
        arr[left] = arr[right]
        arr[right] = temp
        }

    }
    // If the right = left then we will put in the middle pivot position 
        arr [Start] = arr [left];
        arr[left] = pivot;
        quickSort(start , left - 1);
        quickSort(right + 1,end);

}

quickSort(0,arr.length - 1)
console.log(arr)

 

Guess you like

Origin www.cnblogs.com/tengx/p/11904759.html