diff --git a/src/sorts/IterativeCombSort.java b/src/sorts/IterativeCombSort.java new file mode 100644 index 00000000..4842d18d --- /dev/null +++ b/src/sorts/IterativeCombSort.java @@ -0,0 +1,71 @@ +package sorts; + +import templates.Sort; +import utils.Delays; +import utils.Highlights; +import utils.Reads; +import utils.Writes; + +/* + * +The MIT License (MIT) + +Copyright (c) 2020 aphitorite + +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 IterativeCombSort extends Sort { + public IterativeCombSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) { + super(delayOps, markOps, readOps, writeOps); + + this.setSortPromptID("Iterative Comb"); + this.setRunAllID("Iterative Comb Sort"); + this.setReportSortID("Iterative Combsort"); + this.setCategory("Exchange Sorts"); + this.isComparisonBased(true); + this.isBucketSort(false); + this.isRadixSort(false); + this.isUnreasonablySlow(false); + this.setUnreasonableLimit(0); + this.isBogoSort(false); + + } + + @Override + public void runSort(int[] array, int length, int bucketCount) { + int pow2 = (int)(Math.log(length-1)/Math.log(2)); + + for(int k = pow2; k >= 0; k--) { + int pow3 = (int)((Math.log(length) - k*Math.log(2))/Math.log(3)); + + for(int j = pow3; j >= 0; j--) { + int gap = (int)(Math.pow(2, k)*Math.pow(3, j)); + + for(int i = 0; i+gap < length; i++) { + Highlights.markArray(1, i); + Highlights.markArray(2, i+gap); + Delays.sleep(0.25); + if(Reads.compare(array[i],array[i+gap])==1) + Writes.swap(array, i, i+gap, 0.75, true, false); + } + } + } + } +} \ No newline at end of file diff --git a/src/sorts/SmartBogoSort.java b/src/sorts/SmartBogoSort.java new file mode 100644 index 00000000..cb6412f8 --- /dev/null +++ b/src/sorts/SmartBogoSort.java @@ -0,0 +1,79 @@ +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 SmartBogoSort extends BogoSorting { + + private int max; + + public SmartBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) { + super(delayOps, markOps, readOps, writeOps); + this.setSortPromptID("Smart Bogo"); + this.setRunAllID("Permutation Sort"); + this.setReportSortID("Permutation Sort"); + this.setCategory("Impractical Sorts"); + this.isComparisonBased(false); + this.isBucketSort(false); + this.isRadixSort(false); + this.isUnreasonablySlow(true); + this.setUnreasonableLimit(12); + this.isBogoSort(true); + } + + @Override + public void runSort(int[] array, int currentLen, int bucketCount) { + max = currentLen - 1; + permutationSort(array, 0); + } + + private boolean permutationSort(int[] array, int min) + { + boolean sorted = false; + int i; + for(i = max; i > min; i--) + { + if(max > min+1) + { + sorted = permutationSort(array, min+1); //permutation = recurrence relation + } + if(sorted || this.bogoIsSorted(array, max+1)) + { + return true; + } + 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); //permutation = recurrence relation + } + return false; + } +} \ No newline at end of file diff --git a/src/threads/RunExchangeSorts.java b/src/threads/RunExchangeSorts.java index 5926f30c..242ffd64 100644 --- a/src/threads/RunExchangeSorts.java +++ b/src/threads/RunExchangeSorts.java @@ -8,6 +8,7 @@ import sorts.CombSort; import sorts.DualPivotQuickSort; import sorts.GnomeSort; +import sorts.IterativeCombSort; import sorts.LLQuickSort; import sorts.LRQuickSort; import sorts.OddEvenSort; @@ -61,6 +62,7 @@ final public class RunExchangeSorts extends MultipleSortThread { private Sort LRQuickSort; private Sort DualPivotQuickSort; private Sort StableQuickSort; + private Sort IterativeCombSort; public RunExchangeSorts(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -81,6 +83,7 @@ public RunExchangeSorts(ArrayVisualizer ArrayVisualizer) { LRQuickSort = new LRQuickSort(Delays, Highlights, Reads, Writes); DualPivotQuickSort = new DualPivotQuickSort(Delays, Highlights, Reads, Writes); StableQuickSort = new StableQuickSort(Delays, Highlights, Reads, Writes); + IterativeCombSort = new IterativeCombSort(Delays, Highlights, Reads, Writes); } @Override @@ -94,6 +97,7 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunExchangeSorts.this.runIndividualSort(SmartGnomeSort, 0, array, 128, 0.025); RunExchangeSorts.this.runIndividualSort(BinaryGnomeSort, 0, array, 128, 0.025); RunExchangeSorts.this.runIndividualSort(CombSort, 0, array, 1024, 1); + RunExchangeSorts.this.runIndividualSort(IterativeCombSort, 0, array, 1024, 1); RunExchangeSorts.this.runIndividualSort(CircleSort, 0, array, 1024, 1); RunExchangeSorts.this.runIndividualSort(LLQuickSort, 0, array, 2048, ArrayManager.getShuffle() == Shuffles.RANDOM ? 1.5 : 65); RunExchangeSorts.this.runIndividualSort(LRQuickSort, 0, array, 2048, 1); diff --git a/src/threads/RunImpracticalSorts.java b/src/threads/RunImpracticalSorts.java index 69da1159..552dbca5 100644 --- a/src/threads/RunImpracticalSorts.java +++ b/src/threads/RunImpracticalSorts.java @@ -9,6 +9,7 @@ import sorts.LessBogoSort; import sorts.SillySort; import sorts.SlowSort; +import sorts.SmartBogoSort; import sorts.StoogeSort; import templates.JErrorPane; import templates.MultipleSortThread; @@ -49,13 +50,14 @@ final public class RunImpracticalSorts extends MultipleSortThread { private Sort BubbleBogoSort; private Sort LessBogoSort; private Sort CocktailBogoSort; + private Sort SmartBogoSort; private Sort BogoSort; - + public RunImpracticalSorts(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); this.sortCount = 9; this.categoryCount = this.sortCount; - + BadSort = new BadSort(Delays, Highlights, Reads, Writes); StoogeSort = new StoogeSort(Delays, Highlights, Reads, Writes); SillySort = new SillySort(Delays, Highlights, Reads, Writes); @@ -64,6 +66,7 @@ public RunImpracticalSorts(ArrayVisualizer ArrayVisualizer) { BubbleBogoSort = new BubbleBogoSort(Delays, Highlights, Reads, Writes); LessBogoSort = new LessBogoSort(Delays, Highlights, Reads, Writes); CocktailBogoSort = new CocktailBogoSort(Delays, Highlights, Reads, Writes); + SmartBogoSort = new SmartBogoSort(Delays, Highlights, Reads, Writes); BogoSort = new BogoSort(Delays, Highlights, Reads, Writes); } @@ -78,6 +81,7 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunImpracticalSorts.this.runIndividualSort(BubbleBogoSort, 0, array, 32, 0.01); RunImpracticalSorts.this.runIndividualSort(LessBogoSort, 0, array, 16, 0.0025); RunImpracticalSorts.this.runIndividualSort(CocktailBogoSort, 0, array, 16, 0.0025); + RunImpracticalSorts.this.runIndividualSort(SmartBogoSort, 0, array, 8, 1); RunImpracticalSorts.this.runIndividualSort(BogoSort, 0, array, 8, 1); Sounds.toggleSofterSounds(false); }