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 wh Ile (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 within the index's bounds on the rightmost for (i = Low; I < up; i++)//scan from top to bottom {if (Myarray[i] > myarray[i + 1]) { Swap (ref myarray[i], ref myarray[i + 1]); index = i;//record Current Index}} up = index;//Record the position of the last interchange//Enter the for loop to place the smallest element within the index's bounds on the leftmost for (i = up; i > Low; i--)//from the last switching position from the bottom up scan {if (Myarray[i] < myarray[i-1]) {Swap (ref m Yarray[i], ref myarray[i-1]); index = i; }} low = index;//record the position of the last Interchange}} private static void Swap (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.
Jiankunking Source: http://blog.csdn.net/jiankunking
Cocktail sort (double bubble sort, stir sort or ripple sort)