this post was submitted on 04 Nov 2025
471 points (99.2% liked)

Programmer Humor

27248 readers
884 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
top 35 comments
sorted by: hot top controversial new old
[–] eager_eagle@lemmy.world 93 points 5 days ago (2 children)

finally, sorting in linear time /s

[–] ytg@sopuli.xyz 31 points 5 days ago (1 children)

It’s kind of linear, in the largest element of the array. Just not in the length of the array.

[–] not_IO@lemmy.blahaj.zone 3 points 5 days ago

it's in constant time then

[–] iAvicenna@lemmy.world 4 points 5 days ago

linear in size

[–] mcmodknower@programming.dev 73 points 5 days ago

I know this under the name sleepSort.

[–] BeigeAgenda@lemmy.ca 58 points 5 days ago

Can't wait for vibe coded programs to use timeoutSort.

[–] LodeMike@lemmy.today 30 points 5 days ago (1 children)
[–] SanctimoniousApe@lemmings.world 9 points 5 days ago (1 children)

Doesn't the tortoise always win that one, too?

[–] JargonWagon@lemmy.world 27 points 5 days ago (2 children)

I'm dumb, can someone ELI5 please?

[–] scrion@lemmy.world 76 points 5 days ago (2 children)

The output is sorted due to the fact that for each number, a timer is started that prints out the number after waiting a number of milliseconds equal to said number.

Therefore, 1 is printed first after delaying for 1 millisecond, 5 is printed second after 5 milliseconds etc.

[–] JargonWagon@lemmy.world 13 points 5 days ago

Perfectly explained, thank you!

[–] ptu@sopuli.xyz 4 points 5 days ago (1 children)

So all items in the array are launched simultaneuously and ran in parallel instead of sequentially?

[–] scrion@lemmy.world 3 points 5 days ago (1 children)

They are launched sequentially, but run simultaneously, yes - at least some of them. And they run concurrently but not in parallel - using a single execution context, there is only a single thread, so no parallelism exist.

[–] ptu@sopuli.xyz 1 points 5 days ago

I see, I was only aware of sleep but that makes sense. Thanks for your insight.

[–] theunknownmuncher@lemmy.world 23 points 5 days ago* (last edited 5 days ago) (1 children)

The program goes through the collection of numbers and prints each one after a delay of milliseconds equal to that number: "Print the number 20 after a 20 millisecond delay. Print the number 5 after a 5 millisecond delay. Print the number 100 after a 100 millisecond delay... etc..." effectively sorting the collection because the numbers will be printed in order from smallest to largest.

This is a clever (but impractical) way to sort a collection, because it does not require comparing any of the elements of the collection.

[–] ulterno@programming.dev 0 points 4 days ago

because it does not require comparing any of the elements of the collection

Well, if you are comparing x + a to y and x + b to y and then both to y', then y'' and so on, then are you really not comparing a to b?

[–] kender242@lemmy.world 23 points 5 days ago (1 children)

This is almost a bucket sort, which is practically O(n).

(I'll leave it to the other readers to state the trade-offs)

[–] Buddahriffic@lemmy.world 3 points 4 days ago

That assumes SetTimeout() is O(1), but I suspect it is O(log(n)), making the algorithm O(n*log(n)), just like any other sort.

Did some looking into the specifics of SetTimeout() and while it uses a data structure with theoretical O(1) insertion, deletion, and execution (called a time wheel if you want to look it up), the actual complexity for deletion and execution is O(n/m) (if values get well distributed across the buckets, just O(n) if not) where m is the number of buckets used. For a lot of use cases you do get an effective O(1) for each step, but I don't believe using it as a sorting engine would get the best case performance out of it. So in terms of just n (considering m is usually constant), it'll be more like O(n²).

And it's actually a bit worse than that because the algorithm isn't just O(n/m) on execution. It needs to check each element of one bucket every tick of whatever bucket resolution it is using. So it's actually non-trivially dependent on the wait time of the longest value. It's still a constant multiplier so the big O notation still says O(n) (just for the check on all ticks), but it might be one of the most misleading O(n)'s I've ever seen.

Other timer implementations can do better for execute and delete, but then you lose that O(1) insertion and end up back at O(n*log(n)), but one that scales worse than tree sort because it is literally tree sort plus waiting for timeouts.

Oh and now, reading your comment again after reading about SetTimeout(), I see I misunderstood when I first read it and thought you meant it was almost as fast as bucket sort, but see now you meant it basically is bucket sort because of that SetTimeout() implementation. Bucket sort best case is O(n), worst case is O(n²), so I guess I can still do decent analysis lol.

[–] zea_64@lemmy.blahaj.zone 20 points 5 days ago (1 children)

Wait till you find out how the runtime manages multiple concurrent timers

[–] sus@programming.dev 13 points 5 days ago (2 children)

it's

while (true) {
    let t = Date.now();
    if (timeoutMap.has(t)) timeoutMap[t]();
}

of course. Clearly O(n).

disclaimerFeel free to use it. I guarantee it is bug free. Comes with express warranty. This notice is legally binding.

[–] FooBarrington@lemmy.world 4 points 4 days ago* (last edited 4 days ago)

I found a way to optimize your code without affecting the result. By making it branchless, I was able to get my CPU to 100% utilization!

[–] yetAnotherUser@lemmy.ca 2 points 5 days ago (1 children)

Then don't complain once you get arrested...

[–] ulterno@programming.dev 1 points 4 days ago

From nowaday's standards, that's express warranty that lasts until you start executing your code.

[–] Tartas1995@discuss.tchncs.de 15 points 5 days ago

For better usage: you don't need to write it into console. Just write it in an array!

[–] lugal@lemmy.dbzer0.com 13 points 5 days ago (2 children)

Would this lead to problems if there are multiple identical and close by values? Like for example you have 100 elements each between 1 and 5

[–] rbn@sopuli.xyz 33 points 5 days ago (2 children)

To reduce the chance of errors, you can multiply all numbers by a factor of 10, 100, 1000, 10000, .... for the timeout. The higher the factor, the lower the chances of an incorrect result. And as no one asked about performance...

[–] filcuk@lemmy.zip 35 points 5 days ago (1 children)

As added benefit, you can then opyimise the code by dividing the number by 2, making it twice as fast. Think of the savings!

[–] lugal@lemmy.dbzer0.com 5 points 5 days ago

Better yet: take the square root and you get a sub-linear run time

[–] BlueKey@fedia.io 3 points 5 days ago

Maybe not peak performance but heigh CPU efficency, it's load ist mostly 0.

[–] Object@sh.itjust.works 8 points 4 days ago
[-3, -1, 
main@desktop:~/Projects/TimeoutSort$ _
[–] 1985MustangCobra@lemmy.ca 6 points 5 days ago

why is it doing it in ordinal order?

[–] Dyskolos@lemmy.zip 3 points 4 days ago

In a situation where performance isn't required but absolute zero overhead is paramount, this is a perfect sort. Depends on the data itself though. Lots of small numbers maybe multiply each by 10 or 20, lots of big numbers...well...bad luck.

[–] CanadaPlus@lemmy.sdf.org 2 points 5 days ago* (last edited 5 days ago)

Isn't this basically an implementation of spaghetti sort? I've seriously taken the delay approach before in distributed memory situations.