Bubble sort --JavaScript description

  I believe those who have contact with introductory programming bubble sort algorithm, sorting algorithm often used in programming.

1. code

1      / * *
 2       * Bubble Sort
 3       * 1 Comparative equals the total number of wheels - 1
 . 4       * 2, comparing the number of times equal to compare - 1
 . 5       * - Comparative from the first to the last
 6       * - - After each round to give a final value comparison, without participation of a comparator
 7       *
 8       * * / 
. 9  
10      / * *
 11      * maximum time complexity of O (n-2 ^)
 12 is      * space complexity O (. 1)
 13 is      * * / 
14      var ARR = [10, 0, 100, 20 is, 60, 30 ];
 15      var TEMP;
 16      / * *
 . 17       * descending
18       * * * / 
19      / *   controls the number of cycles of Comparative    * / 
20 is      for ( var I = 0; I <arr.length -. 1; I ++ ) {
 21 is          / *   control of each of a number of comparisons    * / 
22 is          for ( var J = 0; J <arr.length -. 1 - I; J ++ ) {
 23 is              IF (ARR [J] <ARR [J +. 1 ]) {
 24                  TEMP = ARR [J];
 25                  ARR [J] = ARR [ . 1 + J ];
 26 is                  ARR [J +. 1] = TEMP;
 27              }
 28          }
 29     }
 30      // Alert (ARR); 
31 is      the console.log ( "small to large:" + ARR);
 32      / * *
 33 is       * ascending
 34 is       * * / 
35      var arr2 is = [10, 0, 100, 20 is, 60, 30 ];
 36      / *   number wheel comparative control * / 
37 [      for ( var I = 0; I <arr2.length -. 1; I ++ ) {
 38 is          / * number of times the comparing control * / 
39          for ( var J = 0 ; J <arr2.length -. 1 - I; J ++ ) {
 40              IF (arr2 is [J]> arr2 is [+ J. 1 ]) {
 41 is                 = TEMP arr2 is [J];
 42 is                  arr2 is [J] = arr2 is [+ J. 1 ];
 43 is                  arr2 is [+ J. 1] = TEMP;
 44 is              }
 45          }
 46 is      }
 47      the console.log ( "descending:" + arr2 )

2. Analysis of Algorithms

(1) time complexity

If the initial state is the positive sequence file, one pass to complete the order. Comparing the number of desired keywords C and recording the number of movements M have reached a minimum:

                                                                    

Therefore, the best time to bubbling order of complexity of O (n).

 If the reverse order of the original file, needs to be n-1 times sort. To sort per trip ni compare times a keyword (≤ i ≤ n-1) , and record each comparison must be moved three times to achieve record exchange position. In this case, the comparison and reached the maximum number of mobile:

 
                                                                              

Worst case time complexity of bubble sort is O (n ^ 2)

In summary, therefore the average total time of bubble sort complex degree len heteroaryl O (n ^ 2)

(2) the stability of the algorithm

Bubble sort is the little elements move it forward or backwards in the big elements. Comparison of two adjacent elements is more, also the exchange occurs between these two elements. Therefore, if the two elements are equal, then the exchange will not; if no adjacent two equal elements, even if the front by pairwise exchange the two adjacent up, this time will not be exchanged, the same elements before and after the order has not changed, so the bubble sort is a stable sort algorithm.

 1 function bubbleSort(arr) {
 2     var i = arr.length, j;
 3     var tempExchangVal;
 4     while (i > 0) {
 5         for (j = 0; j < i - 1; j++) {
 6             if (arr[j] > arr[j + 1]) {
 7                 tempExchangVal = arr[j];
 8                 arr[j] = arr[j + 1];
 9                 arr[j + 1] = tempExchangVal;
10             }
11         }
12         i--;
13     }
14     return arr;
15 }
16  
17 var arr = [3, 2, 4, 9, 1, 5, 7, 6, 8];
18 var arrSorted = bubbleSort(arr);
19 console.log(arrSorted);
20 alert(arrSorted);

 

Guess you like

Origin www.cnblogs.com/YangxCNWeb/p/11415805.html