Baidu Encyclopedia
Bubble sort is often the test of a written interview, although it is the slowest sort in these algorithms
Principle: From the beginning, each element is compared to its next element, and if it is large, it will be exchanged with the elements of the comparison, otherwise it won't move.
This means that a large element always moves backwards until it encounters a larger element than it does. So each turn can be completed to the maximum value
To the end. Original source: https://www.cnblogs.com/wangjiahong/p/3570465.html冒泡算法Cnamespace数组排序{ classProgram { staticvoidMain(string[] args) { inttemp = 0; int[] arr = {23, 44, 66, 76, 98, 11, 3, 9, 7}; for(inti = 0; i < arr.Length - 1; i++) { #region将大的数字移到数组的arr.Length-1-i for(intj = 0; j < arr.Length - 1 - i; j++) { if(arr[j] > arr[j + 1]) { temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } Console.WriteLine("排序后的数组:"); foreach (intitem inarr) { Console.Write(item+""); } Console.WriteLine(); Console.ReadKey(); } }}
C # bubble sort (full code)