JS basic grammar --- bubbling order

Bubble sort: all data arranged according to a certain order (from small to large, from largest to smallest)

 

1. Paint help understand the following:

 

2. small to large order:

      var ARR = [10, 0, 100, 20 is, 60, 30 ];
       // cycle 1: Comparison of Control rounds 
      for ( var I = 0; I <arr.length - 1; I ++ ) {
         // cycle 2: comparing each of a number of controls 
        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);

 

3. descending order:

      var ARR = [10, 0, 100, 20 is, 60, 30 ];
       // cycle control comparable round number 
      for ( var I = 0; I <arr.length -. 1; I ++ ) {
         // the control of each one of the comparison of the number of times 
        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);

 

Guess you like

Origin www.cnblogs.com/jane-panyiyun/p/11932857.html