Skip to content

Commit 8b7b485

Browse files
authored
Merge pull request TheAlgorithms#109 from saurabhbazzad/patch-1
Added Sleep Sort
2 parents 9072113 + 0b544c6 commit 8b7b485

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sorts/SleepSort.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*Sleep sort is a sorting algorithm in which, for every element
2+
to be sorted, we set a timeout for the value of that element.
3+
After the timeout is over, we print the value of the element.
4+
Hence the output is printed in the sorted order.
5+
*/
6+
7+
Array.prototype.sleepSort = function(callback_function) {
8+
const arr = [];
9+
for (let n of this)
10+
setTimeout(() => {
11+
arr.push(n);
12+
if (this.length === arr.length)
13+
callback_function(arr);
14+
}, n + 1);
15+
return arr;
16+
};
17+
18+
[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].sleepSort(console.log);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy