this post was submitted on 04 Nov 2025
410 points (99.3% liked)

Programmer Humor

27193 readers
1037 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 30 comments
sorted by: hot top controversial new old
[–] Object@sh.itjust.works 4 points 1 hour ago
[-3, -1, 
main@desktop:~/Projects/TimeoutSort$ _
[–] CanadaPlus@lemmy.sdf.org 2 points 3 hours ago* (last edited 3 hours ago)

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

[–] zea_64@lemmy.blahaj.zone 17 points 9 hours ago (1 children)

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

[–] sus@programming.dev 9 points 7 hours ago (1 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.

[–] yetAnotherUser@lemmy.ca 1 points 7 hours ago

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

[–] 1985MustangCobra@lemmy.ca 5 points 7 hours ago

why is it doing it in ordinal order?

[–] Tartas1995@discuss.tchncs.de 14 points 12 hours 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 12 hours 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 31 points 10 hours 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...

[–] BlueKey@fedia.io 2 points 1 hour ago

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

[–] filcuk@lemmy.zip 32 points 10 hours 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 4 points 4 hours ago

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

[–] mcmodknower@programming.dev 63 points 17 hours ago

I know this under the name sleepSort.

[–] eager_eagle@lemmy.world 86 points 19 hours ago (2 children)

finally, sorting in linear time /s

[–] iAvicenna@lemmy.world 4 points 9 hours ago

linear in size

[–] ytg@sopuli.xyz 28 points 15 hours 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 2 points 1 hour ago

it's in constant time then

[–] BeigeAgenda@lemmy.ca 54 points 19 hours ago

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

[–] kender242@lemmy.world 19 points 17 hours ago

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

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

[–] JargonWagon@lemmy.world 24 points 19 hours ago (2 children)

I'm dumb, can someone ELI5 please?

[–] scrion@lemmy.world 72 points 19 hours 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.

[–] ptu@sopuli.xyz 4 points 6 hours ago (1 children)

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

[–] scrion@lemmy.world 2 points 4 hours 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 4 hours ago

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

[–] JargonWagon@lemmy.world 13 points 19 hours ago

Perfectly explained, thank you!

[–] theunknownmuncher@lemmy.world 21 points 19 hours ago* (last edited 19 hours ago)

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.

[–] LodeMike@lemmy.today 23 points 20 hours ago (1 children)
[–] SanctimoniousApe@lemmings.world 8 points 15 hours ago (1 children)

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