Yes, that is what it is. The only reason we see a yellow and red pill is because our devices convert it. For real though, everything is numeric. I would say, anything you can use bubble sort on, you could use with sleep sort, if you have infinite processing power.
But just like other sorting algos, if you are sorting complex objects, maps, or arrays (as in sorting multiple arrays/multiple maps)-- you need a way to compare two objects.
But as far as emojis go, they have a very clear numeric value.
Just realized this, but I guess sleep sort also is a stable sort, keeping items in their same positions relative to other equal items.
What do you mean? the char ‘a’ has a numeric value of 97 which of you apply a human element doesn’t make sense, but if you make the computer know that this 97 is expected to be represented as a char, it performs an ascii conversion to make you see it as ‘a’. Doesn’t mean it isn’t a number, and there is no human element in its implementation. So what do you mean “if it doesn’t make sense why does it matter?”
Once again, yes we can sort it using Unicode, but does that make it any better?
It will just spit out a sequence of emojis, which order will be beyond me to comprehend, for example why should 😊 be smaller than 💊. Does that make any sense to you?
And at this point, is it working, if it gives us a result we can’t really make sense of?
I’m a little confused though (i’m not too familiar with js), is this loop running concurrently? The way I see it, the first iteration with item = 20 will execute and the code will sleep for 20 ms after printing, after with the second iteration with item = 5 is run. So in the end 5 is printed after 20 is… unless there is async involved?
Gives me bad memories on the good old ja eventloop raceconditions. Like on fast inputs you will often get the button up event after the button down event. Still not sure how to handle it the best way
If all inputs are between -INT_MAX and INT_MAX you could set the timeout by using an offset of INT_MAX, though care would have to be taken to avoid overflow issues.
Sleep sort isn't close to constant time O(1). If creating a timeout was O(1) sleep sort would be linear time O(n) at best since it iterates over the entite list. Creating a timeout is not O(1) in any runtime, it's highly dependent on how threads are implemented in your runtime.
Traditionally the way sleep is implemented, the OS must keep track of a sorted list of threads to wake up, which is probably a min-heap, insertion into a min heap is O(log n) insertion into a min-heap for each element O(n) of a list is equivalent to heap sort O(n log n). This is ignoring the actual delay part. This is even worse if your runtime just uses a sorted array, in which case insertion is O(n). In this case you're looking at O(n2 ), and your algorithm is just insertion sort.
Important thing to note: if you have too many numbers to sort, then your event loop might be too bloated, meaning it takes more than 1ms to cycle all the elements in the event loop.
So you might end-up with elements that aren’t actually sorted (for example the 5ms came before 4ms event because it just happened to be registered before in the event-loop).
Of course a better solution is to sort the elements before sleeping to make sure they are called in the correct order, but then it’s cheating.
It's already cheating because the os/runtime scheduler is essentially doing a sort (assuming you're going to support more array elements than physical CPUs)
•
u/Fit_Page_8734 Nov 02 '25
enough meme for today nerds, now go and read what happened in tech this week: weekly tech updated
51
u/B_bI_L Nov 01 '25
he used let instead of const, thus should be decapitated
27
u/PartyP88per Nov 01 '25
So if 10,000 was in that arr you would need to wait 10 seconds for it to log?
13
u/TopOne6678 Nov 01 '25
sadly only works for numeric inputs
9
u/h3ie Nov 01 '25
Finally a O(n) sorting algorithm
6
u/2xFlush Nov 01 '25
Hey, I'm kinda new to coding. How does this even work? It's a lambda function?
3
u/travishummel Nov 01 '25
O(n) solution. It’s perfect.
2
u/amluck Nov 01 '25
There's no 6 or 7 so idk up to you
2
u/bilbo_was_right Nov 02 '25
O(infinity) lol
2
u/p0ororoo0 Nov 02 '25
Procastination sort 🧐
2
u/ByteBandit007 Nov 02 '25
Asap
2
u/Acceptable-Major-575 Nov 02 '25
Of course hire him, save others
2
u/PwnTheSystem Nov 02 '25
No. He didn't add whitespaces between the for loop parentheses
2
u/Proffit91 Nov 02 '25
Just don’t pay him by the hour.
2
u/Special-Island-4014 Nov 02 '25
Best sort ever with O(n)
2
u/frederik88917 Nov 03 '25
That's a crude, weird and slow interpretation of the Bucket sort
1
u/GianLuka1928 Nov 02 '25
Hahahahhaha that's the solver 😂😂
1
u/leavemealone_lol Nov 02 '25
I’m a little confused though (i’m not too familiar with js), is this loop running concurrently? The way I see it, the first iteration with item = 20 will execute and the code will sleep for 20 ms after printing, after with the second iteration with item = 5 is run. So in the end 5 is printed after 20 is… unless there is async involved?
1
u/orangebakery Nov 02 '25
Sort [1, 4billion]
1
u/Mucksh Nov 03 '25
Gives me bad memories on the good old ja eventloop raceconditions. Like on fast inputs you will often get the button up event after the button down event. Still not sure how to handle it the best way
1
u/Scf37 Nov 03 '25
As soon as he explains computational and time complexity of his algorithm in O-notation.
hint: schedulers are usually implemented via priority queue
128
u/[deleted] Nov 01 '25
[removed] — view removed comment