先列举下最简单的冒泡排序:
n个数需要排 n-1 趟;
第t趟比较的次数为:n-t次- class Program
- {
- static void Main(string[] args)
- {
- int[] array = { 2,4,6,8,9,23,32,45,67,0,56};
- for (int i = 0; i < array.Length - 1; i++)
- {
- for (int j = 0; j < array.Length - 1 - i; j++)
- {
- if (array[j] < array[j + 1])
- {
- int temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- for (int i=0;i<array.Length-1;i++)
- {
- Console.WriteLine (array[i]);
- }
- Console.ReadKey();
- }
复制代码 |