setTimeout is just executing the logging function after a delay of x milliseconds without blocking the forEach loop. So, the lower the value, the shorter the delay.
That said if you have a large enough array and your first item is a 2 and the last item is a 1, it probably won't "sort" correctly
The complexy in account for the number of cycles to calculate the result is (nearly) uneffected by multiplying each by 1000.
The rest is more or less correct, but not related to my modification of the algorithm.
(at least in v8 instances a priority queue is used, but I think they make some bucket optimizations which also is the reason why setTimeout(..., n) for n really small is the same as setTimeout(..., 0))
How do you guys keep track, or know, what function works asynchronously, what is not? That's genuine question, I'm really curious. My primary skills is in .NET ecosystem, and every function that works asynchronously there has -Async suffixed to the function's name.
a) a function accepting a callback function as argument b) a function returning a Promise object c) your code doesn't work like you expect and after searching for 2 hours you try the other way
It runs a loop through each array item and prints the value of it in the console after waiting the amount a milliseconds the value is. This means the lowest values are printed first because they wait less time to print. This function would take 650 milliseconds to print all these array numbers because 650 is the largest value.
So this is actually kinda funny but ultimately useless. And stupid lol it isn’t sorting anything.
It's O(n*log n) to schedule the threads, and O(max) to wait for them to wake up. Also thread schedulers don't make absolute guarantees so it's not even correct. You can increase the accuracy by multiplying all the values by a scaling factor, but this increases max.
It seems like one. However I would argue that the browser actually sorts these events into a sorted list. The inserting is linear, which means the SleepSort we see is O(n^2) or a rather a InsertionSort with waiting time.
It isn't a true sorting algorithm. If the array is large enough you may have console.log executing in an undesired order. The foreach operation isn't going to call setTimeOut on all of the elements at the exact same time.
It doesn't really work, try it with an array that has a larger amount of items that are all low numbers ex. < 50.
That's because all calls have x amount of delay.
Just like Thread.sleep() (Or equivalents) in most languages will not sleep the exact amount of milliseconds passed. I believe for that it's something like a minimum of 15ms.
I don't know what it is for setTimeout() though but probably something similar.
Every call has an execution time. And since it's JavaScript, you can't really now how long each execution will take. By the time setTimeOut is called for one of the later 1's, it's too late to be executed before one the former 2's. You could check it by using an array of strings like 1A, 2A, 3A, 1B, 2B, 3B etc.
We had to analyze this so-called sleep-sort algorithm. Turns out it is O(n+max(array)) but inaccurate due to hardware limitations. Counting sort has the same time-complexity but runs faster on actual hardware and is accurate.
yeah, we did it under the assumption that the threads are running on a machine with enough (but constant) cores so that scheduling can (theoretically) be done in constant time.
This obviously isn’t sorting an array, just printing in order. Someone should create a function that uses this method to return the sorted array. I think the easiest way is to create a promise and resolve when the new array is same length as original.
I remember someone presenting it in Flutter event like two years back and calling it "Futuristic Sort". He used Futures in Dart with the same logic. He did get laughs and appreciation from everyone.
16
u/Morasiu Aug 11 '20
Nice one!
You can do this in C# too. Check it
193
u/uncoded_decimal Aug 11 '20
Non-js guy here, what's happening here?
98
u/misterrandom1 Aug 11 '20
Cool, try with negative numbers.
73
u/heartofrainbow Aug 11 '20
And it's an O(n) sorting algorithm.
17
u/ixahmedxii Aug 11 '20
sweats nervously
7
u/omega_haunter Aug 11 '20
Looks like sleep sort or thread sort
7
u/currentlyatwork1234 Aug 11 '20
It doesn't really work, try it with an array that has a larger amount of items that are all low numbers ex. < 50.
That's because all calls have x amount of delay.
Just like Thread.sleep() (Or equivalents) in most languages will not sleep the exact amount of milliseconds passed. I believe for that it's something like a minimum of 15ms.
I don't know what it is for setTimeout() though but probably something similar.
Here's something for demonstration:
7
u/scp-NUMBERNOTFOUND Aug 11 '20
Now pack it into a node module, make it depend on isEven. 996865 downloads in a week
5
u/McLPyoutube Aug 11 '20
We had to analyze this so-called sleep-sort algorithm. Turns out it is O(n+max(array)) but inaccurate due to hardware limitations. Counting sort has the same time-complexity but runs faster on actual hardware and is accurate.
12
u/t3hlazy1 Aug 11 '20
This obviously isn’t sorting an array, just printing in order. Someone should create a function that uses this method to return the sorted array. I think the easiest way is to create a promise and resolve when the new array is same length as original.
4
u/Snapstromegon Aug 11 '20
Did I have to write this as a promise based implementation you could actually use?
No.
Did I do it anyways?
Of course!
I present to you asyncTimeoutSort - watch out for the npm package.
1
u/ForeignerLove Aug 11 '20
Good luck if you have 100000000000000 as one of your numbers :)
1
u/Inglonias Aug 11 '20
this is horrifying.
1
u/coloredgreyscale Aug 11 '20
How stable is it for ordering (repeating) similar numbers, especially when the first and last values of a long unordered list are close to zero?
3
u/nfsuismyname Aug 11 '20
Sleepsort is the best kind of sorting 👌 After bogosort ofc
2
u/_4kills Aug 11 '20
Sleep sort runs in constant time!
2
u/[deleted] Aug 11 '20
It's so damn stupid it's actually genius
2
u/eyekwah2 Aug 11 '20
I prefer quantum bogosort myself. Check if array is sorted. If so, good. If not, scrap universe.
1
u/BazilExposition Aug 11 '20
Sometimes you need to do everything with settimeout.
1
u/[deleted] Aug 11 '20 edited Aug 11 '20
I remember someone presenting it in Flutter event like two years back and calling it "Futuristic Sort". He used Futures in Dart with the same logic. He did get laughs and appreciation from everyone.
1
u/uses_irony_correctly Aug 11 '20
So, sleep sort?
1
u/HeKis4 Aug 11 '20
Ah yes, the O(max(n)) sorting algorithm.
1
u/tharnadar Aug 11 '20
O(n) where n is the greatest number in the array
1
u/beautyyu Aug 11 '20
We call it sleeping sort
1
u/[deleted] Aug 11 '20
For a those who are womdering, its SLEEP SORT
1
u/[deleted] Aug 11 '20
let arr = [5000, 10000, 8754, 6436]
My boss: My is this so slow Me: There is a very complex algorithm behind the sorting function.
1
u/[deleted] Aug 11 '20
The question is, how big can this list get before it doesn't execute all the commands quick enough?
1
u/Krixate Aug 12 '20
the famous sleep sort
1
u/ososalsosal Aug 12 '20
This is amazing. A sort in O(n) time
1
u/okawo80085 Aug 11 '20
wtf?!
why???
-4
u/escargotBleu Aug 11 '20
Nothing to do with js, you can do it with any language.
0
u/[deleted] Aug 11 '20
[deleted] — view deleted comment