diff --git a/src/sorts/insert/GambitInsertionSort.java b/src/sorts/insert/GambitInsertionSort.java index 87ee5cec..47556cd8 100644 --- a/src/sorts/insert/GambitInsertionSort.java +++ b/src/sorts/insert/GambitInsertionSort.java @@ -32,39 +32,42 @@ public GambitInsertionSort(ArrayVisualizer arrayVisualizer) { setBogoSort(false); } - + private int binSearch(int[] array, int begin, int end, int target) { - while (true) { - int delta = end - begin; - if (delta <= 0) - break; - int p = begin + delta / 2; - if (this.Reads.compareIndices(array, p, target, 0.5D, true) == 0) return p; - - if (this.Reads.compareIndices(array, p, target, 0.5D, true) > 0) { end = p; continue; } - begin = p + 1; - } - return end; + while (true) { + int delta = end - begin; + if (delta <= 0) + break; + int p = begin + delta / 2; + if (this.Reads.compareIndices(array, p, target, 0.5D, true) == 0) + return p; + + if (this.Reads.compareIndices(array, p, target, 0.5D, true) > 0) { + end = p; + continue; + } + begin = p + 1; + } + return end; } - + private void binInsert(int[] array, int len, int start, int end) { - int offset = 1; - for (; offset * offset < len; offset *= 2); - - for (int bStart = 0, bEnd = end, i = start + offset; i < end; i++) { - int target = binSearch(array, bStart, bEnd, i); - - int tmp = array[i]; - int j = i - 1; - while (j >= target && array[j] > tmp) { - this.Writes.write(array, j + 1, array[j], 0.125D, true, false); - j--; - } - array[j + 1] = tmp; - } - } + int offset = 1; + for (; offset * offset < len; offset *= 2) + ; + for (int bStart = 0, bEnd = end, i = start + offset; i < end; i++) { + int target = binSearch(array, bStart, bEnd, i); + int tmp = array[i]; + int j = i - 1; + while (j >= target && array[j] > tmp) { + this.Writes.write(array, j + 1, array[j], 0.125D, true, false); + j--; + } + array[j + 1] = tmp; + } + } @Override public void runSort(int[] array, int sortLength, int bucketCount) { diff --git a/src/sorts/insert/ReverseInsertionSort.java b/src/sorts/insert/ReverseInsertionSort.java index 9d7aa89f..575b31ff 100644 --- a/src/sorts/insert/ReverseInsertionSort.java +++ b/src/sorts/insert/ReverseInsertionSort.java @@ -18,34 +18,31 @@ public final class ReverseInsertionSort extends Sort { public ReverseInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); // TODO Auto-generated constructor stub - this.setSortListName("Reverse Insertion"); - this.setRunAllSortsName("Reverse Insertion Sort"); - this.setRunSortName("Reverse Insertsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); + this.setSortListName("Reverse Insertion"); + this.setRunAllSortsName("Reverse Insertion Sort"); + this.setRunSortName("Reverse Insertsort"); + this.setCategory("Insertion Sorts"); + this.setComparisonBased(true); + this.setBucketSort(false); + this.setRadixSort(false); + this.setUnreasonablySlow(false); + this.setUnreasonableLimit(0); + this.setBogoSort(false); } @Override public void runSort(int[] array, int length, int bucketCount) throws Exception { // TODO Auto-generated method stub - for (int i = length - 1; i >= 0; i--) { - int current = array[i]; - int pos = i + 1; - - while (pos <= length - 1 && Reads.compareValues(array[pos], current) < 0) { - Writes.write(array, pos - 1, array[pos], 0.015D, true, false); - pos++; - } - Writes.write(array, pos - 1, current, 0.015D, true, false); - } - } + for (int i = length - 1; i >= 0; i--) { + int current = array[i]; + int pos = i + 1; - - + while (pos <= length - 1 && Reads.compareValues(array[pos], current) < 0) { + Writes.write(array, pos - 1, array[pos], 0.015D, true, false); + pos++; + } + Writes.write(array, pos - 1, current, 0.015D, true, false); + } + } } diff --git a/src/threads/RunInsertionSorts.java b/src/threads/RunInsertionSorts.java index 31bad6c7..b7c75cdd 100644 --- a/src/threads/RunInsertionSorts.java +++ b/src/threads/RunInsertionSorts.java @@ -54,58 +54,67 @@ final public class RunInsertionSorts extends MultipleSortThread { private Sort ShellSortParallel; private Sort ShuffledTreeSort; private Sort TreeSort; + private Sort AdaptiveBinaryInsertionSort; + private Sort GambitInsertionSort; + private Sort ReverseInsertionSort; public RunInsertionSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); this.sortCount = 21; this.categoryCount = this.sortCount; - InsertionSort = new InsertionSort(this.arrayVisualizer); - DoubleInsertionSort = new DoubleInsertionSort(this.arrayVisualizer); - BinaryInsertionSort = new BinaryInsertionSort(this.arrayVisualizer); - TriSearchInsertionSort = new TriSearchInsertionSort(this.arrayVisualizer); - FibonacciInsertionSort = new FibonacciInsertionSort(this.arrayVisualizer); - UnstableInsertionSort = new UnstableInsertionSort(this.arrayVisualizer); - ShellSort = new ShellSort(this.arrayVisualizer); - RecursiveShellSort = new RecursiveShellSort(this.arrayVisualizer); - RendezvousSort = new RendezvousSort(this.arrayVisualizer); - RoomSort = new RoomSort(this.arrayVisualizer); - LibrarySort = new LibrarySort(this.arrayVisualizer); - PatienceSort = new PatienceSort(this.arrayVisualizer); - ClassicTreeSort = new ClassicTreeSort(this.arrayVisualizer); - AATreeSort = new AATreeSort(this.arrayVisualizer); - AVLTreeSort = new AVLTreeSort(this.arrayVisualizer); - SplaySort = new SplaySort(this.arrayVisualizer); - ClassicLibrarySort = new ClassicLibrarySort(this.arrayVisualizer); - CocktailShellSort = new CocktailShellSort(this.arrayVisualizer); - ShellSortParallel = new ShellSortParallel(this.arrayVisualizer); - ShuffledTreeSort = new ShuffledTreeSort(this.arrayVisualizer); - TreeSort = new TreeSort(this.arrayVisualizer); + InsertionSort = new InsertionSort(this.arrayVisualizer); + DoubleInsertionSort = new DoubleInsertionSort(this.arrayVisualizer); + BinaryInsertionSort = new BinaryInsertionSort(this.arrayVisualizer); + TriSearchInsertionSort = new TriSearchInsertionSort(this.arrayVisualizer); + FibonacciInsertionSort = new FibonacciInsertionSort(this.arrayVisualizer); + UnstableInsertionSort = new UnstableInsertionSort(this.arrayVisualizer); + ShellSort = new ShellSort(this.arrayVisualizer); + RecursiveShellSort = new RecursiveShellSort(this.arrayVisualizer); + RendezvousSort = new RendezvousSort(this.arrayVisualizer); + RoomSort = new RoomSort(this.arrayVisualizer); + LibrarySort = new LibrarySort(this.arrayVisualizer); + PatienceSort = new PatienceSort(this.arrayVisualizer); + ClassicTreeSort = new ClassicTreeSort(this.arrayVisualizer); + AATreeSort = new AATreeSort(this.arrayVisualizer); + AVLTreeSort = new AVLTreeSort(this.arrayVisualizer); + SplaySort = new SplaySort(this.arrayVisualizer); + ClassicLibrarySort = new ClassicLibrarySort(this.arrayVisualizer); + CocktailShellSort = new CocktailShellSort(this.arrayVisualizer); + ShellSortParallel = new ShellSortParallel(this.arrayVisualizer); + ShuffledTreeSort = new ShuffledTreeSort(this.arrayVisualizer); + TreeSort = new TreeSort(this.arrayVisualizer); + AdaptiveBinaryInsertionSort = new AdaptiveBinaryInsertionSort(this.arrayVisualizer); + GambitInsertionSort = new GambitInsertionSort(this.arrayVisualizer); + ReverseInsertionSort = new ReverseInsertionSort(this.arrayVisualizer); } @Override protected synchronized void executeSortList(int[] array) throws Exception { - RunInsertionSorts.this.runIndividualSort(InsertionSort, 0, array, 128, 0.005, false); - RunInsertionSorts.this.runIndividualSort(DoubleInsertionSort, 0, array, 128, 0.002, false); - RunInsertionSorts.this.runIndividualSort(BinaryInsertionSort, 0, array, 128, 0.025, false); - RunInsertionSorts.this.runIndividualSort(TriSearchInsertionSort, 0, array, 128, 1, false); - RunInsertionSorts.this.runIndividualSort(FibonacciInsertionSort, 0, array, 128, 0.025, false); - RunInsertionSorts.this.runIndividualSort(UnstableInsertionSort, 0, array, 128, 0.2, false); - RunInsertionSorts.this.runIndividualSort(ShellSort, 0, array, 256, 0.1, false); - RunInsertionSorts.this.runIndividualSort(CocktailShellSort, 0, array, 256, 0.1, false); - RunInsertionSorts.this.runIndividualSort(RecursiveShellSort, 0, array, 256, 0.1, false); - RunInsertionSorts.this.runIndividualSort(ShellSortParallel, 0, array, 256, 0.1, false); - RunInsertionSorts.this.runIndividualSort(RendezvousSort, 0, array, 256, 0.1, false); - RunInsertionSorts.this.runIndividualSort(RoomSort, 0, array, 512, 0.05, false); - RunInsertionSorts.this.runIndividualSort(LibrarySort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(ClassicLibrarySort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(PatienceSort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(ClassicTreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false); - RunInsertionSorts.this.runIndividualSort(TreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false); - RunInsertionSorts.this.runIndividualSort(ShuffledTreeSort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(AATreeSort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(AVLTreeSort, 0, array, 2048, 1, false); - RunInsertionSorts.this.runIndividualSort(SplaySort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(InsertionSort, 0, array, 128, 0.005, false); + RunInsertionSorts.this.runIndividualSort(ReverseInsertionSort, 0, array, 128, 0.005, false); + RunInsertionSorts.this.runIndividualSort(DoubleInsertionSort, 0, array, 128, 0.002, false); + RunInsertionSorts.this.runIndividualSort(BinaryInsertionSort, 0, array, 128, 0.025, false); + RunInsertionSorts.this.runIndividualSort(AdaptiveBinaryInsertionSort, 0, array, 128, 0.025, false); + RunInsertionSorts.this.runIndividualSort(TriSearchInsertionSort, 0, array, 128, 1, false); + RunInsertionSorts.this.runIndividualSort(FibonacciInsertionSort, 0, array, 128, 0.025, false); + RunInsertionSorts.this.runIndividualSort(GambitInsertionSort, 0, array, 128, 0.025, false); + RunInsertionSorts.this.runIndividualSort(UnstableInsertionSort, 0, array, 128, 0.2, false); + RunInsertionSorts.this.runIndividualSort(ShellSort, 0, array, 256, 0.1, false); + RunInsertionSorts.this.runIndividualSort(CocktailShellSort, 0, array, 256, 0.1, false); + RunInsertionSorts.this.runIndividualSort(RecursiveShellSort, 0, array, 256, 0.1, false); + RunInsertionSorts.this.runIndividualSort(ShellSortParallel, 0, array, 256, 0.1, false); + RunInsertionSorts.this.runIndividualSort(RendezvousSort, 0, array, 256, 0.1, false); + RunInsertionSorts.this.runIndividualSort(RoomSort, 0, array, 512, 0.05, false); + RunInsertionSorts.this.runIndividualSort(LibrarySort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(ClassicLibrarySort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(PatienceSort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(ClassicTreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false); + RunInsertionSorts.this.runIndividualSort(TreeSort, 0, array, 2048, arrayManager.getShuffle() == Shuffles.RANDOM ? 1 : 5, false); + RunInsertionSorts.this.runIndividualSort(ShuffledTreeSort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(AATreeSort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(AVLTreeSort, 0, array, 2048, 1, false); + RunInsertionSorts.this.runIndividualSort(SplaySort, 0, array, 2048, 1, false); } @Override