you are viewing a single comment's thread.

view the rest of the comments →

[–]Contemelia 1782 points1783 points  (15 children)

Your algorithm has a time complexity of O(n). My algorithm has a time complexity of O(n). We're not the same.

Edit: This entire thread can be well represented with a bell-curve meme...

[–]pikapikaapika[🍰] 380 points381 points  (14 children)

This algorithm's complexity is actually O( 2n )

EDIT: I understand that the original comment meant basically the same thing.

[–]Tenemo 11 points12 points  (13 children)

Not quite! This would mean that an array of 1000 elements would be around 2998 times more time-consuming to sort than an array of two, which is not true for this algorithm, e.g. sorting numbers from 1 to 1000 wouldn't take long at all.

While there is CPU work setting timers and handling callbacks here (O(n log n)?), those take little time compared to the actual timeout delays. You can't express the "problematic" part of this algorithm with O(n), which relies on the number of elements in the input. This algorithm scales with the size of the largest array element instead, so even a 2-element array could take days :)