用了兩種形式的數據,一個是泛型List,一個是數據int[]。記錄一下,作為自己學習過程中的筆記。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 冒泡排序算法 { class Program { static void Main(string[] args) { List<int> _list = new List<int>() { 29, 20, 49, 84, 3, 48, 86, 95, 69, 28 }; Bubble(_list); foreach (var item in _list) { Console.WriteLine(item); } int[] bortargs = { 29, 20, 49, 84, 3, 48, 86, 95, 69, 28 }; Bubble(bortargs); foreach (var article in bortargs) { Console.WriteLine(article); } Console.ReadKey(); } static void Bubble(List<int> list) { int temp = 0; for (int i = list.Count; i > 0; i--) { for (int j = 0; j < i - 1; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } static void Bubble(int[] args) { int temp = 0; for (int i = args.Length; i > 0; i--) { for (int j = 0; j < i - 1; j++) { if (args[j] > args[j + 1]) { temp = args[j]; args[j] = args[j + 1]; args[j + 1] = temp; } } } } } }View Code