Sort foaming and cocktails

Keywords: Programming less

Sort foaming and cocktails

Long time no see.

Having spent the Qingming vacation and looking forward to the May Day holiday, I wish all programmers a pleasant vacation, mainly to maintain their health

Bubble sort

When you mention the sorting algorithm, you have to mention bubble sorting. It can be said that you are a beginner sorting algorithm. You haven't written bubble sorting for a long time, so take this opportunity to review it with everyone

Bubble sort, as the name implies, is like a bubble coming out, bubble sort, the first array, int arr[2,5,1,9,4,7]; two adjacent comparisons,
Large number If the previous number is larger than the latter, the swap position is changed, and vice versa, so after a cycle, the largest number of values is placed at the end of the array, and in the next cycle, the number at the end of the cycle is one less time, as in the previous array, the first cycle finishes int arr1[2,1,5,4,7,9]; the next cycle 9 does not participate in the cycle.Is bubble sort.

public static void BubbleSort(int [] arr){
 int temp;//Temporary variable
 for(int i=0; i<arr.length-1; i++){   //Represents the number of trips, arr.length-1 total.
     for(int j=arr.length-1; j>i; j--){

         if(arr[j] < arr[j-1]){
             temp = arr[j];
             arr[j] = arr[j-1];
             arr[j-1] = temp;
         }
     }
 }

}
Average Time Complexity O(n2)

Of course, it's not because of this bubble sort that I wrote this blog...

Cocktail sort

Algorithmic principle: The numbers in the array are arranged irregularly. First find the smallest number, put it first, then find the largest number and put it last.Then find the second smallest number and put the second largest number in the second last place.And so on until the sorting is complete.

Bubble sort equivalent to distortion

29         static List<int> CockTailSort(List<int> list)
30         {
31             //Because it is a two-way comparison, the number of comparisons is only 1/2 of the original array.
32             for (int i = 1; i <= list.Count / 2; i++)
33             {
34                 //Sort from front to back (ascending)
35                 for (int m = i - 1; m <= list.Count - i; m++)
36                 {
37                     //Swap if the front is larger than the back
38                     if (m + 1 < list.Count && list[m] > list[m + 1])
39                     {
40                         var temp = list[m];
41 
42                         list[m] = list[m + 1];
43 
44                         list[m + 1] = temp;
45                     }
46                 }
47 
48                 Console.WriteLine("Forward Sort => {0}", string.Join(",", list));
49 
50                 //Sort from back to front (descending)
51                 for (int n = list.Count - i - 1; n >= i; n--)
52                 {
53                     //Swap if the front is larger than the back
54                     if (n > 0 && list[n - 1] > list[n])
55                     {
56                         var temp = list[n];
57 
58                         list[n] = list[n - 1];
59 
60                         list[n - 1] = temp;
61                     }
62                 }
63 
64                 Console.WriteLine("Reverse sort => {0}", string.Join(",", list));
65             }
66 
67             return list;
68         }
69     }
70 }

Posted by brewmiser on Sat, 30 Nov 2019 02:34:48 -0800