What is a bubble sort? What is the selection sort? What is the difference between them?

1. Bubble Sort

principle:

  Two adjacent units, comparing the stored data. If the first data cell is large, it will be stored in two adjacent unit exchange data.

Process:
  start from the initial comparison unit, the first cycle, selects a maximum value, all the cells in the final array;
  after each cycle, will compare a maximum value of the current cycle, the current on the participation Finally, the comparison unit;
  previously elected comparing unit, a relatively lower will not participate.

Optimization:
  (1) the last unit has passed the penultimate unit involved in the comparison, so the last unit would not participate in a single cycle.
  (2) compare the previous maximum value, the next comparison is no longer involved once

  (3) n units long cycle times n-1 comparison can, and finally a comparison unit when not recycled.

core:

  Storage of data exchange, two adjacent unit compares the data size, a larger value on the first data exchange unit two storage units.

 

var arr = [30, 33, 13, 2, 1];
            for (j = 0; j <= (arr.length - 1) - 1; j++) {
                for (var i = 0; i <= (arr.length - 1) - 1 - j; i++) {
                    if (arr[i] > arr[i + 1]) {
                        var middle = 0;
                        middle = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = middle;
                    }
                }
            }
            console.log(arr);

 

2. Select Sort

Step:
  (1) to define the starting position of the cycle of the default location is the minimum, the cycle started from a position at an initial position.

  (2) if the value is less than the value at the position on the storage position of the index, the index value is stored in this location.

  After comparison (3) whether the index is stored in the cycle start position of the index, if the value is not exchanged on the two positions, the minimum cycle will be present, is placed in the starting position of the cycle.

  (4) performing multiple cycles to complete the re-ordering.

Core:

  Index minimum is found, then the exchange value of the starting position.

Optimization:

  Values (1) before the comparison is not involved in a mark
  (2) 2 n units, n-1 times as long as Comparative

 

        ARR = var [. 5,. 4,. 3, 2,. 1]; 
            // outer loop, the last remaining number that is already the greatest therefore not involved in the cycle, the number of cycles to -1 
            for (J = 0 ; J <= (arr.length -. 1) -. 1; J ++) { 
                // we start position is the default minimum 
                var min = J; 
                // default start position is a minimum value, just from the comparison when the next start comparing it 
                for (I = J +. 1; I <= arr.length -. 1; I ++) { 
                    // make memory array subscript min minimum value 
                    IF (ARR [min]> ARR [I]) { 
                        min I =; 
                    } 
                } 
                // if the array index is not the initial array subscripts 
                // exchanged index value corresponding to the index value stored in the min and subscript index j corresponding 
                IF (j = min!) { 
                    var middle = 0; 
                    middle = arr [j];
                    arr[j] = arr[min];
                    arr[min] = middle;

                }
            }
            console.log(arr);

 

 

to sum up:

Select the sort: (High Efficiency)
  If the order problem, just do the operation assignment index. Like cycle is completed, judgment is performed, to make a data exchange.

Bubble sort:
  the order of every occurrence of the problem, have to perform a data exchange operation. Execution frequency is higher than the selected sorting data exchange, the data exchange operation is relatively cumbersome, too execution times and low efficiency.

Guess you like

Origin www.cnblogs.com/hwy6/p/12549746.html