this post was submitted on 08 Nov 2024
465 points (98.1% liked)

Programmer Humor

19488 readers
894 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 1 year ago
MODERATORS
 
all 44 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 4 hours ago (1 children)

The most beautiful thing about this program is that it would work.

Various bit flips will once lead to all numbers being in the correct order. No guarantee the numbers will be the same, though...

[–] [email protected] 5 points 2 hours ago

Might also take a very long time (or a large amount of radiation).

[–] [email protected] 15 points 6 hours ago* (last edited 6 hours ago) (2 children)
import yhwh  

def interventionSort(unsortedList):
    sortedList = yhwh.pray(
    "Oh great and merciful Lord above, let thine glory shine upon yonder list!", 
    unsortedList
    )  
    return sortedList
[–] [email protected] 2 points 3 hours ago

Camelcase in python, ew, a fundamentalist would do that

[–] [email protected] 0 points 3 hours ago

yhwh looking suspiciously like an LLM

[–] [email protected] 3 points 4 hours ago (1 children)

Hello programmers...

I recently took a course that went through basic python, C, and C++.

I had a hard time implementing various forms of sorting functions by hand (these were exercises for exam study). Are there any resources you folks would recommend so that I can build a better grasp of sorting implementations and efficiency?

[–] [email protected] 4 points 4 hours ago* (last edited 4 hours ago) (1 children)

Skiena's Algorithm design manual is very widely recommended for learning algorithms, I've also heard good things about A common sense guide to algorithms and data structures. Skiena's also has video lectures on YouTube if you prefer videos.

From what I've seen, a common sense guide seems to be more geared towards newer programmers while Skiena assumes more experience. Consequently, Skiena goes into more depth while A common sense guide seems to be more focused on what you specifically asked for. algorithm design manual A common sense guide

[–] [email protected] 1 points 3 hours ago

Thank you, awesome! I will definitely check out this material :)

[–] [email protected] 15 points 8 hours ago

I prefer the one where you randomly sorting the array until all elements are in order. ( Bogosort )

[–] [email protected] 81 points 11 hours ago (1 children)

My favorite is StalinSort. You go through the list and eliminate all elements which are not in line.

[–] [email protected] 27 points 9 hours ago* (last edited 9 hours ago) (2 children)

you should post this on lemmy.ml

[–] [email protected] 0 points 1 hour ago

Do you not understand federation yet? https://lemmy.ml/post/22259107/14814668

[–] [email protected] 11 points 5 hours ago

it would be a pretty funny post for the full 5 minutes it would last until it got stalin sorted out of lemmy.ml

[–] [email protected] 16 points 9 hours ago (1 children)
[–] [email protected] 20 points 8 hours ago

I hear, it actually significantly increases the chance of the miracle occurring when you pass the array into multiple threads. It's a very mysterious algorithm.

[–] [email protected] 16 points 9 hours ago

This is the algoritm I use at work.

[–] [email protected] 102 points 13 hours ago (5 children)

Reminds me of quantum-bogosort: randomize the list; check if it is sorted. If it is, you're done; otherwise, destroy this universe.

[–] [email protected] 17 points 8 hours ago (1 children)

The creation and destruction of universes is left as an exercise to the reader

[–] [email protected] 2 points 1 hour ago

Creation is easy, assuming the many-worlds interpretation of quantum mechanics!

[–] [email protected] 69 points 13 hours ago (3 children)

Guaranteed to sort the list in nearly instantaneous time and with absolutely no downsides that are capable of objecting.

[–] [email protected] 36 points 12 hours ago (2 children)

You still have to check that it's sorted, which is O(n).

We'll also assume that destroying the universe takes constant time.

[–] [email protected] 5 points 7 hours ago

We'll also assume that destroying the universe takes constant time.

Well yeah just delete the pointer to it!

[–] [email protected] 29 points 11 hours ago (2 children)

In the universe where the list is sorted, it doesn't actually matter how long the destruction takes!

[–] [email protected] 4 points 8 hours ago (1 children)

It actually takes a few trillion years but its fine because we just stop considering the "failed" universes because they will be gone soon™ anyway.

[–] [email protected] 5 points 5 hours ago

Eh, trillion is a constant

[–] [email protected] 6 points 11 hours ago

amortized O(0)

[–] [email protected] 12 points 12 hours ago (1 children)

Except you missed a bug in the "check if it's sorted" code and it ends up destroying every universe.

[–] [email protected] 4 points 12 hours ago

There's a bug in it now, that's why we're still here.

[–] [email protected] 12 points 10 hours ago (1 children)

Instead of destroying the universe, can we destroy prior, failed shuffle/check iterations to retain o(1)? Then we wouldn't have to reload all of creation into RAM.

[–] [email protected] 2 points 8 hours ago

Delete prior iterations of the loop in the same timeline? I'm not sure there's anything in quantum mechanics to permit that...

[–] [email protected] 9 points 13 hours ago (2 children)

What library are you using for that?

[–] [email protected] 7 points 9 hours ago

In Python you just use

import destroy_universe
[–] [email protected] 21 points 13 hours ago* (last edited 13 hours ago) (1 children)

is-sorted and a handful of about 300 other npm packages. Cloning the repo and installing takes about 16 hours but after that you're pretty much good for the rest of eternity

[–] [email protected] 3 points 6 hours ago (1 children)

that explains why it took god 7 days to make the universe

[–] [email protected] 1 points 5 hours ago* (last edited 5 hours ago)

We still suffer from the runtime errors that could've been caught at compilation time.

Anti Commercial-AI license

[–] [email protected] 2 points 12 hours ago

Since randomizing the list increases entropy, it could theoretically make your cpu cooler just before it destroys the universe.

[–] [email protected] 3 points 7 hours ago (1 children)

And the time complexity is only O(1)

[–] [email protected] 8 points 4 hours ago

I don't think you can check if array of n elements is sorted in O(1), if you skip the check though and just assume it is sorted now (have faith), then the time would be constant, depending on how long you're willing to wait until the miracle happens. As long as MTM (Mean Time to Miracle) is constant, the faithfull miracle sort has O(1) time complexity, even if MTM is infinite. Faithless miracle sort has at best the complexity of the algorithm that checks if the array is sorted.

Technically you can to down to O(0) if you assume all array are always sorted.

[–] [email protected] 42 points 13 hours ago (1 children)
[–] [email protected] 22 points 12 hours ago
// portability

Gave me the giggles. I've helped maintain systems where this portable solution would have left everyone better off.

[–] [email protected] 33 points 13 hours ago (1 children)
[–] [email protected] 2 points 3 hours ago

I wonder how many 2 item lists have been sorted that way IRL.

[–] [email protected] 7 points 11 hours ago* (last edited 11 hours ago)

Shameless plug for my sort lib

edit: Looking at my old code it might be time to add typescript, es6 and promises to make it ✨  p r o d u c t i o n   r e a d y  ✨