static void Main(string[] args) { //从小到大 int[] num = new int[10] { 21, 13, 34, 25, 36, 17, 84, 2, 11, 43 }; for (int i = 0; i < num.Length; i++) { for (int j = i + 1; j < num.Length; j++) { if (num[i] > num[j]) { int temp = num[i]; num[i] = num[j]; num[j] = temp; } } } foreach (var item in num) { Console.WriteLine(item); } }
static void selection_sort<T>(T[] arr) where T : System.IComparable { int i, j, min, len = arr.Length; T temp; for (i = 0; i < len - 1; i++) { min = i; for (j = 0; j < len; j++) { if (arr[min].CompareTo(arr[j]) > 0) { min = j; } temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } }
原文:https://www.cnblogs.com/nanguoyezi/p/9369101.html