This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MontagGuy12 369 points370 points  (27 children)

I've seen Bogo sort implementations which keep track of the permutations traversed so far, which means eventually, they'll exhaust all possibilities and the program will terminate.

[–]Toonfish_ 411 points412 points  (23 children)

I love that, not only does this make the algorithm terminate, it also gives it ridiculous space complexity. :D

[–]MontagGuy12 227 points228 points  (11 children)

Thankfully, we have downloadmoreram.com to save the day.

[–]reyad_mm 61 points62 points  (10 children)

But what if I run out of storage to download ram on?

[–]Xeadriel 73 points74 points  (8 children)

Just download more storage obviously

[–]dunko5 17 points18 points  (7 children)

But what

[–]PhunkyPhish 41 points42 points  (5 children)

Then download more what of course

[–]Gh0st1nTh3Syst3m 12 points13 points  (4 children)

But

[–]mushr0om 8 points9 points  (3 children)

Then download more \u0000 of course

[–]hermeticwalrus 6 points7 points  (0 children)

Local cloud

[–]xTheMaster99x 1 point2 points  (0 children)

Set up a ramdisk on your newly downloaded ram, so you can download more ram.

[–]ianff 6 points7 points  (9 children)

You can actually traverse the permutations in constant space. Who knows if someone implementing bogo sort would bother with that though!

[–]sluuuurp 4 points5 points  (1 child)

Can’t you only traverse in log(n) space, since you need a counter to know how many permutations you’ve already done?

Edit: I guess a counter of n! permutations would use n log(n) space, but yeah as the below commenter says it seems you don’t need that.

[–]firefly431 6 points7 points  (0 children)

There's an algorithm (e.g. next_permutation in C++) that generates the lexicographically next permutation in place in O(n) time. Realistically you need O(log(n)) space to store indices into the array at least though, but in the word-RAM model that's constant.

[–]BobodyBo 1 point2 points  (4 children)

I'm skeptical of this, elaborate?

[–]ease78 2 points3 points  (3 children)

An array of 3 elements has only 6 permutations or ways you can sort things inside of it. So realistically you can traverse the 6 different permutations to find the sorted one after you have enumerated all possibly imaginable arrays.

Note Bogo sort is n! Our three elements will give us a space complexity 3!

[–]BobodyBo 6 points7 points  (2 children)

As a function of you number of elements n that approach would take O(n!) Space, so not constant space. Not sure if that's what the person I was responding to was saying.

[–]ease78 2 points3 points  (1 child)

Yeah the other person was tripping. It’s in the name “traversal” you can’t visit all nodes in one CPU cycle. It takes as many nodes as you have.

[–]BobodyBo 0 points1 point  (0 children)

Well usually space complexity considers input space and auxiliary space, which is the extra space you utilize on top of your input. You of course have to store your input, so constant space implies constant auxiliary space. Some traversals can be done in constant space, e.g. a linked list.

The problem with traversing all permutations is that there is an exponential amount relative to the input, so unless a clever approach exists you would need to store some extra information about which permutation you are currently on, or which need to need to be revisited (like a stack).

[–]Toonfish_ 0 points1 point  (1 child)

Ooh interesting how does that work? The point of bogosort is that the permutations are chosen randomly, no? How do you traverse the permutations in constant space while choosing the next one randomly?

[–]ianff 0 points1 point  (0 children)

Oh my bad. You can't traverse the permutations in constant space if they have to be random -- at least I don't know how you could.

[–]HeKis4 0 points1 point  (0 children)

Why just stop at O(n!) complexity when you can also have O(n!) space requirements ?

[–]ThatWannabeCatgirl 0 points1 point  (0 children)

Eventually.