Skip to content

Instantly share code, notes, and snippets.

@mfalade
Last active January 26, 2020 00:17
Show Gist options
  • Save mfalade/fbd5eb882b6dffec5bd12663e1d4722a to your computer and use it in GitHub Desktop.
Save mfalade/fbd5eb882b6dffec5bd12663e1d4722a to your computer and use it in GitHub Desktop.
My play play draft implementation of the sleep sort algo. Would nap for a long time for really large numbers though.
const unsortedListOfNumbers = [5, 13, 3, 0, 1, 89, 1, 21, 34, 2, 55, 8];
const TIMEOUT_PADDING = 50;
const sleepSort = async listOfNums => {
const result = [];
const promises = listOfNums.map(
num =>
new Promise(resolve => {
setTimeout(() => {
result.push(num);
resolve();
}, num * TIMEOUT_PADDING);
}),
);
return Promise.all(promises).then(() => result);
};
const exec = async () => {
const sorted = await sleepSort(unsortedListOfNumbers);
console.log(sorted);
}
exec();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment