380
Not my problem sort (infosec.pub)
all 35 comments
sorted by: hot top controversial new old
[-] TheOakTree@lemm.ee 2 points 22 minutes ago

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?

[-] 90s_hacker@reddthat.com 1 points 9 minutes ago

Skiena's Algorithm 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.

[-] Swedneck@discuss.tchncs.de 8 points 1 hour ago* (last edited 1 hour ago)
import yhwh  

def interventionSort(unsortedList):
    sortedList = yhwh.pray(
    "Oh great and merciful Lord above, let thine glory shine upon yonder list!", 
    unsortedList
    )  
    return sortedList
[-] fluckx@lemmy.world 18 points 3 hours ago

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

[-] 1boiledpotato@sh.itjust.works 6 points 3 hours ago

And the time complexity is only O(1)

[-] voldage@lemmy.world 4 points 33 minutes 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.

[-] lemmydividebyzero@reddthat.com 17 points 4 hours ago
[-] Ephera@lemmy.ml 26 points 4 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.

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

[-] pyre@lemmy.world 34 points 5 hours ago* (last edited 5 hours ago)

you should post this on lemmy.ml

[-] affiliate@lemmy.world 5 points 41 minutes 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

[-] BatmanAoD@programming.dev 92 points 9 hours ago

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 11 points 3 hours ago

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

[-] random72guy@lemmy.world 12 points 5 hours ago

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 4 points 3 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...

[-] xmunk@sh.itjust.works 62 points 9 hours ago

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

[-] frezik@midwest.social 29 points 8 hours ago

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

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

[-] Benjaben@lemmy.world 4 points 3 hours ago

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

Well yeah just delete the pointer to it!

[-] BatmanAoD@programming.dev 19 points 7 hours ago

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

[-] groet@feddit.org 4 points 3 hours ago

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 2 points 57 minutes ago

Eh, trillion is a constant

[-] FiskFisk33@startrek.website 6 points 7 hours ago

amortized O(0)

[-] vithigar@lemmy.ca 11 points 8 hours ago

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

[-] db2@lemmy.world 3 points 7 hours ago

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

[-] SubArcticTundra@lemmy.ml 10 points 9 hours ago

What library are you using for that?

[-] SkaveRat@discuss.tchncs.de 7 points 4 hours ago

In Python you just use

import destroy_universe
[-] jcg@halubilo.social 22 points 9 hours ago* (last edited 9 hours ago)

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 3 points 1 hour ago

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

[-] onlinepersona@programming.dev 1 points 1 hour ago* (last edited 1 hour 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 8 hours ago

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

[-] ChaoticNeutralCzech@feddit.org 39 points 9 hours ago
[-] MajorHavoc@programming.dev 23 points 8 hours ago
// portability

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

[-] TheTechnician27@lemmy.world 31 points 9 hours ago

CosmicRaySort.

[-] aeharding@vger.social 7 points 7 hours ago* (last edited 6 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  ✨

this post was submitted on 08 Nov 2024
380 points (100.0% liked)

Programmer Humor

19488 readers
609 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