Pigeonhole Sort

Distribution sort suitable when number of elements and range of possible values are roughly the same.

Best O(n+N) Avg O(n+N) Worst O(n+N) Space O(N) Stable Yes In-place No Distribution

How it works

Distribution sort suitable when number of elements and range of possible values are roughly the same.

Implementation

function pigeonholeSort(arr) {
  const min = Math.min(...arr), max = Math.max(...arr);
  const range = max - min + 1;
  const holes = new Array(range).fill(0);
  for (const v of arr) holes[v - min]++;
  let idx = 0;
  for (let i = 0; i < range; i++)
    while (holes[i]-- > 0) arr[idx++] = i + min;
}