冒泡排序(C#实现)

冒泡排序算法的思想如下:
1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
3.重复步骤1和2,除了最后一个。
4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较
 
C#实现:
 1         internal static int[] BubbleSortMethod(int[] inputArray)
 2         {
 3             for (int ensureCount = 0; ensureCount < inputArray.Length - 1; ensureCount++)
 4             {
 5                 for (int compareIndex = 0; compareIndex < inputArray.Length - ensureCount - 1; compareIndex++)
 6                 {
 7                     if (inputArray[compareIndex] > inputArray[compareIndex + 1])
 8                     {
 9                         int temp = inputArray[compareIndex];
10                         inputArray[compareIndex] = inputArray[compareIndex + 1];
11                         inputArray[compareIndex + 1] = temp;
12                     }
13                 }
14             }
15             return inputArray;
16         }


冒泡算法是一种稳定的排序算法。

冒泡排序最好的时间复杂度是,最坏时间复杂度为,平均时间复杂度为

原文地址:https://www.cnblogs.com/ByronsHome/p/3527033.html