Last active
January 26, 2020 00:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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