From 0b544c66c59b0c02b8037e66d0b6ce27e65e7a83 Mon Sep 17 00:00:00 2001 From: saurabhbazzad <36169626+saurabhbazzad@users.noreply.github.com> Date: Mon, 21 Oct 2019 23:13:16 +0530 Subject: [PATCH] Added Sleep Sort --- Sorts/SleepSort.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Sorts/SleepSort.js diff --git a/Sorts/SleepSort.js b/Sorts/SleepSort.js new file mode 100644 index 0000000000..309f11c221 --- /dev/null +++ b/Sorts/SleepSort.js @@ -0,0 +1,18 @@ +/*Sleep sort is a sorting algorithm in which, for every element + to be sorted, we set a timeout for the value of that element. + After the timeout is over, we print the value of the element. + Hence the output is printed in the sorted order. +*/ + +Array.prototype.sleepSort = function(callback_function) { + const arr = []; + for (let n of this) + setTimeout(() => { + arr.push(n); + if (this.length === arr.length) + callback_function(arr); + }, n + 1); + return arr; +}; + +[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].sleepSort(console.log);
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: