Add Optimized Permutation/Deterministic BogoSort (aka Optimized Smart BogoSort) #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.