选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。
最差时间复杂度 |
О(n²) |
最优时间复杂度 |
О(n²) |
平均时间复杂度 |
О(n²) |
最差空间复杂度 |
О(n) total, O(1) |
1 public static void Selection_Sort(int[] arr) 2 { 3 int pos=0; 4 int temp; 5 for (int i = 0; i < arr.Length-1; i++) 6 { 7 pos = i; 8 for (int j = i + 1; j < arr.Length; j++) 9 { 10 if (arr[j]<arr[pos]) 11 { 12 pos = j; 13 } 14 } 15 temp = arr[i]; 16 arr[i] = arr[pos]; 17 arr[pos] = temp; 18 } 19 }
原文:http://www.cnblogs.com/dreamtaker/p/7470781.html