Skip to content

Instantly share code, notes, and snippets.

@karpolan
Last active February 27, 2021 22:32
Show Gist options
  • Save karpolan/dda2276221b07ebeca20a38e39e1107f to your computer and use it in GitHub Desktop.
Save karpolan/dda2276221b07ebeca20a38e39e1107f to your computer and use it in GitHub Desktop.
Sorting of array in O(max(array)) complicity!
const source = [20, 5, 100, 1, 90, 200, 40, 29];
const sorted = [];
console.log('source:', source);
for (const value of source) {
setTimeout(() => {
sorted.push(value);
console.log('sorting:', value); // to see progress
}, value);
}
setTimeout(() => {
console.log('sorted:', sorted);
}, Math.max(...source) + 42);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment