Bubble sort, selection sort

In the battle against the class of learning, this week I learned my two sorting methods.

(A) bubble sort

 (1) Principle: two adjacent data units, more storage. That is, if the first data unit is large, it will be two adjacent cells, storing data exchange                           

(2) Process: Starting from an 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 discharge in the current comparison unit involved in the final; previously elected comparing unit, the next will not participate in next comparison

(3) Optimization:

1, the inner loop optimized: number of cycles inner -1

                 Current bit and the next bit i and i + 1 Compare Compare

               Current penultimate cycle, and the last one has to compare

                The current cycle of the last one, only through the penultimate position, relatively involved, not involved in the cycle;

         2, the outer loop optimization: 

             If there are n cells participate sorting, sorting cycle only n-1 times

              Sorting the last cycle, in descending order of the last two values ​​will be determined

             Last remaining unit without sorting cycle, but the size has been determined, you do not need to re-cycle sorting;

   The last time to participate in cycling, has decided to come out of the maximum value, do not participate in the next cycle

              The first sort, less participation 0 units

               The second sorting, cells involved in a few

              The third sort, cells involved in at least 2

               ........

              By outer loop variable definition is circular numerical starting from 0

              Each inner loop, the number, minus the value of the outer loop variable

(4) after the optimized code:

  There res = [3,44,38,5,47,25,36,2,79,8,1];

   for(var i = 0;i <= (arr.length-1)-1;i++){

           for(var j=0;j <= (arr.length-1)-1-i;j++){

               if(arr[j]>arr[j+1]){

                   There res = 0;

                   res = arr[j];

                   arr[j]=arr[j+1];

                   arr[j+1]=res;

               }

           }  

       }

Exchange data stored:: (5) the core of two adjacent cells, comparing the data size, the larger value the first unit, the two units exchanged data stored

 

(B) Selection Sort

         (1) Principle: the selected minimum value of this index means the index cycle is located, if the cell is not the start position, the starting unit for data exchange.

(2) Process: cycle starting position to define the default location is the minimum value; starting from a position at a starting position, execution cycle; if the position value, less than the index value at the storage location, to store the index value position; end of the cycle, comparing the stored index, if the index is the starting position, if not, the exchange value of the two positions, the minimum value of the present cycle, is placed in the starting position of the cycle; then execute multiple cycles to complete the sequencing

(3) Optimization:

 1) an outer layer optimization: the value before the comparison, a comparison without participation

              2) an inner layer optimization: position to start the cycle from a start position of the next

(4) after the optimized code:

    There res = [3,44,38,5,47,25,36,2,79,8,1];

        for(var i = 0;i <= (res.length-1)-1;i++){

             my = I;

             for(var j=i+1;j <= res.length-1;j++){

                  if(res[min]>res[j]){

                    min = j;

                 }

             }

             if(min != i){

               There are m = 0;

               m = res[i];

               nothing [i] = nothing [min];

               res[min]=m;

            }

       }

(5) Core: Find the minimum index value exchanged with the starting position. First find the index value exchanged

Guess you like

Origin www.cnblogs.com/w8261/p/12544154.html