A method for implementing the cocktail ordering of Java Exchange _java

Source: Internet
Author: User

The example of this article describes the method of implementing the cocktail ordering of Java Exchange. Share to everyone for your reference. Specifically as follows:

Cocktail sort, that is, directional bubble sort, cocktail stirring sort, stirring sort (also can be seen as a variant of the selection sort), ripple sort, back and forth sort or happy hour sort, is a kind of bubble sort deformation. This algorithm differs from bubble sorting in that sorting is sorted in two directions in a sequence.

Different places from bubble sort:
Cocktail sort equals a slight distortion of bubble sort. The difference is from low to high and then from high to low, while the bubbling sort compares each element of the sequence only from low to high. He can get a little better performance than bubble sort because the bubble sort only moves from one Direction to the other (from low to high), and only one item per loop.

In sequence (2,3,4,5,1), for example, a cocktail order can complete a sort only by accessing a sequence, but it takes four times if you use a bubble sort. But in the state of the chaos sequence, cocktail ordering and bubble sequencing are inefficient.


Worst time complexity O (n^2)
Optimal time complexity O (n)
Average time complexity O (n^2)

The cocktail sort dynamic diagram is as follows:

Code Analysis:

Package com.baobaotao.test; /** * Sorting Research */public class Sort {/** * Classic cocktail sort * @param array incoming arrays */public static void 
    Cocatailsort (int[] array) {int length = Array.Length; Loop LENGTH/2 time for (int i=0;i<length/2;i++) {for (int j=i;j<length-i-1;j++) {if (array[j) > 
        Array[j+1]) {Swap (array, j, j+1); } for (int j=length-i-1;j>i;j--) {if (Array[j] < array[j-1]) {swap (array, j-1, J 
        ) ; 
    } printarr (array); 
    }/** * Cocktail sort (with logo bit) * @param array incoming arrays */public static void Cocatailsortflag (int[] array) { 
    int length = Array.Length; 
    Boolean flag1,flag2 = true; 
      Loop LENGTH/2 times for (int i=0;i<length/2;i++) {Flag1 = true; 
      Flag2 = true; 
          for (int j=i;j<length-i-1;j++) {if (Array[j] > array[j+1]) {Swap (array, j, j+1); Flag1 = FALSE; } for (int j=length-i-1;j>i;j--) {if (Array[j] < array[j-1]) {swap (array, j-1, J 
          ) ; 
        Flag2 = false; 
      } if (Flag1 && flag2) {break; 
    } printarr (array); }/** * from small to large order Exchange array * @param a incoming array * @param b the number of the incoming to Exchange B * @param c the number of incoming to exchange c * * * * Publ 
    IC static void Swap (int[] A, int b, int c) {int temp = 0; 
        if (b < c) {if (A[b] > A[c]) {temp = a[b]; 
        A[B] = A[c];  
      A[C] = temp;  /** * Print Array * @param array/public static void Printarr (int[] array) {for (int c 
    : array) {System.out.print (c + ""); 
  } System.out.println (); 
    public static void Main (string[] args) {int[] number={11,95,45,15,78,84,51,24,12}; 
    Int[] number2 = {11,95,45,15,78,84,51,24,12}; 
    Cocatailsort (number); System.out.println ("*****************"); 
  Cocatailsortflag (NUMBER2); } 
}

Results Analysis:

95
   *****************  
11 12 15 24 45 51 78 84 95 (k/k) (+)  

You can see the cocktail sort is a lot less than the normal bubble sort. Only 4 times, with an improved version of the logo-bit cocktail order that takes only 3 times to complete the sort.

I hope this article will help you with your Java programming.

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.