1function countingSort(arr, k) {
2 const count = new Array(k + 1).fill(0);
3 for (const x of arr) count[x]++;
4 for (let i = 1; i <= k; i++) count[i] += count[i - 1];
5 const out = new Array(arr.length);
6 for (let i = arr.length - 1; i >= 0; i--) {
7 out[--count[arr[i]]] = arr[i];
8 }
9 return out;
10}