冒泡排序
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 冒泡排序 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] oldArray = { 23, 44, 66, 76, 98, 11, 3, 9, 7 }; 14 Console.Write("原始数组:"); 15 foreach (var item in oldArray) 16 { 17 Console.Write(item + " "); 18 } 19 PrintArray(oldArray); 20 21 Console.ReadKey(); 22 } 23 public static void PrintArray(int[] array) 24 { 25 int temp = 0; 26 for (int i = 0; i < array.Length; i++) 27 { 28 for (int j = 0; j < array.Length - 1 - i; j++) 29 { 30 if (array[j] > array[j + 1]) 31 { 32 temp = array[j]; 33 array[j] = array[j + 1]; 34 array[j + 1] = temp; 35 } 36 }; 37 Console.WriteLine(); 38 Console.WriteLine(); 39 Console.Write("第"+(i+1)+"次排序后的结果:"); 40 foreach (var item in array) 41 { 42 Console.Write(item + " "); 43 } 44 } 45 } 46 } 47 }