Java implementation bubble sort and bidirectional bubble sort algorithm code example _java

Source: Internet
Author: User

Bubble Sort:
is the two elements that are contiguous by index, and if greater than/less than (depending on whether ascending or descending order is required), replace, otherwise do not make changes
this round down, compares the n-1 times, n equals the number of elements; n-2 , n-3 ... One to the last round, compared 1 times
so the comparison is decremented: from n-1 to 1
The total number of comparisons is: 1+2+3+...+ (n-1),  is calculated in a linear formula: (1+n-1)/2* (n-1) ==> n/2* (n-1) = = > (n^2-n) * 0.5
Using large O to indicate the time complexity of the algorithm: O (n^2),  ignores coefficients 0.5 and constant-n

public class Bubblesort {public static void main (string[] args) {int len = 10; 
    int[] ary = new Int[len]; 
    Random Random = new Random (); 
    for (int j = 0; J < Len; J +) {Ary[j] = random.nextint (1000); 
    } System.out.println ("Before-------------sort"); 
    for (int j = 0; J < Ary.length J + +) {System.out.print (Ary[j] + ""); * * * Ascending, ASC1 and ASC2 optimize the number of times the internal loops are compared: * Asc1, ASC2: (1+n-1)/2* (n-1) ==> n/2* (n-1) ==&gt ; 
N (n-1)/2 ==> (N^2-N)/2 * asc:n^2-n *//ORDERASC (ARY); 
    ORDERASC2 (ary); 
     
    ORDERASC1 (ary); Descending, you just need to replace the size of the decision} static void Orderasc (int[] ary) {int count = 0;//compare times int len = Ary.lengt 
    H for (int j = 0; J < Len; J +) {//outer fixed loop count for (int k = 0; k < len-1; k++) {//inner fixed cycle count if (ary[k) & Gt 
     Ary[k + 1]) {Ary[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//One-step swap/* Swap two variable values      * A=a+b * B=a-b * a=a-b/} count++; 
    } System.out.println ("\ n-----orderasc Ascending sort------number of times:" + count); 
    for (int j = 0; J < Len; J +) {System.out.print (Ary[j] + ""); 
    } static void OrderAsc1 (int[] ary) {int count = 0;//compare times int len = ary.length; for (int j = 0; J < Len; J +) {//outer fixed loop count for (int k = len-1 k > J; k--) {//inner layer from multiple to less descending comparison if (ary[ 
      K] < ary[k-1]) {ary[k] = Ary[k-1] + (ary[k-1] = ary[k]) * 0;//one-step exchange} count++; 
    } System.out.println ("\ n-----orderAsc1 Ascending sort------number of times:" + count); 
    for (int j = 0; J < Len; J +) {System.out.print (Ary[j] + ""); 
    } static void OrderAsc2 (int[] ary) {int count = 0;//compare times int len = ary.length; for (int j = len-1 J > 0; j--) {//outer fixed cycle times/* k < J; total comparison times = (n^2-n)/2/FOR (int k = 0; k < J; k++) {//inner layer from multiple to less descending compare times if (Ary[k] > ary[k + 1]) {Ary[k] = ary[k + 1] + (ary[ 
      K + 1] = ary[k]) * 0;//one Step exchange} count++; 
    } System.out.println ("\ n-----orderAsc2 Ascending sort------number of times:" + count); 
    for (int j = 0; J < Len; J +) {System.out.print (Ary[j] + ""); 
 } 
  } 
}

Print

-------Sort------ 
898 7 862 286 879 660 433 724 316 737  
-----ORDERASC1 Ascending sort------number of times: 
7 286 316 433 660 724 73 7 862 879 898  

Bidirectional bubble sort
Bubble Sort _ cocktail sort, which is bidirectional bubble sort.
The difference between this algorithm and bubble ordering is that the sort is sorted in a bidirectional sequence, with the outer layer comparing the left and right edges L<r,
The inner layer of a cycle from left to right ratio, take the high value of the post; a cycle from right to left, take low value before;
Efficiency, O (n^2), no faster than the ordinary bubbling

public class Bubble_cocktailsort {public static void main (string[] args) {int len = 10; 
    int[] ary = new Int[len]; 
    Random Random = new Random (); 
    for (int j = 0; J < Len; J +) {Ary[j] = random.nextint (1000); * * * * The minimum number of exchanges is also 1 times, the largest is (n^2-n)/2 times//Ary=new int[]{10,9,8,7,6,5,4,3,2,1}; Test Exchange times//Ary=new int[]{1,2,3,4,5,6,7,8,10,9}; 
    Test Exchange times System.out.println ("------before-------sorting"); 
    for (int j = 0; J < Ary.length J + +) {System.out.print (Ary[j] + ""); 
} orderAsc1 (ary); 
     
    ORDERASC2 (ary); Descending, you just need to replace the size of the judgement} static void OrderAsc1 (int[] ary) {int comparecount = 0;//compare times int Changec 
    Ount = 0;//exchange number int len = ary.length; 
    int left = 0, right = len-1, tl, tr; 
      while (left < right) {//outer fixed loop number TL = left + 1; 
      tr = right-1; for (int k = left; k < right; k++) {//inner layer from more to less descending comparison times, left-to-right if (Ary[k] > ary[k + 1]) {//before greater than, permutation ary[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//one step Exchange changecount++; tr = k; 
      In the last comparison of the round, the index of K is assigned to TR, and TR indicates the end index value of the later comparison, from left to right ratio, K to the left index} comparecount++; 
      right = TR; for (int k = right; k > left; k--) {//inner layer from multiple to less descending comparison, right-to-left if (Ary[k] < ary[k-1]) {//after less than before substitution ary 
          [k] = Ary[k-1] + (ary[k-1] = ary[k]) * 0;//One-step Exchange changecount++; TL = k; 
      In the last comparison of the round, the K index is assigned to the TL, and TL indicates the starting index value of the later comparison, from right to left ratio, K to the right index} comparecount++; 
    left = TL; 
    } System.out.println ("\ n-----orderAsc1 Ascending sort------Compare times:" + Comparecount + ", Number of Exchanges:" + changecount); 
    for (int j = 0; J < Len; J +) {System.out.print (Ary[j] + ""); }///The idea of ORDERASC1 is no different. static void OrderAsc2 (int[] ary) {int comparecount = 0;//compare times int Changeco 
    UNT = 0;//exchange number int len = ary.length; 
    int L = 0, r = len-1, tl, tr; for (; L < R; 
      ) {//outer fixed cycle number TL = l + 1; 
      tr = r-1; 
          * * * from left to right ratio, move large to back/for (int k = l; k < R k++) {if (Ary[k] > ary[k + 1]) { 
          int temp = Ary[k] + ary[k + 1]; 
          Ary[k + 1] = temp-ary[k + 1]; 
          Ary[k] = temp-ary[k + 1]; 
          changecount++; 
        tr = k; 
      } comparecount++; 
      } r = TR; 
          * * from right to left ratio, move small to front/for (int k = r; k > L; k--) {if (Ary[k] < ary[k-1]) { 
          int temp = Ary[k] + ary[k-1]; 
          ARY[K-1] = temp-ary[k-1]; 
          ARY[K] = temp-ary[k-1]; 
          changecount++; 
        TL = k; 
      } comparecount++; 
    L = TL; 
    } System.out.println ("\ n-----orderAsc2 Ascending sort------Compare times:" + Comparecount + ", Number of Exchanges:" + changecount); 
    for (int j = 0; J < Len; J +) {System.out.print (Ary[j] + ""); 
 } 
  } 
}

Print

-------Sort------ 
783 173 955 697 839 201 899 680 677  
-----orderAsc1 Ascending sort------compare times: 34, Exchange times: 
53 173 201 6 77 680 697 783 839 899 955  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.