Achieve three basic sorting and Efficiency Comparison: bubble sort, insertion sort and selection sort

ThreeTypesOfBaseSort class {public
  // ========================== Efficiency Comparison =========== three basic sort =================
  public static void main (String [] args) {
    ThreeTypesOfBaseSort new new ThreeTypesOfBaseSort Sort = ();

    // test array sorting one million level, see three basic efficiency difference sort of:
    int Number = 500000;
    int [] Array = new new int [Number];
    int [] array1 = new new int [Number];
    int [] array2 = new new int [Number];
    for (int I = 0 ; I <be array.length; I ++) {
      int new new TEMP = the Random () the nextInt (Number);.
      Array [I] = TEMP;
      array1 [I] = TEMP;
      array2 [I] = TEMP;
    }
    System.out.println ( "array ready ~");

    Long Start1 = System.currentTimeMillis ();
    sort.bubbleSort (Array);
    Long END1 = System.currentTimeMillis ();
    System.out.println ( "when used bubbleSort:" + (end1 - start1) ); // 5 million: 41575000: 4302551000: 1,644,079

    Long start2 = System.currentTimeMillis ();
    sort.selectionSort (array1);
    Long end2 = System.currentTimeMillis ();
    System.out.println ( "when selectSort with:" + (end2 - start2) ); // 5 million: 7275000: 742 531 000: 281 276 == "select Sort 5.7 times faster than the bubble.

    start3 = System.currentTimeMillis Long ();
    sort.insertionSort (array2);
    Long END3 = System.currentTimeMillis ();
    System.out.println ( "when used insertionSort:" + (end3 - start3) ); // 5 million: 827 .50 million: 84644. == "Sort insert a little slower than the selection.
  }

  // ========================== BubbleSort ===================== =======
  / **
  * bubble sort: adjacent to compare two numbers, if it is small to large order, then put the bigger number, every time the exchange.
  * Bubble sort of optimization ideas:
  * ① sentenced empty;
  * ② when the number of elements is very special: 0,1 when the number is;
  * ③ array has been ordered, or an array where all elements are the same:
  * /
  public void bubbleSort (int [] target) {
    IF (target == null) {
      return;
    }
    Boolean = Tag to true; // if the array is ordered, then do not need to be exchanged.
    for (int i = 0; i <target.length -1 && tag; i ++) {// The outer loop: the number of the control group compared :( number of elements -1) times.
      = to false Tag;
      for (int J = 0; J <target.length - I -. 1; J ++) {// inner loop: control the number of each Comparative: Comparative comparisons begin from the first element of each group, the comparing each max when moved to the back, each of the number of comparisons = target.length-1-i.
        if (target [j]> target [j + 1]) {
          target TEMP = int [J];
          target [J] = target [J +. 1];
          target [J +. 1] = TEMP;
          Tag = to true;
        }
      }
    }
  }

  // =========== SelectionSort ============================ ============
  / **
  * select the sort:
  * The first trip data sequence selected from n elements keywords min / element on the most forward position and,
  * the selected trip min / elements from the n-1 element and is not sorted on the foremost position of the element. So, after n-1 times to complete the order.
  * /
  Public void SelectionSort (int [] target) {
    IF (target == null) {
      return;
    }
    for (int I = 0; I <target.length -. 1; I ++) {
      int tempIndex = I;
      for (int J = i + 1; j <target.length ; j ++) {
        IF (target [tempIndex]> target [J]) {
          tempIndex = J;
        }
      }
    int TEMP = target [tempIndex];
    target [tempIndex] target = [I];
    target [I] = TEMP;
    }
  }

  // == =================== insertSort ============================== ======
  / **
  * insertion sort: inserting data into a sorted been sequenced data (usually the default is the first element in order, starting from the second comparison element),
  * such that to obtain a new, plus a number of sequenced data, sorting algorithm is suitable for small amounts of data.
  * /
  Public void InsertionSort (int [] target) {
    IF (target == null) {
      return;
    }
    // set the number of comparing the outer loop;
    for (int I = 0; I <target.length -. 1; I ++ ) {
      // two cycles: the outer comparing control group number; inner responsible for identifying the group, the first element in a disordered after having been sorted array, and by comparing the random element into this ordered array.
      for (int I = J +. 1; J> 0; J -) {
        IF (target [J -. 1]> target [J]) {
          int target TEMP = [J];
          target [J] = target [J - . 1];
          target [J -. 1] = TEMP;
        } the else {
          BREAK;
        }
      }
    }
  }
}

Guess you like

Origin www.cnblogs.com/laipimei/p/11118561.html