Algorithm.Sort.Sort.RadixSort10 C# (CSharp) Method

RadixSort10() public static method

基数ソート。 概念説明用の簡易版。 10進数で3桁(0~999)までしかソートできない。
public static RadixSort10 ( int a ) : void
a int 対象の配列
return void
        public static void RadixSort10(int[] a)
        {
            // バケツを用意
            List<int>[] bucket = new List<int>[10];

            for (int d = 0, r = 1; d < 3; ++d, r *= 10)
            {
                // バケツに値を入れる
                for (int i = 0; i < a.Length; ++i)
                {
                    int key = (a[i] / r) % 10; // a[i] の d 桁目だけを取り出す。
                    if (bucket[key] == null) bucket[key] = new List<int>();
                    bucket[key].Add(a[i]);
                }

                // バケツ中の値の結合
                for (int j = 0, i = 0; j < bucket.Length; ++j)
                    if (bucket[j] != null)
                        foreach (int val in bucket[j])
                            a[i++] = val;

                // バケツを一度空にする
                for (int j = 0; j < bucket.Length; ++j)
                    bucket[j] = null;
            }
        }