基数排序(C#) 代码实现: /// <summary> /// 基数排序 /// </summary> /// <param name="arrayToSort">要排序的数组</param> /// <param name="maxDigit">数字的最大位数</param> /// <returns>排序后的结果</returns> public static int[] RadixSort(int[] arrayToSort, int maxDigit) { for (int i = 0; i < maxDigit; i++) { int[] tempArray = new int[arrayToSort.Length]; int[] countingArray = new int[10]; // 初始化计数数组 for (int j = 0; j < 10; j++) { countingArray[j] = 0; } // 元素计数 for (int j = 0; j < arrayToSort.Length; j++) { int splitNum = (int)(arrayToSort[j] / Math.Pow(10, i)) - (int)(arrayToSort[j] / Math.Pow(10, i + 1)) * 10; countingArray[splitNum] = countingArray[splitNum] + 1; } // 计数小与等于某元素的个数 for (int j = 1; j < 10; j++) { countingArray[j] += countingArray[j - 1]; } for (int j = arrayToSort.Length - 1; j >= 0; j--) { int splitNum = (int)(arrayToSort[j] / Math.Pow(10, i)) - (int)(arrayToSort[j] / Math.Pow(10, i + 1)) * 10; int splitNumIndex = countingArray[splitNum] - 1; tempArray[splitNumIndex] = arrayToSort[j]; countingArray[splitNum] -= 1; } Array.Copy(tempArray, arrayToSort, tempArray.Length); } return arrayToSort; }