Skip to content

Conversation

@wakfi
Copy link

@wakfi wakfi commented Aug 14, 2020

Deterministic Bogosort, based on permutations, can be done with no wasted operations; every single swap creates a new permutation that has not occurred yet, no overlaps with any previous states of the array. This is accomplished using Heap's Algorithm applied with checks for sort. This is the most efficient strategy for a permutation sort. The only caveat is that I have implemented it using recursion; I've considered trying it without recursion but I feel it would add more writes and comparisons than it would save memory, especially since bogosort gets ridiculously long so quickly that the function call stack won't be able to get large enough to matter. I've done some performance checks between SmartBogoSort and this implementation (by adding 10 and 12 to the array sizes to get better benchmarks) to verify that it is actually an optimization, and confirmed that it is considerably faster and uses less operations. The maximum amount of swaps this implementation will do given a worst case input is exactly n!; the number of comparisons appears to be between 3-5 times the number of swaps in general.

I'm pulling against this branch as this is the branch that MusicTheorist#14 is open from, and I thought it would make the most sense to add it to the existing pull request; I also wanted to submit it to your consideration as you already have an implementation of the same concept awaiting approval.

@aphitorite
Copy link
Owner

When I made my version, I haven't heard of Heap's algorithm. Because this is the most optimal way to make a Permutation Sort, why not replace the old code with the new optimized version?

@aphitorite aphitorite closed this Sep 7, 2020
@wakfi
Copy link
Author

wakfi commented Sep 7, 2020

Replace your "SmartBogoSort" implementation, you mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants