Bubble Sort _ Cocktail sort
is two-way bubble sort
The difference between this algorithm and the bubbling sort is that the sort is sorted in two directions in the sequence, and the outer layer compares the left and right boundary L<r,
The inner layer of a loop from left to right ratio, take the high value is placed, a loop from right to left, take low value before;
Efficiency, O (n^2), no faster than normal 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}; Number of test exchanges//ary=new int[]{1,2,3,4,5,6,7,8,10,9}; Test interchange Count System.out.println ("-------------before sorting"), for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");} ORDERASC1 (ary);//orderasc2 (ary);//Descending, just the size of the replacement}static void OrderAsc1 (int[] ary) {int comparecount = 0;//comparison int Changecount = 0;//Interchange int len = Ary.length;int left = 0, right = len-1, tl, tr;while (left < right) {//outer fixed loop number TL = Le FT + 1;tr = right-1;for (int k = left; k < right; k++) {//The inner layer is decremented from many to less, the number of times between the right if (Ary[k] > ary[k + 1]) {//Before the AR Y[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//One-step exchange changecount++;tr = k; When the last comparison in the round, the index of K is assigned to the TR, TR represents the end index value after comparison, from left to right, K for the left index} comparecount++;} right = tr;for (int k = right, K > left; k--) {//inner layer from multiple to less decrement comparison, right-to-left if (Ary[k] < ary[k-1]) {//post less than pre-displacement ary[k] = Ary[k-1] + (ary[k-1] = ary[k]) * 0;//One-step Exchange cha Ngecount++;tl = k; When the last comparison in the round, the index of K is assigned to the TL, TL represents the starting index value of the next comparison, from right to left, K for the right Index} comparecount++;} left = TL;} System.out.println ("\ n-----orderAsc1 in ascending order------comparison:" + Comparecount + ", Number of Exchanges:" + Changecount); for (int j = 0; J < Len ; J + +) {System.out.print (Ary[j] + "");}} There's no difference with ORDERASC1 's idea. static void OrderAsc2 (int[] ary) {int comparecount = 0;//comparison int changecount = 0;//interchange int len = Ary.le Ngth;int L = 0, r = len-1, TL, tr;for (; l < R;) {//outer number of fixed cycles 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 tem p = ary[k] + ary[k + 1];ary[k + 1] = temp-ary[k + 1];ary[k] = temp-ary[k + 1];changecount++;tr = k;} comparecount++;} R = tr;/* * 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];ar Y[K-1] = temp-ary[k-1];ary[k] = temp-ary[k-1];changecount++;tl = k;} comparecount++;} L = TL;} System.out.println ("\ n-----orderAsc2 in ascending order------comparison:" + Comparecount + ", Number of Exchanges:" + Changecount); for (int j = 0; J < Len ; J + +) {System.out.print (Ary[j] + "");}}}
Print
Java implements two-way bubble sorting