Bubble sorting is one of the simple sorting algorithms. By comparing two adjacent elements, when the first element is larger than the second element, the first element is moved back (ascending, similarly, descending). After each sorting, there must be a maximum or minimum value at the end.

First bubble sort

``` 1         static void BubbleSort<T>(T[] arr)where T : IComparable<T>
2         {
3             //Determine the number of times to sort
4             for(int i = 0; i < arr.Length; i++)
5             {
6                 //Every time the platoon is reduced I Elements, I After sorting
7                 for(int j=0;j<arr.Length-i-1;j++)
8                 {
9                     if(arr[j].CompareTo(arr[j+1])>0)
10                     {
11                         //Exchange algorithm
12                         var temp = arr[j];
13                         arr[j] = arr[j + 1];
14                         arr[j + 1] = temp;
15                     }
16
17                 }
18                 Print(arr);
19             }
20         }```

This kind of bubbling sorting can be further optimized, because after sorting, it is still sorting. So you can define a variable in the outer layer to indicate whether it is sorted or not. Here, use the bool value.

``` 1     static void BubbleSort<T>(T[] arr) where T : IComparable<T>
2         {
3             //Determine the number of times to sort
4             for (int i = 0; i < arr.Length; i++)
5             {
6                 //Define a to determine if sorting is in progress
7                 var Sorting = false;
8                 //Every time the platoon is reduced I Elements, I After sorting
9                 for (int j = 0; j < arr.Length - i - 1; j++)
10                 {
11                     if (arr[j].CompareTo(arr[j + 1]) > 0)
12                     {
13                         //Exchange algorithm
14                         var temp = arr[j];
15                         arr[j] = arr[j + 1];
16                         arr[j + 1] = temp;
17                         Sorting = true;
18                     }
19
20                 }
21                 if(Sorting==false)
22                 {
23                     return;
24                 }
25                 Print(arr);
26             }
27         }```

There is a third optimization method for bubble sorting. After sorting, when the sorting is finished, there is no need to continue the comparison.

``` 1         static void BubbleSort<T>(T[] arr)where T : IComparable<T>
2         {
3             //Record the last swap location
4             int lastExChangeIndex = 0;
5             //The boundary of an unordered sequence stops every time it is compared here
6             int sortBorder = arr.Length - 1;
7             //Decide how many times to go
8             for(int i=0;i<arr.Length;i++)
9             {
10                 var Sorting = false;
11                 //Determine the number of comparisons. After each sorting, there must be the largest one at the back, so every time you go-i
12                 for (int j=0;j< sortBorder; j++)
13                 {
14                     if(arr[j].CompareTo(arr[j+1])>0)
15                     {
16                         //Exchange algorithm
17                         var temp = arr[j];
18                         arr[j] = arr[j + 1];
19                         arr[j + 1] = temp;
20                         //Sorting
21                         Sorting=true;
22                         //Update the boundary of the disordered sequence to the position of the last exchange element
23                         lastExChangeIndex = j;
24                     }
25                 }
26                 sortBorder = lastExChangeIndex;
27                 //Exit if sorting is complete
28                 if(Sorting==false)
29                 {
30                     return;
31                 }
32                 Print(arr);
33             }
34         }```