Problem:
Counting Sort

Counting sort can be used for sorting elements in an array which each of the n input elements is an integer in the range 0 to k. The idea of counting sort is to determine, for each input element x, the number of elements less than x as C[x]. This information can be used to place element x directly into its position in the output array B. This scheme must be modified to handle the situation in which several elements have the same value. Please see the following pseudocode for the detail:

Counting-Sort(A, B, k)
1 for i = 0 to k
2 do C[i] = 0
3 for j = 1 to length[A]
4 do C[A[j]] = C[A[j]]+1
5 /* C[i] now contains the number of elements equal to i */
6 for i = 1 to k
7 do C[i] = C[i] + C[i-1]
8 /* C[i] now contains the number of elements less than or equal to i */
9 for j = length[A] downto 1
10 do B[C[A[j]]] = A[j]
11 C[A[j]] = C[A[j]]-1

Write a program which sorts elements of given array ascending order based on the counting sort.

Input

The first line of the input includes an integer n, the number of elements in the sequence.

In the second line, n elements of the sequence are given separated by spaces characters.

Output

Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

Constraints

1 ≤ n ≤ 2,000,000

0 ≤ A[i] ≤ 10,000

Sample Input 1

7
2 5 1 3 2 3 0

Sample Output 1

0 1 2 2 3 3 5

Solution in C (judge verdict: Accepted)

// AOJ ALDS1_6_A Counting Sort
// 2018.4.27 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

char buf[20]; int w;
void mypr(int n)
{
	int i;
	w = 0; while (n) buf[w++] = n%10 + '0', n/=10;
	i = w; while (i--) pc(buf[i]);
}

// 計数ソート（整数用）昇順
#define MAX_N 10002
int f[MAX_N];
void counting_sort(int n, int *a, int *b, int max)
{
	int i, x;
//	memset(f, 0, sizeof(f));
	for (i = 0; i < n; i++) f[a[i]]++;
	for (i = 1; i <= max; i++) f[i] += f[i-1];
	for (i = n-1; i >= 0; i--) {
		x = a[i], b[--f[x]] = x;
	}
}

int a[2000002], b[2000002];

int main()
{
	int N, i, j;

	N = in();
	for (i = 0; i < N; i++) a[i] = in();
	counting_sort(N, a, b, 10000);
	if (!b[0]) pc('0'); else mypr(b[0]);
	for (i = 1; i < N; i++) {
		pc(' ');
		if (!b[i]) pc('0');
		else if (b[i] != b[i-1]) mypr(b[i]);
		else { j = w; while (j--) pc(buf[j]); }
	}
	pc('\n');
	return 0;
}