两种冒泡排序方法的调用
class Program { static int[] Method(int[] arr)//方法一,先去确定最大值,从后往前排 { for (int i = arr.Length - 1; i > 0; i--) { for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] > arr[j + 1]) { int k = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = k; } } } return arr; } static int[] Method2(int[] arr)//方法二,先确定最小值,从前往后排 { for (int i = 0; i < arr.Length - 1;i ++) { for (int j = i ; j <= arr.Length - 1;j++) { if (arr[i] > arr[j]) { int k = arr[i]; arr[i] = arr[j]; arr[j] = k; } } } return arr; } static void Main(string[] args) { int[] arr = new int[18]; for (int i = 0;i <= 17;i ++) { Random rd = new Random(); Thread.Sleep(200); arr[i] = rd.Next(100); } foreach (int i in arr) Console.Write(i + " "); Console.Write("\n"); //利用方法一输出结果 Console.WriteLine ("方法一输出从小到大:"); arr = Method(arr); foreach (int i in arr) Console.Write(i + " "); //利用方法二输出结果 Console.WriteLine("\n方法二输出从小到大:"); arr = Method2(arr); foreach (int i in arr) Console.Write(i + " "); Console.ReadLine(); } }