The front end of the sorting algorithm needs to know

Today, a good learning algorithm on sorting, the following are some conclusions can be considered a consolidation.

First sorting algorithm is no better or worse in different scenarios, different sorting algorithm efficiency.

1. Select sorting (selection sort):

A sorting can be aligned to the minimum value of an interval of the first region

For example: Array [5, 1, 3, 2, 4, 6]

Step 1: Select the minimum value of the array is generally set to a value of 5,

Step two: the value of the underlying value, and if the following value is small, on the exchange value, otherwise unchanged,

Step Three: cycle next area.

  For example: [5, 1, 3, 2, 4, 6] default minimum value is 5, then the minimum area that is interchangeable with subsequent 1. [1, 5, 3, 2, 4, 6]

     [1, 5, 3, 2, 4, 6] to identify a next interval [5, 3, 2, 4, 6] 5 the default minimum, and the minimum area that is interchangeable with the subsequent 2, [2, 3 , 5, 4, 6]

     [2, 3, 5, 4, 6] to identify a next interval [3, 5, 4, 6] default minimum 3, then subsequent zone with a minimum value i.e. exchange 4, he small did not change [3 , 5, 4, 6] and so on subsequent

// reversed array value 
function the swap (ARR, I1, I2) {
     var TEMP = ARR [I1]; 
    ARR [I1] = ARR [I2]; 
    ARR [I2] = TEMP; 
} 
/ * * 
 * Selection Sort 
 * @param ARR} * { 
 * / 
function SelectionSort (ARR) {
     for ( var I = 0; I <arr.length -. 1; I ++ ) {
         // record its minimum value, smaller than if followed would change his position 
        var = min ARR [I];
         var index = I;
         for ( var J = I +. 1; J <arr.length -. 1; J ++ ) {
            if (arr[j] < min) {
                min = arr[j];
                index = j;
            }
        }
        swap(arr, i, index);
    }
}
 was arr = [5, 1, 3, 2, 4, 6];
  selectionSort(arr);
  console.log(arr)
 

2. bubble sort (bubble sort):

A bubble sort, sort of a maximum area of ​​the sequence may be the last bit of the region, the specific way:

  1. Comparing the first and second bit 2, if the former is larger than the latter exchange
  2. Comparison of the bits 2 and 3, if the former is larger than the latter exchange
  3. And so on, until the comparison to the last two of the region
  4. Repeat the process until the sequence is completed ordering

For example: [5, 1, 3, 2, 4, 6] 

  First: [1, 5, 3, 2, 4, 6]

  Second: [1, 3, 5, 2, 4, 6] 

  Third: [1, 3, 2, 5, 4, 6] 

  Fourth: [1, 3, 2, 4, 5, 6] and so on

/ * * 
 * Bubble sort 
 * * @param {} ARR 
 * / 
function bubbleSort (ARR) {
     for ( var I = 0; I <arr.length -. 1; I ++) { // cycle several times 
        for ( var J 0 =; J <arr.length -. 1 - I; J ++ ) {
             // need to go through the time-arr.length bubbling. 1 
            // I: 0 cycle: ~ 0. 1-I-arr.length 
            // I:. 1 cycle: ~ 0. 1-I-arr.length 
            // I: 2 cycles: arr.length-0. 1 ~ I- 
            IF (ARR [J]> ARR [J +. 1 ]) { 
                the swap (ARR, J, J + . 1 ); 
            } 
        } 
    } 
} 
var arr = [5, 1, 3, 2, 4, 6];
bubbleSort(arr);
console.log(arr)

3. insertion sort (insertion sort):

The sequence is divided into two parts, one part is ordered, disordered part, to do now is, data is continuously taken from a disordered portion, is added to the ordered part, the sort is complete until the entire

Principle: sequence [5, 7, 2, 3, 6]

  1. Disordered and ordered into a sequence of sequence (5) (7236)
  2. Continue the expansion of an ordered sequence (57) (236)
  3. Continue the expansion of an ordered sequence (257) (36)
  4. Continue the expansion of an ordered sequence of (2357) (6)
  5. Continue the expansion of an ordered sequence of (23567)
  6. Sort completion

Programs written:

[5, 1, 3, 2, 4, 6 ], assuming an ordered sequence of (5,1,3), to save a value after the first arr [i] = 2 to temp, temp, and then have value sequence table before the last bit of a comparison, 
at this time it is the last one before a 1, less than a temp, so temp will be placed on the last one, its original value in an ordered list Finally, becomes (5,1,2,3), can the cycle
/ * * 
 * Insert sequencing 
 * * @param {} ARR 
 * / 
function InsertionSort (ARR) {
     for ( var I =. 1; I <arr.length; I ++ ) {
         IF (ARR [I] <ARR [I -. 1] ) { // If the former is larger than the bit, 
            // the value of the i-th bit is added to the front of the ordered queue correct position 
            var TEMP = ARR [i];
             for ( var J = i; J> = 0; J - ) {
                 IF (J> 0 && ARR [J -. 1]> TEMP) { 
                    ARR [J] = ARR [J -. 1]; // the last one before the ordered list if greater than a temp, it the last set ordered list of its former. 
                }
                the else { // if ordered before the last list is less than a temp, it will be the last set temp. 
                    ARR [J] = TEMP;
                     BREAK ; 
                } 
            } 
        } 
    } 
} 
var ARR = [. 5,. 3,. 1,. 6,. 7,. 4 ]; 
InsertionSort (ARR) 
the console.log (ARR);

4. The quick sort (quick sort):

Selecting a number (such as the last one of the sequence) as the reference number, the entire ordered sequence into two portions, one smaller than the number, the number is larger than the other part, the reference number in the middle and then do the same thing remaining sequence until the sorting is done

For example: the sequence [5, 7, 2, 3, 6, 4]

  1. 4 selected as the reference number, ranking become: (3, 2) 4 (7, 6, 5)
  2. For 3,2, continue to use the sorting mode, obtained: (2, 3) 4 (7,6,5)
  3. For 7,6,5, continue to use the sorting mode, obtained: (2, 3) 4 (5,6,7)
  4. Sort completion
function QUICKSORT (ARR) {
     / * * 
     * 
     * @param {*} ARR 
     * @param {*} Start Start subscript 
     * @param {*} superscript End End 
     * / 
    function _quickSort (ARR, Start, End) {
         IF (Start> End = || Start> arr.length -. 1) return ;
         var High = End, Low = Start; // set the high position and 
        var Key = [High] ARR; // set the reference value is the last one 
        while (low < High) {
             the while (low <High && ARR [low] <= Key) low ++; // if the value is lower than the reference value, the lower closer to the upper 
            ARR [High] = ARR [low]; //If the value is found lower than a reference value, the bit value in the high, low and not closer to the upper 
            the while high-- (Low <High && ARR [High]> = Key); // low does not move, if the high is greater than the reference value, the high-low close, 
            ARR [low] = ARR [High]; // close process, if the high than the reference value, the low value placed closer. 
        } 
        ARR [Low] = Key; // high position is equal to the low, the reference value into. 
        _quickSort (ARR, Start, Low -. 1); // cyclic fast sort the array reference value to the left 
        _quickSort (ARR,. 1 + High, End); // array reference value to the right of quicksort 
    }    
    _quickSort (ARR, 0, ARR. . 1-length ); 
} 
var ARR = [. 5,. 1,. 3, 2,. 4,. 6 ]; 
QUICKSORT (ARR); 
the console.log (ARR)







Guess you like

Origin www.cnblogs.com/whaleAlice/p/11923321.html