Eight sort (a) ------- bubble sort

From small to large bubble sort

Bubble sort is treated by collating sequence from front to back, sequentially comparing the value of adjacent elements, then the switch if the reverse order, so that a large value of the element toward the rear. Bubble sort is a stable algorithm, the time complexity of O (n2)

 

Subscript starts at 1  

Therefore, because the number of outer loop 10 to 10 cycles  so i = 10 i> 0 i--

 

9 second first secondary flights third 7 8   

  The first 1211 + 1 Comparative

  Second 2,322 + comparative

  Ninth 9 10 j j + 1 Comparative  so j = 1 j <i 

 

Analyzing the proviso that a [j]> a [j + 1] to the exchange position

 

 

class MaoPao {
    
    int [] = {ARR 0,13, 14, 26 is,. 7,. 9, 22 is,. 5,. 8, 11,17 };
     public  static  void main (String [] args) {
        
        MaoPao MP = new new MaoPao () ; 
        mp.maopao (); 
        System.out.println ( "delimiter ------------------- --------------- - " ); 
        mp.prin (); 
    } 
    void maopao () {
         int I = 0 ;
         int J = 0 ;
         int TEMP = 0 ;
         //   for the next index starts easily seen that 
        for (I = 10; I > 0; I -) {   //10 cycles set to 10 because 10 is easily understood that the number i = 9; i> 0 below j = 0; j <i may be 
            for (J =. 1; J <I; J ++) {   // first As long as i less than 9 times per Run 1 
                IF (ARR [J]> ARR [J + 1 ]) { 
                    TEMP = ARR [J]; 
                    ARR [J] = ARR [J + 1 ]; 
                    ARR [J + 1'd ] = TEMP; 
                } 
            } 

        } 

    } 

    void PRIN () {
         for ( int I =. 1; I <arr.length; I ++ ) { 
            System.out.println (ARR [I]); 
        } 
        
    } 


}

 

Guess you like

Origin www.cnblogs.com/cnng/p/12329561.html