1
/// <summary>
/// 与传统比较的排序算法不一样的排序的手段,使用下标来确定正确位置的排序方法
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private static int[] CountSort(int[] array) 2 { 3 int max = array[0]; 4 int min = array[0]; 5 // 找出最大的数 6 for (int i = 1; i < array.Length; i++) 7 { 8 if (max < array[i]) 9 { 10 max = array[i]; 11 } 12 if (min > array[i]) 13 { 14 min = array[i]; 15 } 16 } 17 int[] countArray = new int[max - min + 1]; 18 for (int i = min; i < array.Length; i++) 19 { 20 countArray[array[i]]++; 21 } 22 23 24 int index = 0; 25 for (int i = min; i < countArray.Length; i++) 26 { 27 for (int j = 0; j < countArray[i]; j++) 28 { 29 array[index++] = i; 30 } 31 } 32 return array; 33 }