There's actually a classification for these kinds of things in complexity theory: Pseudo-polynomial time, meaning polynomial complexity wrt the value of the input, instead of the size.
A "practical" example of these would be the knapsack problem, which is polynomially bounded by the value of the maximum weight.
I don't think there's any hard time that N has to represent the number of elements. For any reasonable algorithm, that's the limiting factor, but in this case the limiting factor is the maximum element size, which the algorithm time complexity scales with linearly.
No. Computational complexity measures the number of operations the algorithm performs on the input to get the output. In this case, there are N inputs in the array, and each operation takes constant time, so it is O(N). It doesn't matter if that constant is 1 or 200, it's still constant and doesn't change in the size of the input, which is the important part.
Yes. Except there are no threads in JS (other than the main one). Here we are actually pushing items into the microtask queue. The microtasks are run when they are due by the part of the runtime in charge of the event loop.
No, we'd use another variable, typically k or r, as in counting sort which is complexity O(n + r) or O(n + k) where r / k is the value of the highest number sorted.
Not really. Because the number of elements makes no difference. You could have 1000000000 elements and if the maximum value doesn't change it doesn't take any longer to sort.
I was referring to n items that need to be sorted, and m seconds it takes for each counter to expire. Honestly, I’m not sure how to evaluate timer runtime.
Edit: never mind, reread your comment, you’re correct. But I’m still curious how timer runtime is calculated?
Yeah I get what you are saying. Algorithms only count iterative steps regardless of time. So in this case even though it takes 10 seconds to display a line it’s still counted as O(1). Right? Does anyone else see the flaw? Are we missing something?
So in this case even though it takes 10 seconds to display a line it’s still counted as O(1).
Are we missing something?
Yes, you are. You can't calculate Big-O for a specific instance of a run because all algorithms end in a finite amount of time and would therefore be O(1). (Infinite loops are not technically algorithms.)
Big-O only applies to algorithms, not a specific run of an algorithm. If an algorithm always works within 10 seconds no matter what input, then it's O(1). But this algorithm could take 20 seconds if the array contains the value 20000.
Because it scales linearly to the highest value in the array, it's O(N) where N is the maximum value.
Code looks like some random writing, and sorting algorithms literally look like jdjxndjskskdj jdjejejs “hdjddj” (hdjddj) dkdjejdjdj; &;&;&:&;& hdjddj aja = [jejdjd djdjdj hdjddj djdj, hdjddj djdj]
Actually it just hides the computational complexity. JavaScript timeouts are put onto a queue (as pointed out in this comment). So even before the timeouts get a chance to run you are essentially sorting the list using whatever algorithm the queue uses to order its elements. Meaning the actual time complexity is still likely O(n•log(n)) (or worse).
It only appears to be O(n) because the delay due to the timeout is far greater than anything else happening for an array that size. But under the hood, there is still a scheduler that would need to loop through all the timeouts every tick and process them. So it's probably O(n^2), unless there is some internal optimization that pre-sorts the timeouts, in which case it could be less, but still no lower than O(n *log(n)), with a far higher k to boot in terms of both space and time.
This could be tested by measuring the delay of "sorting" an array of, say, a billion integers, and how slow it is compared to 10 billion, where every integer's value is 1..60, so the timeout is not too long compared to the size of the array.
Why would the runtime sort them? Each timeout knows its own delta time delay, and each timeout will be queued as a task (or micro task) to be ran with the next cycle of the event loop. Then each timeout will check if it's delta time has passed, if it has, call the cb, otherwise queue another copy of it self as a task. There's no need to sort the calls since each timeout will check their time logic within the same time step.
It would be neither. Time complexity exists because difference in processing speed makes the execution time on vary on different machines. To say it was big O or Theta implies it would execute in the same amount of time as another algorithm with the same time complexity. That's not true here. In this case the algorithm would take an exact amount of time regardless of the machine it's running on (unless the JavaScript engine is broken). The running time of this algorithm would simply be max(n) milliseconds.
Wouldn't it depend on where in the list max(n) is? If it's the last item in the list, and it took 1ms to reach that index, it would take max(n) + 1 ms to execute, or am I wrong?
You have a point there. I guess it could be written O(n) + max(n) milliseconds. But in that case, the O(n) portion may be negligible enough to omit it. You would need a very large list for there to be a noticeable difference.
311
u/Seismicsentinel Aug 11 '20
Where else on reddit did sleepsort come up today? I know it was somewhere
553
u/[deleted] Aug 11 '20
A sorting algorithm that makes computational complexity practically obsolete. Wow.
120
u/binarycat64 Aug 11 '20
Good old sleep sort.
133
u/szymek655 Aug 11 '20
65
u/linuxlib Aug 11 '20
If it's horrific but it's genius, it's not horrific.
46
u/PowerlessMainframe Aug 11 '20
God created Javascript, and the men did this
60
u/0x564A00 Aug 11 '20
Relevant xkcd
(my favorite is stacksort)
41
u/AppelEnPeer Aug 11 '20
Just a slower version of counting sort
38
u/blu-7 Aug 11 '20
So, this is an O(n) sorting algorithm... kinda.
37
u/[deleted] Aug 11 '20
A part of me is impressed
10
u/fartcloud101 Aug 11 '20
Could somebody please explain how this works?
15
u/justingolden21 Aug 11 '20
Or for a sorted list, just push it to a new list instead of logging it...
5
u/gulyman Aug 11 '20
I think this is known as sleep sort.
3
u/snppmike Aug 11 '20
This is like Homer Simpson running for trash commissioner: “Can’t someone else do it?”
3
u/implicatureSquanch Aug 11 '20
confuseSort
3
u/OwnStorm Aug 12 '20
Well it's : O(max(arr))
3
u/GeekMatta Aug 12 '20
"it works" is something for the end user to confirm and not programmer
2
u/FrenchieM Aug 12 '20
*inserts 1000000
1
u/rift95 Aug 11 '20
I actually made a "working" implementation of this algorithm last time it was posted. https://i.imgur.com/79Jhzcn.png
1
u/lorenzoiddd Aug 12 '20
How what why i have so many questions
1
u/John_Fx Aug 12 '20
Ship it
1
u/NightVow Aug 12 '20
holy shit
1
u/examinedliving Aug 12 '20
Why this do this?
1
u/jabeith Aug 12 '20
I love it haha
1
u/kopczak1995 Aug 12 '20
Devil here. First of all... I'm a big fan...
1
u/wobblyweasel Aug 12 '20
the problem is that this doesn't detect the end of sorting. it's easily fixed by adding a value that exceeds all other values:
1
u/leftoversalad Aug 13 '20
oh my god is this O(n)??????
1
u/rebelsofliberty Sep 06 '20
Well, it’s more reliable in sorting numeric values than sort() is
1
u/jackstine Oct 10 '20
Impressive
1
u/KyunDesu Oct 29 '20
Is this what's called gravity sort?
1
u/cstheory Aug 11 '20
Lol
0
u/[deleted] Aug 11 '20
[deleted] — view deleted comment
0
u/[deleted] Aug 11 '20
[deleted] — view deleted comment
0
u/[deleted] Aug 12 '20
[deleted] — view deleted comment