this post was submitted on 08 Nov 2024
673 points (98.3% liked)

Programmer Humor

19497 readers
1291 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
 
top 50 comments
sorted by: hot top controversial new old
[–] Allero@lemmy.today 27 points 14 hours ago (8 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...

[–] fallingcats@discuss.tchncs.de 5 points 10 hours ago (2 children)

Those bitflips are probably more likely to skip the section erroneously than waiting for the array to be sorted.

load more comments (2 replies)
[–] Midnitte@beehaw.org 7 points 12 hours ago

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

[–] Zoomboingding@lemmy.world 5 points 7 hours ago

Reminds me of a program in Homestuck. It's code that iterates until the author/universe dies, then executes some unknown code. The coding language is ~ath, or TilDeath.

load more comments (5 replies)
[–] Swedneck@discuss.tchncs.de 30 points 16 hours ago* (last edited 16 hours ago) (3 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
[–] porous_grey_matter@lemmy.ml 10 points 13 hours ago

Camelcase in python, ew, a fundamentalist would do that

[–] BluesF@lemmy.world 1 points 14 hours ago

yhwh looking suspiciously like an LLM

load more comments (1 replies)
[–] state_electrician@discuss.tchncs.de 126 points 21 hours ago (3 children)

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

[–] pyre@lemmy.world 43 points 19 hours ago* (last edited 19 hours ago) (4 children)

you should post this on lemmy.ml

[–] affiliate@lemmy.world 23 points 15 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

[–] magic_lobster_party@fedia.io 3 points 14 hours ago

They would see nothing wrong with it

load more comments (2 replies)
load more comments (2 replies)
[–] fluckx@lemmy.world 24 points 18 hours ago (1 children)

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

load more comments (1 replies)
[–] BatmanAoD@programming.dev 141 points 23 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.

[–] Zaphod@discuss.tchncs.de 23 points 18 hours ago (1 children)

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

[–] BatmanAoD@programming.dev 5 points 11 hours ago

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

[–] xmunk@sh.itjust.works 86 points 23 hours ago (3 children)

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

[–] frezik@midwest.social 45 points 22 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.

[–] BatmanAoD@programming.dev 40 points 22 hours ago (2 children)

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

[–] groet@feddit.org 11 points 18 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.

[–] MBM@lemmings.world 7 points 15 hours ago

Eh, trillion is a constant

[–] FiskFisk33@startrek.website 8 points 21 hours ago

amortized O(0)

[–] Benjaben@lemmy.world 8 points 18 hours ago (1 children)

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

Well yeah just delete the pointer to it!

load more comments (1 replies)
[–] vithigar@lemmy.ca 14 points 22 hours ago (1 children)

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

[–] db2@lemmy.world 7 points 22 hours ago

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

[–] random72guy@lemmy.world 17 points 20 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.

[–] BatmanAoD@programming.dev 6 points 18 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...

[–] SubArcticTundra@lemmy.ml 13 points 23 hours ago (2 children)

What library are you using for that?

[–] SkaveRat@discuss.tchncs.de 11 points 19 hours ago

In Python you just use

import destroy_universe
[–] jcg@halubilo.social 27 points 23 hours ago* (last edited 23 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

[–] Swedneck@discuss.tchncs.de 7 points 16 hours ago (1 children)

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

[–] onlinepersona@programming.dev 1 points 15 hours ago* (last edited 15 hours ago)

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

Anti Commercial-AI license

[–] frezik@midwest.social 2 points 22 hours ago

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

[–] TheOakTree@lemm.ee 6 points 14 hours ago (2 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?

[–] AllHailTheSheep@sh.itjust.works 2 points 5 hours ago* (last edited 5 hours ago)

don't get discouraged. sorting algorithms occur frequently in interviews, and yes you use them a decent amount (especially in languages without built in sorts like c) but they are one of the harder things to visualize in terms of how they work. I'd say avoid anything recursive for now until you can get selection and insertion down pat. check out geeksforgeeks articles on them, but also don't be afraid to Google willy nilly, you'll find the resource that makes it click eventually.

in terms of efficiency, it does become a little more difficult to grasp without some math background. big o is known as asymptomatic notation, and describes how a function grows. for example, if you graph f1(x)=15log(x) and f2(x)=x, you'll notice that if x is bigger than 19, then f2(x) always has a higher output value than f1(x). in computer science terms, we'd say f1 is O(log(n)), meaning it has logarithmic growth, and f2 is O(n), or linear growth. the formal definition of big o is that f(x) is O(g(x)), if and only if (sometimes abbreviated as iff) there exists constants N and C such that |f(x)| <= C|g(x)| for all x>N. in our example, we can say that C = 1, and N>19, so that fulfills definition as |15log(x)| <= 1|x| whenever x>19. therefore, f1(x) is O(f2(x)). apologies for just throwing numbers at you, (or if you've heard all this before) but having even just the most basic grasp of the math is gonna help a lot. again, in terms of best resources, geeksforgeeks is always great and googling can help you find thousands of more resources. trust that you are not the first person to have trouble with these and most people before you have asked online about it as well.

I also highly reccomend grabbing a copy of discrete mathematics and it's applications by Kenneth Rosen to dig farther into the math. there's a few other types of asymptomatic notation such os big omega and big theta, even little o, that I didn't mention here but are useful for comparing functions in slightly different ways. it's a big book but it starts at the bottom and is generally pretty well written and well laid out.

feel free to dm me if you have questions, I'm always down to talk math and comp sci.

edit: in our example, we could also pick c =19 and n = 1, or probably a few other combinations as well. as long as it fills the definition it's correct.

[–] 90s_hacker@reddthat.com 7 points 14 hours ago* (last edited 14 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

[–] TheOakTree@lemm.ee 2 points 13 hours ago

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

[–] 1984@lemmy.today 24 points 19 hours ago (1 children)

This is the algoritm I use at work.

load more comments (1 replies)
[–] lemmydividebyzero@reddthat.com 19 points 19 hours ago (1 children)
[–] Ephera@lemmy.ml 32 points 18 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.

[–] ChaoticNeutralCzech@feddit.org 48 points 23 hours ago (1 children)
[–] MajorHavoc@programming.dev 30 points 22 hours ago
// portability

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

[–] 1boiledpotato@sh.itjust.works 8 points 17 hours ago (1 children)

And the time complexity is only O(1)

[–] voldage@lemmy.world 13 points 15 hours ago (1 children)

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.

[–] 1boiledpotato@sh.itjust.works 1 points 13 hours ago

Oh yeah, I didn't think about the time that it takes to check if it's sorted. The sorting time is constant though

[–] TheTechnician27@lemmy.world 38 points 23 hours ago (1 children)
[–] nilloc@discuss.tchncs.de 2 points 13 hours ago

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

[–] aeharding@vger.social 13 points 21 hours ago* (last edited 21 hours ago) (1 children)

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  ✨

load more comments (1 replies)
[–] iAvicenna@lemmy.world 12 points 4 hours ago (2 children)

you can also call it quantum sort since there is non zero probability that it will sort itself by random flips

load more comments (2 replies)
load more comments
view more: next ›