this post was submitted on 04 Nov 2025
440 points (99.1% liked)
Programmer Humor
27193 readers
1127 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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I'm dumb, can someone ELI5 please?
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.
So all items in the array are launched simultaneuously and ran in parallel instead of sequentially?
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.
I see, I was only aware of sleep but that makes sense. Thanks for your insight.
Perfectly explained, thank you!
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.
Well, if you are comparing
x + atoyandx + btoyand then both toy', theny''and so on, then are you really not comparingatob?