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)!