Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/sorts/IterativeCombSort.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
79 changes: 79 additions & 0 deletions src/sorts/SmartBogoSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 4 additions & 0 deletions src/threads/RunExchangeSorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/threads/RunImpracticalSorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down