Counting Sort

Sort an array of non-negative integers using counting sort for linear time performance.

Code

Algorithms
const max = Math.max(...arr);
const count = new Array(max + 1).fill(0);
arr.forEach(x => count[x]++);
const result = [];
count.forEach((c, i) => { for (let j = 0; j < c; j++) result.push(i); });
return result;

Parameters

Array of integers

Browser·fetch() may be limited by CORS

More JavaScript Snippets