Bubble Sort (Bubble Sorting)

It describes the basic time complexity of O (n ^ 2)

The basic idea bubble sort (Bubble Sorting) is: by treating the collating sequence from front to back (from the smaller index of the element), successively comparing the value of adjacent elements, then the switch if the reverse was found that the larger the value of the element gradually from the front toward the rear, as air bubbles under the water gradually take up the same. Because the sorting process, the elements keep close to their position, if the trip more down there has been no exchange, it shows an orderly sequence, so to set a flag in the sorting process flag to determine whether the elements had exchanged. Thus reducing the relatively unnecessary.

Package com.diyo.sort; 

Import the java.text.SimpleDateFormat;
 Import java.util.Arrays;
 Import java.util.Date; 

/ ** 
 * the BubbleSort bubble sort time complexity of O (n-^ 2) 
 * 
 * @author DengZY 
 * 
 * / 
public  class the BubbleSort { 

    public  static  void main (String [] args) { 

        int ARR [] = {. 3,. 9, -1, 10, -2 }; 

        // test counterfeit sorting speed of O (n ^ 2) , the test data to 80000
 //         int [] = arr2 is new new int [80000];
 //         for (int I = 0; I <80000; I ++) {
 //            arr2 is [I] = (int) (Math.random () * 999999); // generates a [0,999999) the number of
 //         } 

//         a Date = new new date1 a Date ();
 //         the SimpleDateFormat SimpleDateFormat the SimpleDateFormat new new = ( "the mM-dd-YYYY HH: mm: SS");
 //         String date1Str = simpleDateFormat.format (date1);
 //         System.out.println ( "time is before sorting =" + date1Str); 
        
        the System.out. println ( "before ordering ----- -----" ); 
        System.out.println (Arrays.toString (arr)); 

        System.out.println ( "start sorting ---- ----- - " );
         bubbleSort (arr); 
        System.out.println ( " Sort ----- ----- end ");

        System.out.println ( "Sort after ----- -----" ); 
        System.out.println (of Arrays.toString (ARR)); 

//         a Date = new new DATE2 a Date ();
 //         String date2Str simpleDateFormat.format = (DATE2);
 //         System.out.println ( "time is sorted =" + date2Str); 
        
        / * 
         * // Sort the first pass, the largest number of rows in a penultimate 
      for (int j = 0; j <arr.length - 1 - 0; j ++) {// if the number is larger than the latter in front of the number, the exchange
        IF (ARR [J]> ARR [J +. 1]) {
          TEMP ARR = [J];
          ARR [J] = ARR [J + 1'd];
           ARR [J +. 1] = TEMP;
        }
      }
      System.out.println ( "first pass after sorting arrays"); the System.out. println (Arrays.toString (arr)); * * // Sort the second pass, the second largest number of rows in the penultimate position for (int J = 0; J <arr.length -. 1 -. 1; J ++) {// * If the previous number later than the number is large, the exchange IF (ARR [J]> ARR [J +. 1]) {TEMP = ARR [J]; ARR [J] = ARR [J + *. 1]; ARR [J +. 1] = TEMP; }} System.out.println ( "after the second pass sorted array"); * System.out.println (of Arrays.toString (ARR)); * * // sort third times, the third number of rows in the third lowest for (int J = 0; J <arr.length -. 1 - 2; J ++) {// * If the number is larger than the latter in front of the number, then the switch if (arr [j]> arr [j . 1 +]) {ARR TEMP = [J]; ARR [J] = ARR [+ J *. 1]; ARR [J +. 1] = TEMP; array after}} System.out.println ( "third sort times "); * System.out.println (Arrays.toString (arr)); * * // sort fourth times, the fourth largest number of rows in the fourth countdown for (int J = 0; J <arr.length -. 1 -. 3; J ++) {// * If the number of the front is larger than the latter number, the exchange IF (ARR [J]> ARR [J +. 1]) {TEMP = ARR [J]; ARR [J] = ARR [J + *. 1]; ARR [ j + 1] = temp;} } ( " a fourth array after sorting times") System.out.println; * System.out.println (of Arrays.toString (ARR));
// if the foregoing ratio of the number of behind the large number, the exchange* / } // bubble sort public void bubbleSort static (int [] ARR) { int TEMP = 0; // temporary variables for exchanging boolean flag = false; // identification variable, indicating whether or not interchanging, for reducing the number of sorting repeated for (int i = 0; I <arr.length -. 1; I ++) { // of the (i + 1) times sorted, the maximum number of rows in the final for (int j = 0; j <arr.length - 1 - i; j ++) { In Flag to true =; TEMP = ARR [J]; IF (ARR [J]> ARR [J +. 1]) { ARR [J] = ARR [J +. 1]; ARR [J +. 1] = TEMP; } } System.out.println ( "first" + (i + 1) "times the sorted array"); System.out.println (of Arrays.toString (ARR)); ! IF (in Flag) {// trip in the sorting process, an exchange never happened break; the else {} In Flag to false =; // reset flag, for the next determination } } } }

 (1) for a total size of the array -1 times a cycle

 (2) the number of each sort decreases gradually

 (3) If we find that at some times ordering, exchange did not happen once, you can end up ordering in advance.

Guess you like

Origin www.cnblogs.com/Diyo/p/11415909.html