From 54c0fab37b1ba817467a4e12bb7e0ffebf8d81e7 Mon Sep 17 00:00:00 2001 From: wakfi <55608093+wakfi@users.noreply.github.com> Date: Thu, 13 Aug 2020 23:59:43 -0700 Subject: [PATCH] Add Optimized Permutation/Deterministic BogoSort (aka Smart BogoSort) --- .gitignore | 27 ++++++++- src/sorts/OptimizedSmartBogoSort.java | 87 +++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/sorts/OptimizedSmartBogoSort.java diff --git a/.gitignore b/.gitignore index cee99345..d72af4a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,27 @@ *.class -/bin/ \ No newline at end of file +/bin/ +.idea/** +*.iml +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# CMake +cmake-build-*/ + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties \ No newline at end of file diff --git a/src/sorts/OptimizedSmartBogoSort.java b/src/sorts/OptimizedSmartBogoSort.java new file mode 100644 index 00000000..1d58f3c8 --- /dev/null +++ b/src/sorts/OptimizedSmartBogoSort.java @@ -0,0 +1,87 @@ +package sorts; + +import templates.BogoSorting; +import utils.Delays; +import utils.Highlights; +import utils.Reads; +import utils.Writes; + +/* MIT License + +Copyright (c) 2020 Walker Gray + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +final public class OptimizedSmartBogoSort extends BogoSorting { + + private int length; + + public OptimizedSmartBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) { + super(delayOps, markOps, readOps, writeOps); + //Alernative name, Optimized Smart BogoSort + this.setSortPromptID("Permutation"); + this.setRunAllID("Permutation Sort"); + this.setReportSortID("Permutation Sort"); + this.setCategory("Distributive Sorts"); + this.isComparisonBased(false); + this.isBucketSort(false); + this.isRadixSort(false); + this.isUnreasonablySlow(true); + this.setUnreasonableLimit(16); + this.isBogoSort(true); + } + + @Override + public void runSort(int[] array, int currentLen, int bucketCount) { + length = currentLen; + permutationSort(array, 0, currentLen-1); + } + + private boolean permutationSort(int[] array, int min, int max) + { + if(max != length-1) throw new RuntimeException(); + boolean sorted = false; + int i; + for(i = max; i > min; i--) + { + if(max > min+1) + { + sorted = permutationSort(array, min+1, max); //permutation = recurrence relation + } + if(sorted || this.bogoIsSorted(array, max+1)) + { + return sorted; + } + if((max+1-min)%2 == 0) + { + Writes.swap(array, min, i, 0, true, false); + } else { + Writes.swap(array, min, max, 0, true, false); + } + } + if(max > min+1) + { + return permutationSort(array, min+1, max); //permutation = recurrence relation + } + return false; + } +} + +