private static void CountingSortForRadixSortElement(RadixSortElement[] array) { int n = array.Length; int rangeUpperBound = 10; int[] countsArray = new int[rangeUpperBound + 1];//0...10 RadixSortElement[] sortedArray = new RadixSortElement[n]; for (int i = 0; i <= rangeUpperBound; i++) countsArray[i] = 0; for (int i = 0; i < n; i++) countsArray[array[i].CurrDigit] += 1; for (int i = 1; i <= rangeUpperBound; i++) countsArray[i] += countsArray[i - 1]; for (int i = n - 1; i >= 0; i--) { sortedArray[countsArray[array[i].CurrDigit] - 1] = array[i]; countsArray[array[i].CurrDigit]--; } for (int i = 0; i < n; i++) array[i] = sortedArray[i]; }
public static void RadixSort(int[] array) { int n = array.Length; int maxDigits = -1; RadixSortElement[] sortedArray = new RadixSortElement[n]; for (int i = 0; i < n; i++) { sortedArray[i] = new RadixSortElement(array[i]); if (sortedArray[i].TotalDigits > maxDigits) maxDigits = sortedArray[i].TotalDigits; } for (int i = 1; i <= maxDigits; i++) { for(int j = 0; j < n; j++) sortedArray[j].SetCurrDigit(i); CountingSortForRadixSortElement(sortedArray); } for (int i = 0; i < n; i++) { array[i] = sortedArray[i].OrigNum; } }