冒泡排序

it2022-05-05  172

效果图: 输出值: 源码:

public static void TestMaoPaoZA() { int temp = 0; int[] arr = { 21, 99, 66, 86, 98, 19, 3, 9, 7 }; Console.WriteLine("·························"); Console.WriteLine("·························"); Console.WriteLine("排序前的数组:"); foreach (int item in arr) { Console.Write(item + ","); } Console.WriteLine(); for (int i = 0; i < arr.Length - 1; i++) { for (int j = 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("·························"); Console.WriteLine("·························"); Console.WriteLine("排序后的数组:"); foreach (int item in arr) { Console.Write(item + ","); } Console.WriteLine(); Console.ReadKey(); }

最新回复(0)