#t76. 初级桶排序

初级桶排序

Beginner's Bucket Sort

Problem Description

Little J has finally learned about arrays, and he knows that he can use an array to sort a sequence of numbers!

Today he gives it a try, using an array to sort nn numbers. Can you do it too?

Input Format

The first line contains a positive integer n(0<n≤104)n(0 \lt n \le 10^4).

The second line contains nn consecutive non-negative integers, each not greater than 10410^4.

Output Format

One line: output the nn numbers sorted from smallest to largest, separated by single spaces. If a number appears multiple times, write it only once in the sorted result.

Sample Input 1

5
3 7 0 2 4

Sample Output 1

0 2 3 4 7

Sample Input 2

5
1 2 1 2 1

Sample Output 2

1 2

Bucket Sort

By observation we find that the nn consecutive non-negative integers (each ≤104\le 10^4) are exactly suitable to be used as array indices.

So far we have only one way of using arrays:

a[i] = x; means the ii-th number is xx.

Now let's think: can we also use it the other way around?

c[x]=1; — what does that represent?

It means that xx appears 11 time in array aa.

We call the array cc a "bucket", used to record how many times a number appears in another array.

We let ii start from 00 and loop until the maximum value among the inputs; as long as c[i]c[i] is not 00, we output ii.

Special note: the size of the cc array depends on the maximum value among the inputs, and all input numbers must be natural numbers that can serve as array indices.