Counting Sort is a method used to sort elements in an array where each input element is an integer within the range of 0 to k. The algorithm works by counting the number of elements less than each input element x and then placing x directly into its position in the output array B. The algorithm needs to be adjusted to handle situations where multiple elements have the same value. The pseudocode provided outlines the steps for implementing Counting Sort. The task is to write a program that sorts the elements of a given array in ascending order using Counting Sort.

**Input:**
The first line of the input contains an integer n, representing the number of elements in the sequence. The second line contains n elements of the sequence separated by spaces.

**Output:**
Print the sorted sequence, with each pair of contiguous elements separated by a space.

**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

# Buggy version:
n = int(input())
l = list(map(int,input().split()))
bucket = [0]*(n+1)
for i in range(n+1):
    for j in range(n):
        if i == l[j]:
            bucket[i] += 1
            flg = i
print(bucket)
for i in range(flg):
    for j in range(bucket[i]):
        print(i,end = ' ')
for i in range(bucket[flg]-1):
    print(flg,end=' ')
print(flg)



# Fixed version:
flg = 0
n = int(input())
l = list(map(int,input().split()))
bucket = [0]*10000
for i in range(n):
    bucket[l[i]] += 1
    if flg < l[i]:
        flg = l[i]
for i in range(flg):
    for j in range(bucket[i]):
        print(i,end = ' ')
for i in range(bucket[flg]-1):
    print(flg,end=' ')
print(flg)

# Buggy version:
def counting_sort(MAX):
    C = [0 for _ in range(MAX+1)]

    for i in range(N):
        C[A[i]] += 1

    for i in range(1, MAX+1):
        C[i] = C[i] + C[i-1]

    for i in range(N-1, -1, -1):
        C[A[i]] -= 1
        B[C[A[i]]] = A[i]

N = int(input())
A = list(map(int, input().split()))

MAX = 0
for n in A:
    if MAX < n:
        MAX = n

B = [0 for _ in range(N)]
counting_sort(MAX)
print(" ".join(B))


# Fixed version:
def counting_sort(MAX):
    C = [0 for _ in range(MAX+1)]

    for i in range(N):
        C[A[i]] += 1

    for i in range(1, MAX+1):
        C[i] = C[i] + C[i-1]

    for i in range(N-1, -1, -1):
        C[A[i]] -= 1
        B[C[A[i]]] = A[i]

N = int(input())
A = list(map(int, input().split()))

MAX = 0
for n in A:
    if MAX < n:
        MAX = n

B = [0 for _ in range(N)]
counting_sort(MAX)
print(" ".join(map(str, B)))

# Buggy version:
def counting_sort(A, k):
    B = [0] * len(A)
    C = [0] * k

    for a in A:
        C[a] += 1

    for i in range(1, k):
        C[i] += C[i - 1]

    for a in reversed(A):
        print(a, C[a])
        B[C[a] - 1] = a
        C[a] -= 1

    return B


def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    print(*counting_sort(A, 11))


main()


# Fixed version:
def counting_sort(A, k):
    B = [0] * len(A)
    C = [0] * k

    for a in A:
        C[a] += 1

    for i in range(1, k):
        C[i] += C[i - 1]

    for a in A:
        B[C[a] - 1] = a
        C[a] -= 1

    return B


def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    print(*counting_sort(A, 10001))


main()

# Buggy version:
def InputData():
    
    sequence_len = int(input())
    sequence = [None] + [int(a) for a in input().split(" ")]

    return sequence_len, sequence

def countingSort(sequence, k, sequence_len):
    
    B = [None]*(sequence_len+1)
    C = [0]*k
    
    for j in range(1, sequence_len+1):
        C[sequence[j]] += 1
    
    for i in range(1, k):
        C[i] = C[i] + C[i-1]
    
    for j in range(sequence_len, 0, -1):
        B[C[sequence[j]]] = sequence[j]
        C[sequence[j]] -= 1

    return B

def PrintOut(sequence):
    print(' '.join( map(str, sequence)))

def main():
    [sequence_len, sequence] = InputData()
    sequence_out = countingSort(sequence, 10000, sequence_len)
    PrintOut(sequence[1:])

if __name__=="__main__":
    main()


# Fixed version:
def InputData():
    
    sequence_len = int(input())
    sequence = [None] + [int(a) for a in input().split(" ")]

    return sequence_len, sequence

def countingSort(sequence, k, sequence_len):
    
    B = [None]*(sequence_len+1)
    C = [0]*k
    
    for j in range(1, sequence_len+1):
        C[sequence[j]] += 1
    
    for i in range(1, k):
        C[i] = C[i] + C[i-1]
    
    for j in range(sequence_len, 0, -1):
        B[C[sequence[j]]] = sequence[j]
        C[sequence[j]] -= 1

    return B

def PrintOut(sequence):
    print(' '.join( map(str, sequence)))

def main():
    [sequence_len, sequence] = InputData()
    sequence_out = countingSort(sequence, 10000, sequence_len)
    PrintOut(sequence_out[1:])

if __name__=="__main__":
    main()

# Buggy version:
def counting_sort(lst,ans,max):
    cnt_lst = [0] * (max+1)
    for i in range(1,len(lst)):
        cnt_lst[lst[i]] += 1
    for j in range(1,max+1):
        cnt_lst[j] = cnt_lst[j] + cnt_lst[j-1]

    for k in range(len(lst)-1,0,-1):
        ans[cnt_lst[lst[k]]] = lst[k]
        cnt_lst[lst[k]] -= 1
    return ans

n = int(input())
n_lst = list(map(int,input().split()))
ans = [0] * len(n_lst)
ans = map(str,counting_sort(n_lst,ans,max(n_lst)))
print(' '.join(ans))


# Fixed version:
def counting_sort(lst,ans,max):
    cnt_lst = [0] * (max+1)
    for i in range(0,len(lst)):
        cnt_lst[lst[i]] += 1
    for j in range(1,max+1):
        cnt_lst[j] = cnt_lst[j] + cnt_lst[j-1]

    for k in range(len(lst)-1,-1,-1):
        ans[cnt_lst[lst[k]]-1] = lst[k]
        cnt_lst[lst[k]] -= 1
    return ans

n = int(input())
n_lst = list(map(int,input().split()))
ans = [0] * len(n_lst)
ans = map(str,counting_sort(n_lst,ans,max(n_lst)))
print(' '.join(ans))

# Buggy version:
def countingsort(a,k):
    c=[0]*(k+1)
    n=len(a)
    for i in a:
        c[i]+=1
    for i in range(1,k+1):
        c[i]=c[i]+c[i-1]
    b=[0]*n
    for i in range(n-1,-1,-1):
        b[c[a[i]]-1]=a[i]
        c[a[i]]-=1
    return b
o=int(input())
a=list(map(int,input().split()))
q=max(a)
ret=countingsort(a,q)
for i in range(n):
    if i!=n-1:
        print(ret[i],end=" ")
    else:
        print(ret[i])


# Fixed version:
def countingsort(a,k):
    c=[0]*(k+1)
    n=len(a)
    for i in a:
        c[i]+=1
    for i in range(1,k+1):
        c[i]=c[i]+c[i-1]
    b=[0]*n
    for i in range(n-1,-1,-1):
        b[c[a[i]]-1]=a[i]
        c[a[i]]-=1
    return b
o=int(input())
a=list(map(int,input().split()))
q=max(a)
ret=countingsort(a,q)
for i in range(o):
    if i!=o-1:
        print(ret[i],end=" ")
    else:
        print(ret[i])

# Buggy version:
#計数ソート
n = int(input())
a = list(map(int, input().split()))

def countSort(a):
    N = max(a)
    c = [0]*(N+2)
    b = [0]*len(a)
    for i in a:
        c[i] += 1
    for i in range(1,len(c)):
        c[i] += c[i-1]
        
    for i in a:
        b[c[i]-1] = i
        c[i] -= 1
    print(b)

countSort(*a)


# Fixed version:
#計数ソート
n = int(input())
a = list(map(int, input().split()))

def countSort(a):
    N = max(a)
    c = [0]*(N+2)
    b = [0]*len(a)
    for i in a:
        c[i] += 1
    for i in range(1,len(c)):
        c[i] += c[i-1]
        
    for i in a:
        b[c[i]-1] = i
        c[i] -= 1
    print(*b)

countSort(a)

# Buggy version:


def counting_sort(A, k):
    B = [0 for i in range(len(A))]
    C = [0 for i in range(0, k + 1)]

    # C[i]???i???????????°????¨??????????
    for j in range(1, n):
        C[A[j]] += 1

    # C[i]???i??\????????°???????????°????¨??????????
    for i in range(1, k + 1):
        C[i] +=  C[i - 1]

    for j in reversed(range(1, n)):
        B[C[A[j]]] = A[j]
        C[A[j]] -= 1
    return B
n = int(input())
A = list(map(int, input().split()))
k = max(A)

print(counting_sort(A, B, k))

# Fixed version:


def counting_sort(A, k):
    n = len(A)
    B = ['0' for i in range(n + 1)]
    C = [0 for i in range(0, k + 1)]

    # C[i]???i???????????°????¨??????????
    for j in range(0, n):
        C[A[j]] += 1

    # C[i]???i??\????????°???????????°????¨??????????
    for i in range(0, k):
        C[i + 1] +=  C[i]

    for j in reversed(range(0, n)):
        B[C[A[j]]] = str(A[j])
        C[A[j]] -= 1
    return ' '.join(B[1:])

n = int(input())
A = list(map(int, input().split()))
k = max(A)
print(counting_sort(A, k))

# Buggy version:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


def counting_sort(A, k):
    B = [0] * k
    C = [0] * k
    for j in range(array_length):
        C[A[j]] += 1
    print('1', C[:array_length])

    for i in range(1, k):
        C[i] += C[i - 1]
    print('2', C[:array_length])

    for m in range(array_length - 1, -1, -1):
        B[C[A[m]]] = A[m]
        C[A[m]] -= 1

    return B[1:array_length + 1]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    assert len(array) == array_length
    MAX_ELEM = int(1e4 + 1)
    result = counting_sort(A=array, k=MAX_ELEM)
    print(*result)

# Fixed version:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


def counting_sort(A, max_occurance):
    result = [0] * array_length
    count_list = [0] * max_occurance

    for j in range(array_length):
        count_list[A[j]] += 1
    # print('1', C[:array_length])

    for i in range(1, max_occurance):
        count_list[i] += count_list[i - 1]
    # print('2', C[:array_length])

    for m in array[::-1]:
        result[count_list[m] - 1] = m
        count_list[m] -= 1

    return result


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    # assert len(array) == array_length
    max_elem = int(1e4)
    result = counting_sort(A=array, max_occurance=max_elem)
    print(*result)

# Buggy version:
def CountingSort(A, k):
    C = [0] * k
    B = C[:]
    for a in A:
        C[a] += 1
    for i in range(1, len(C)):
        C[i] += C[i - 1]
    for i in range(len(A) - 1, -1, -1):
        B[C[A[i]]] = A[i]
        C[A[i]] -= 1
    return B




if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().strip().split()]
    sorted_A = CountingSort(A, int(3E10))
    print(' '.join([str(a) for a in sorted_A[1:len(A) + 1]]))

# Fixed version:
def CountingSort(A, k):
    C = [0] * k
    B = C[:]
    for a in A:
        C[a] += 1
    for i in range(1, len(C)):
        C[i] += C[i - 1]
    for i in range(len(A) - 1, -1, -1):
        B[C[A[i]]] = A[i]
        C[A[i]] -= 1
    return B



if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().strip().split()]
    sorted_A = CountingSort(A, int(3E6))
    print(' '.join([str(a) for a in sorted_A[1:len(A) + 1]]))

# Buggy version:
def CountingSort(A, B, k):
    C = [0 for i in range(k+1)]
    n = len(A)
    #C[i]???i???????????°????¨??????????
    for j in range(n):
        C[A[j]] += 1

    #C[i]???i??\????????°???????????°????¨??????????
    for i in range(1, k+1):
        C[i] = C[i] + C[i-1]

    for j in range(n)[::-1]:
        B[C[A[j]]-1] = A[j]
        print(C[A[j]])
        C[A[j]] -= 1


n = int(input())
A = list(map(int, input().split()))
B = [0 for i in range(n)]
CountingSort(A, B, max(A))
print(*B)

# Fixed version:
def CountingSort(A, B, k):
    C = [0 for i in range(k+1)]
    n = len(A)
    #C[i]???i???????????°????¨??????????
    for j in range(n):
        C[A[j]] += 1

    #C[i]???i??\????????°???????????°????¨??????????
    for i in range(1, k+1):
        C[i] = C[i] + C[i-1]

    for j in range(n)[::-1]:
        B[C[A[j]]-1] = A[j]
        #print(C[A[j]])
        C[A[j]] -= 1


n = int(input())
A = list(map(int, input().split()))
B = [0 for i in range(n)]
CountingSort(A, B, max(A))
print(*B)

# Buggy version:
def CountingSort(A, B, k):
    C = [0 for i in range(k+2)]
    
    for j in range(n):
        C[A[j]] += 1
        
    for i in range(k+1):
        C[i] += C[i-1]
        
    for j in range(n-1, -1, -1):
        B[C[A[j]]-1] = A[j]
        C[A[j]] -= 1

if __name__=="__main__":
    n = int(input())
    D = list(map(int, input().split()))
    l = max(D)
    E = D[:]
    CountingSort(D, E, l)
    print(" ".join(map, str(E))))

# Fixed version:
def CountingSort(A, B, k):
    C = [0 for i in range(k+2)]
    
    for j in range(n):
        C[A[j]] += 1
        
    for i in range(k+1):
        C[i] += C[i-1]
        
    for j in range(n-1, -1, -1):
        B[C[A[j]]-1] = A[j]
        C[A[j]] -= 1

if __name__=="__main__":
    n = int(input())
    D = list(map(int, input().split()))
    l = max(D)
    E = D[:]
    CountingSort(D, E, l)
    print(" ".join(map(str, E)))

# Buggy version:
def CountingSort(A, B, k, n):
    C = [0 for i in range(k)]   #  for i = 0 to k { C[i] = 0 }

    # /* C[i] ??? i ???????????°????¨?????????? */
    for j in range(n):
        C[A[j]] += 1
        print("j:{} Aj:{} C[Aj]:{}".format(j,A[j], C[A[j]]))

    # /* C[i] ??? i ??\????????°???????????°????¨??????????*/
    for i in range(k):
        print("\ti:{} Ci:{} Ci-1:{}".format(i,C[i], C[i-1]))
        C[i] = C[i] + C[i-1]

    for j in reversed(range(n)):
        print("j:{} Aj:{} C[Aj]:{} k:{}".format(j,A[j], C[A[j]],k))
        B[C[A[j]]] = A[j]
        C[A[j]] -= 1

def main():
    """ ????????? """
    num = int(input().strip())
    A = list(map(int,input().split()))
    max = 0
    for i in range(num):
        if max < A[i]:
            max = A[i]
    if max < num:
        max = num
    max += 2
    B = [0 for i in range(num + 2)]
    CountingSort(A, B, max, num)
    print(" ".join(map(str,B[1:num+1])))

if __name__ == '__main__':
    main()

# Fixed version:
def CountingSort(A, B, k, n):
    C = [0 for i in range(k)]   #  for i = 0 to k { C[i] = 0 }

    # /* C[i] ??? i ???????????°????¨?????????? */
    for j in range(n):
        C[A[j]] += 1
#        print("j:{} Aj:{} C[Aj]:{}".format(j,A[j], C[A[j]]))

    # /* C[i] ??? i ??\????????°???????????°????¨??????????*/
    for i in range(k):
#        print("\ti:{} Ci:{} Ci-1:{}".format(i,C[i], C[i-1]))
        C[i] = C[i] + C[i-1]

    for j in reversed(range(n)):
#        print("j:{} Aj:{} C[Aj]:{} k:{}".format(j,A[j], C[A[j]],k))
        B[C[A[j]]] = A[j]
        C[A[j]] -= 1

def main():
    """ ????????? """
    num = int(input().strip())
    A = list(map(int,input().split()))
    max = 0
    for i in range(num):
        if max < A[i]:
            max = A[i]
    if max < num:
        max = num
    max += 2
    B = [0 for i in range(num + 2)]
    CountingSort(A, B, max, num)
    print(" ".join(map(str,B[1:num+1])))

if __name__ == '__main__':
    main()

# Buggy version:
# -*- coding:utf-8 -*-


def countingSort(A, B, k, n):
    C = [0] * (k+1)

    for i in range(1, n + 1):
        C[A[i]] += 1

    for j in range(1, k + 1):
        C[j] = C[j] + C[j - 1]

    print(C)
    for j in range(n, 0, -1):
        B[C[A[j]]] = A[j]
        C[A[j]] -= 1


if __name__ == "__main__":
    n = int(input())
    lst = [int(val) for val in input().split()]
    lst.insert(0, 0)
    result = [0] * (n + 1)
    k = max(lst)
    countingSort(lst, result, k, n)
    result.pop(0)
    print(" ".join([str(val) for val in result]))

# Fixed version:
# -*- coding:utf-8 -*-


def countingSort(A, B, k, n):
    C = [0] * (k+1)

    for i in range(1, n + 1):
        C[A[i]] += 1

    for j in range(1, k + 1):
        C[j] = C[j] + C[j - 1]

    for j in range(n, 0, -1):
        B[C[A[j]]] = A[j]
        C[A[j]] -= 1


if __name__ == "__main__":
    n = int(input())
    lst = [int(val) for val in input().split()]
    lst.insert(0, 0)
    result = [0] * (n + 1)
    k = max(lst)
    countingSort(lst, result, k, n)
    result.pop(0)
    print(" ".join([str(val) for val in result]))

# Buggy version:
n = int(input())
a = list(map(int, input().split()))


def countingsort(a, k):
    c = [0 for i in range(k)]
    b = [0 for i in range(n)]

    for j in range(n):
        c[a[j]] += 1

    for i in range(k):
        c[i+1] += c[i]

    for j in reversed(range(n)):
        b[c[a[j]]] = a[j]
        c[a[j]] -= 1
    
    return b

b = countingsort(a, 10001)
print(*b)


# Fixed version:
n = int(input())
a = list(map(int, input().split()))


def countingsort(a, k):
    c = [0 for i in range(k+1)]
    b = [0 for i in range(n)]

    for j in range(n):
        c[a[j]] += 1

    for i in range(k):
        c[i+1] += c[i]

    for j in reversed(range(n)):
        b[c[a[j]]-1] = a[j]
        c[a[j]] -= 1

    return b

b = countingsort(a, 10000)
print(*b)

# Buggy version:
def counting_sort(A, B, k):
    len_A = len(A)

    C = [0 for _ in range(k + 1)]

    for j in range(len_A):
        C[A[j]] += 1

    for i in range(1, k + 1):
        C[i] = C[i] + C[i - 1]

    for j in range(len_A - 1, -1, -1):
        B[C[A[j]] - 1] = A[j]
        C[A[j]] -= 1


n = int(input())
input_list = list(map(int, input().split(' ')))

B = [0 for _ in range(n)]

counting_sort(input_list, B, max(input_list) + 1)

print(B)


# Fixed version:
def counting_sort(A, B, k):
    len_A = len(A)

    C = [0 for _ in range(k + 1)]

    for j in range(len_A):
        C[A[j]] += 1

    for i in range(1, k + 1):
        C[i] = C[i] + C[i - 1]

    for j in range(len_A - 1, -1, -1):
        B[C[A[j]] - 1] = A[j]
        C[A[j]] -= 1


n = int(input())
input_list = list(map(int, input().split(' ')))

B = [0 for _ in range(n)]

counting_sort(input_list, B, max(input_list) + 1)

print(*B)

# Buggy version:
def CountingSort(n, input_line, max_num):
    max_num += 1
    count_list = [0 for i in range(max_num)]
    output_line = [0 for i in range(n)]
    for i in range(n):
        count_list[input_line[i]] += 1
    for i in range(1,max_num):
        count_list[i] = count_list[i] + count_list[i-1]
    print(count_list)
    for i in range(n-1,-1, -1):
        output_line[count_list[input_line[i]]-1] = str(input_line[i])
        count_list[input_line[i]] -= 1
    return output_line

n = int(input())
input_line = [int(i) for i in input().split()]
max_num = max(input_line)
output_line = CountingSort(n, input_line, max_num)
print(' '.join(output_line))


# Fixed version:
def CountingSort(n, input_line, max_num):
    max_num += 1
    count_list = [0 for i in range(max_num)]
    output_line = [0 for i in range(n)]
    for i in range(n):
        count_list[input_line[i]] += 1
    for i in range(1,max_num):
        count_list[i] = count_list[i] + count_list[i-1]
    for i in range(n-1,-1, -1):
        output_line[count_list[input_line[i]]-1] = str(input_line[i])
        count_list[input_line[i]] -= 1
    return output_line

n = int(input())
input_line = [int(i) for i in input().split()]
max_num = max(input_line)
output_line = CountingSort(n, input_line, max_num)
print(' '.join(output_line))

# Buggy version:
n = int(input())
A = list(map(int, input().split()))

def countingSort(A, B, k):
    C = [0] * k
    for j in range(1, n):
        C[A[j]]+=1

    for i in range(1, k):
        C[i] = C[i] + C[i - 1]

    for j in range(1, n)[::-1]:
        B[C[A[j]]] = A[j]
        C[A[j]]-=1
    return B
        
print(' '.join(map(str, countingSort(A, [0] * n, 10000))))


# Fixed version:
n = int(input())
A = list(map(int, input().split()))

def CountingSort(A, B, k):
    C = [0] * k

    for j in range(n):
        C[A[j]] += 1

    for i in range(1, k):
        C[i] = C[i] + C[i-1]

    for j in range(n-1, -1, -1):
        B[C[A[j]] - 1] = A[j]
        C[A[j]] -= 1

B = [0] * n
CountingSort(A, B, 10000)

print(' '.join(map(str, B)))

# Buggy version:
def countingsort(a, b, k):
    c = [0 for i in range(k + 1)]
    for i in range(len(a)):
        c[a[i]] += 1
    for i in range(1, len(c)):
        c[i] += c[i - 1]
    for i in range(len(a) - 1, -1, -1):
        b[c[a[i]] - 1] = a[i]
        c[a[i]] -= 1

n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
countingsort(a, b, max(a))
print(*b)

# Fixed version:
def counting_sort(a,b,k):
  c = [0 for _ in range(k+1)]
  for i in range(len(a)):
    c[a[i]] += 1
  for i in range(1, len(c)):
    c[i] += c[i-1]
  for i in range(len(a) - 1, -1, -1):
    b[c[a[i]]- 1] = a[i]
    c[a[i]] -= 1

n = int(input())
a = list(map(int, input().split()))
b = [0 for _ in range(len(a))]
counting_sort(a, b, max(a))
print(*b)