Cocktail sort (double bubble sort, stir sort or ripple sort)

Source: Internet
Author: User
Cocktail sort (double bubble sort, stir sort or ripple sort)

Using System;  Using System.Collections.Generic;  Using System.Linq;          Using System.text;namespace Sort {class Cocktailsorter {private static int[] MyArray;          private static int arraySize;              public static int[] Sort (int[] myArray) {arraySize = Myarray.length;              Cocktailsort (MyArray);          return myArray;              } public static void Cocktailsort (int[] myArray) {int., up, index, I;              Low = 0;//array start index up = myarray.length-1;//array index max value index = low;//Temp variable//Determine if there are multiple elements in the array while (Up > Low)//each time you enter the while loop, the largest and smallest elements within the corresponding range are identified and placed in the corresponding position {//Enter the for loop to place the largest element in the index's bounds to the right Edge for (i = low, i < up; i++)//scan from top to bottom {if (Myarray[i] > Myarr                        Ay[i + 1]) {Swap (ref myarray[i], ref myarray[i + 1]);  index = i;//record Current Index}} up = index;//record the location of the last interchange//enter the FOR                      The loop places the smallest element within the index's bounds on the leftmost for (i = up, i > Low; i--)//from the last interchange position from bottom to top  if (Myarray[i] < myarray[i-1]) {Swap (ref myarray[i], ref myarray[i                          -1]);                      index = i; }} low = index;//record the location of the last interchange}} private static void Swa              P (ref int left, ref int. right) {int temp;              temp = left;              left = right;          right = temp; }      }  }

The cocktail sort equals a slight distortion of the bubbling sort. The difference is from low to high and from high to low, while bubble sorting is only low to high to compare each element in the sequence. He can get a little bit better performance than bubble sort, because the bubble sort only moves from one Direction to the next (from low to high) and only one item per loop.

Using cocktail ordering, the numbers in the array are irregular emissions, find the smallest number first, put him first, and then find the biggest number in the last one. Then find the second small number in the second place, and then find the second largest number in the second place. And so on until the sort is finished.


The above is the content of cocktail sort (double bubble sort, stirring sort or ripple sort), more related content please follow topic.alibabacloud.com (www.php.cn)!

  • 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.