public static int[] counting(int[] a, int k)
    {
        if (a == null || k < 1)
        {
            throw new IllegalArgumentException();
        }
        if (a.length == 0)
        {
            return new int[0];
        }
        if (a.length == 1)
        {
            return new int[] { a[0] };
        }
        int[] c = new int[k];
        for (int i = 0; i < a.length; ++i)
        {
            if (a[i] >= k)
            {
                throw new IllegalArgumentException();
            }
            ++c[a[i]];
        }
        for (int i = 1; i < c.length; ++i)
        {
            c[i] += c[i - 1];
        }
        int[] b = new int[a.length];
        for (int i = a.length - 1; i >= 0; --i)
        {
            b[--c[a[i]]] = a[i];
        }
        return b;
    }