コード例 #1
0
ファイル: SortingClass.cs プロジェクト: neolee11/Algorithm
        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];
        }
コード例 #2
0
ファイル: SortingClass.cs プロジェクト: neolee11/Algorithm
        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;
            }
        }