Sleep Sort

Sort by timeouts.

Best O(max(n)) Avg O(max(n)) Worst O(max(n)) Space O(n) Stable No In-place No Comparison-based

How it works

Sort by timeouts.

Implementation

async function sleepSort(arr) {
  const result = [];
  const promises = arr.map(v =>
    new Promise(r => setTimeout(() => {
      result.push(v); r();
    }, v * 10))
  );
  await Promise.all(promises);
  return result;
}