def countingsort(a):
"""
Sort array a of integers from 0 to k using the counting sort algorithm.
"""
# initialize array counts
k = 1000
counts = [0 for i in range(k+1)]
# count keys
for i in range(len(a)):
counts[a[i]] += 1
# fill array in sorted order
i = 0
for j in range(k+1):
for l in range(counts[j]):
a[i] = j
i += 1
return a