冒泡排序 作为一种渐进复杂度O(N^2)的排序方法, 实质上属于一种形式“玄妙的”选择排序,只是相比选择排序多了一些无意义的交换。它比选择排序更难于理解和解释(就在于它多次无意义的交换的干扰,不信你讲给楼下阿姨比较下),又不具有插入排序的“在线”优点,跟极易理解的基数排序更不能在思维难度上相比,就算同快排,归并相比,也具有复杂度高得多的劣势。那么为什么冒泡排序会作为经典,成为初学者学习语言的遇到的首个算法?
class Program
{
public Program()
{
}
static void Main(string[] args)
{
int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
int temp;
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[i])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", array[m]);
Console.ReadLine();
}
}