算法排序
1.冒泡排序
冒泡排序是将对数组相邻的元素进行比较,将最大的数或最小的数向后移动,就像气泡一样一步一步的移动,这样循环多次直至排好序列。
1 class Program 2 { 3 static void Maopao(int[] arr) 4 { 5 bool f = false; 6 do 7 { 8 f = false; 9 for (int i = 1; i < arr.Length - 1; i++) 10 { 11 if (arr[i] > arr[i + 1]) 12 { 13 int temp = arr[i]; 14 arr[i] = arr[i + 1]; 15 arr[i + 1] = temp; 16 f = true; 17 } 18 } 19 } while (f); 20 } 21 static void Main(string[] args) 22 { 23 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 }; 24 Maopao(list); 25 foreach (var item in list) 26 { 27 Console.WriteLine(item); 28 } 29 Console.ReadKey(); 30 } 31 }
1 class Program 2 { 3 static void Maopao(int[] arr) 4 { 5 bool f = false; 6 do 7 { 8 f = false; 9 for (int i = 1; i < arr.Length - 1; i++) 10 { 11 if (arr[i] > arr[i + 1]) 12 { 13 int temp = arr[i]; 14 arr[i] = arr[i + 1]; 15 arr[i + 1] = temp; 16 f = true; 17 } 18 } 19 } while (f); 20 } 21 static void Main(string[] args) 22 { 23 int[] list = new int[] { 2, 5, 1, 8, 45, 23, 20, 12 }; 24 Maopao(list); 25 foreach (var item in list) 26 { 27 Console.WriteLine(item); 28 } 29 Console.ReadKey(); 30 } 31 }