java - basic - sort

Bubble Sort

    // bubble sort, type = 0 then small to large, type = 1 is descending
     // iterate again if the front is smaller than the back (large), the exchange of both, over the traverse length-1 
    public  void bubbleSort ( int A [], int type) {
         for ( int I = 0; I <a.length -. 1; I ++ ) {
             for ( int J = 0; J <a.length -. 1; J ++ ) {
                 IF ((A [J]> A [+ J. 1] && type == 0) || (A [J] <A [+ J. 1] && type ==. 1 )) {
                     int n-= A [J]; 
                    A [J] A = [J +. 1 ]; 
                    A [J +. 1] = n-; 
                }
                //showArray(a);
            }
        }
    }

 

 

 

 Selection Sort

    // selection sort, type = 0 from the small to large, type = 1 then the descending
     // iterate again before the selected i-th minimum (large) and a [i-1] exchange. i to the length. 1; 
    public  void SelectionSort ( int A [], int type) {
         for ( int i =-a.length. 1; i> = 0; i-- ) {
             int In Flag = 0 ;
             for ( int J = 0; J <= I; J ++ ) {
                 IF ((A [In Flag]> A [J] == 0 && type) || (A [In Flag] <A [J] == && type. 1 )) { 
                    In Flag = J; 
                } 
                // showarray (A);
            }
            int n = a[i];
            a[i] = a[flag];
            a[flag] = n;
        }
    }

 

Guess you like

Origin www.cnblogs.com/clamp7724/p/11590218.html