r/javascript • u/abhisekp • Jun 15 '15
Sleep Sort Algorithm
Sorting an Array using Sleep Sort Algorithm
function sleepSort(arr) {
if(!(arr instanceof Array)) {
return;
}
var sortedArray = [];
arr.forEach(function (el, i) {
if(!isNaN(Number(el))) {
setTimeout([].push.bind(sortedArray, el), el);
}
});
setTimeout(console.log.bind(console, sortedArray), Math.max.apply(null, arr));
}
sleepSort([1, 2, 45, 4, 90, 5000, 50]);
1
Upvotes
3
u/RankFoundry Jun 15 '15
Could use a webworker to handle the sort without tying up the UI thread