diff --git a/src/sorts/concurrent/ApollyonSort.java b/src/sorts/concurrent/ApollyonSort.java deleted file mode 100644 index 2b9c121f..00000000 --- a/src/sorts/concurrent/ApollyonSort.java +++ /dev/null @@ -1,92 +0,0 @@ -package sorts.concurrent; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.templates.CircleSorting; - -final public class ApollyonSort extends CircleSorting { - private boolean direction = true; - - public ApollyonSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Apollyon"); - this.setRunAllSortsName("Apollyon Sort"); - this.setRunSortName("Apollyon Sort"); - this.setCategory("Concurrent Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private static int greatestPowerOfTwoLessThan(int n){ - int k = 1; - while (k < n) { - k = k << 1; - } - return k >> 1; - } - - private void compare(int[] A, int i, int j, boolean dir) - { - int cmp = Reads.compareIndices(A, i, j, 0.5, true); - - if (dir == (cmp == 1)) Writes.swap(A, i, j, 0.5, true, false); - } - - private void apollyonMerge(int[] A, int lo, int n, boolean dir) - { - if (n > 1) - { - int m = greatestPowerOfTwoLessThan(n); - - for (int i = lo; i < lo + n - m; i++) { - this.compare(A, i, i+m, dir); - } - - this.apollyonMerge(A, lo, m, dir); - this.apollyonMerge(A, lo + m, n - m, dir); - } - } - - private void apollyonSort(int[] A, int lo, int n, boolean dir) - { - if (n > 1) - { - int m = n / 2; - this.apollyonSort(A, lo, m, !dir); - this.apollyonMerge(A, lo, n, dir); - } - } - - public void changeDirection(String choice) throws Exception { - if (choice.equals("forward")) this.direction = true; - else if (choice.equals("backward")) this.direction = false; - else throw new Exception("Invalid direction for Apollyon Sort!"); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - this.end = sortLength; - int threshold = 0, n = 1; - for(; n < sortLength; n*=2, threshold++); - - threshold /= 2; - int iterations = 0; - - this.apollyonSort(array, 0, sortLength, this.direction); - - while (this.circleSortRoutine(array, 0, sortLength - 1, 0, 1) != 0) { - iterations++; - - if (iterations >= threshold) { - InsertionSort Inserter = new InsertionSort(this.arrayVisualizer); - Inserter.customInsertSort(array, 0, sortLength, 0.1, false); - break; - } - } - } -} diff --git a/src/sorts/concurrent/MatrixSortParallel.java b/src/sorts/concurrent/MatrixSortParallel.java deleted file mode 100644 index 138f7794..00000000 --- a/src/sorts/concurrent/MatrixSortParallel.java +++ /dev/null @@ -1,148 +0,0 @@ -package sorts.concurrent; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -Idea made by Control#2866 in The Studio Discord Server (https://discord.com/invite/2xGkKC2) -*/ - -final public class MatrixSortParallel extends Sort { - - public MatrixSortParallel(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Matrix (Parallel)"); - this.setRunAllSortsName("Parallel Matrix Sort"); - this.setRunSortName("Parallel Matrix Sort"); - this.setCategory("Concurrent Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int[] array; - - private volatile boolean did; - - private class Insert extends Thread { - private int a, b, g; - private boolean bw; - - Insert(int a, int b, int g, boolean bw) { - this.a = a; - this.b = b; - this.g = g; - this.bw = bw; - } - public void run() { - MatrixSortParallel.this.insert(a, b, g, bw); - } - } - - private class Reversal extends Thread { - private int a, b; - - Reversal(int a, int b) { - this.a = a; - this.b = b; - } - public void run() { - Writes.reversal(array, a, b-1, 1, true, false); - } - } - - private int sqrt(int n) { - int a = 0, b = Math.min(46341, n); - - while(a < b) { - int m = (a+b)/2; - - if(m*m >= n) b = m; - else a = m+1; - } - - return a; - } - - private void insert(int a, int b, int g, boolean bw) { - for(int i = a+g, j; i < b; i += g) { - int t = array[i]; - - for(j = i; j-g >= a && Reads.compareValues(array[j-g], t) == (bw ? -1 : 1); j -= g) { - this.did = true; - Writes.write(array, j, array[j-g], 1, true, false); - } - Writes.write(array, j, t, 1, true, false); - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - this.array = array; - - int g = sqrt(sortLength); - Insert[] ins = new Insert[g]; - - int tCnt = (sortLength-1)/g + 1; - this.did = true; - - while(this.did) { - this.did = false; - - int i = 0, j = 0; - boolean bw = false; - - for(; i+g < sortLength; i += g, j++, bw = !bw) - ins[j] = new Insert(i, i+g, 1, bw); - ins[j] = new Insert(i, sortLength, 1, bw); - - for(j = 0; j < tCnt; j++) ins[j].start(); - - for(j = 0; j < tCnt; j++) { - try { - ins[j].join(); - } - catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - for(i = 0, j = 0; i < g; i++, j++) - ins[j] = new Insert(i, sortLength, g, false); - - for(j = 0; j < g; j++) ins[j].start(); - - for(j = 0; j < g; j++) { - try { - ins[j].join(); - } - catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - - tCnt = (tCnt+1)/2; - Reversal[] revs = new Reversal[tCnt]; - - int i = g, j = 0; - for(; i+g < sortLength; i += 2*g, j++) - revs[j] = new Reversal(i, i+g); - revs[j] = new Reversal(i, sortLength); - - for(j = 0; j < tCnt; j++) revs[j].start(); - - for(j = 0; j < tCnt; j++) { - try { - revs[j].join(); - } - catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } -} \ No newline at end of file diff --git a/src/sorts/concurrent/OptimizedOddEvenMergeSort.java b/src/sorts/concurrent/OptimizedOddEvenMergeSort.java deleted file mode 100644 index 0b497428..00000000 --- a/src/sorts/concurrent/OptimizedOddEvenMergeSort.java +++ /dev/null @@ -1,115 +0,0 @@ -package sorts.concurrent; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -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 OptimizedOddEvenMergeSort extends Sort { - public OptimizedOddEvenMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Odd-Even Merge"); - this.setRunAllSortsName("Optimized Odd-Even Merge Sort"); - this.setRunSortName("Optimized Odd-Even Mergesort"); - this.setCategory("Concurrent Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void compSwap(int[] array, int a, int b) { - if(Reads.compareIndices(array, a, b, 0.5, true) == 1) - Writes.swap(array, a, b, 0.5, true, false); - } - - private void compRange(int[] array, int a, int m, int s) { - for(int i = s; a+i < m; i++) - this.compSwap(array, a+i, m+i); - } - - private void compRangeExtd(int[] array, int a, int m, int p) { - int l = m-a; - - if(l > p) { - int i = a, j, d = l-p; - - for(j = 0; j < d; j++, i++) this.compSwap(array, i, i+p); - for(j = 0; j < p-d; j++, i++) this.compSwap(array, i, i+l); - for(j = 0; j < d; j++, i++) this.compSwap(array, i+d, i+l); - } - else this.compRange(array, a, m, 0); - } - - private void merge(int[] array, int a, int b) { - int m, s = (b-a)%2; - - a -= s; - m = (a+b)/2; - this.compRange(array, a, m, s); - - int l = b-a; - if(l < 4) return; - - int p; - for(p = 1; 2*p < l; p *= 2); - - while(p > 0) { - int i = a+p; - - while(i + 2*p <= m) { - this.compRange(array, i, i+p, 0); - i += 2*p; - } - this.compRangeExtd(array, i, m, p); - i = 2*m - i; - - while(i < b-p) { - this.compRange(array, i, i+p, 0); - i += 2*p; - } - p /= 2; - } - } - - private void mergeSort(int[] array, int a, int b) { - int m = (a+b)/2; - - if(m-a > 1) this.mergeSort(array, a, m); - if(b-m > 1) this.mergeSort(array, m, b); - - this.merge(array, a, b); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - this.mergeSort(array, 0, sortLength); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/DivisorSort.java b/src/sorts/distribute/DivisorSort.java deleted file mode 100644 index 902ddb0d..00000000 --- a/src/sorts/distribute/DivisorSort.java +++ /dev/null @@ -1,92 +0,0 @@ -package sorts.distribute; - -import java.util.ArrayList; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 DivisorSort extends Sort { - public DivisorSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Divisor"); - //this.setRunAllID("Most Significant Digit Radix Sort"); - this.setRunAllSortsName("Divisor Sort"); - this.setRunSortName("Divisor Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(true); - this.setRadixSort(true); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void safePush(ArrayList> aList, int index, int value) { - while (aList.size() <= index) { - aList.add(new ArrayList<>()); - } - Writes.arrayListAdd(aList.get(index), value, true, 1); - } - - private void divisorLoop(int[] array, int start, int length, int base, int log) { - int divisor = (int)Math.pow(base, log); - ArrayList> buckets = new ArrayList<>(); - - for (int i = start; i < start + length; i++) { - Highlights.markArray(1, i); - safePush(buckets, (array[i] - start) / divisor, array[i]); - } - - int current = start; - for (int i = 0; i < buckets.size(); i++) { - int size = buckets.get(i).size(); - for (int j = 0; j < size; j++) { - Writes.write(array, current + j, buckets.get(i).get(j), 1, true, false); - } - if (size > 1) { - divisorLoop(array, current, size, base, log / 2); - } - current += size; - } - - for (ArrayList bucket : buckets) { - Writes.deleteArrayList(bucket); - } - } - - @Override - public void runSort(int[] array, int sortLength, int base) throws Exception { - this.setRunAllSortsName("Divisor Sort, Base " + base); - - int highestLog = Reads.analyzeMaxCeilingLog(array, sortLength, base, 0.5, true); - - divisorLoop(array, 0, sortLength, base, highestLog / 2); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/FeatureSort.java b/src/sorts/distribute/FeatureSort.java deleted file mode 100644 index 56bdebc5..00000000 --- a/src/sorts/distribute/FeatureSort.java +++ /dev/null @@ -1,268 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import java.util.ArrayList; - - -/* -Copyright (c) 2020-2021 thatsOven - -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 FeatureSort extends Sort { - public FeatureSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Feature"); - this.setRunAllSortsName("Feature Sort"); - this.setRunSortName("Feature Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void arrayListSwap(ArrayList arr, int a, int b, int start) { - int temp = arr.get(a); - arr.set(a, arr.get(b)); - arr.set(b, temp); - Highlights.markArray(0, start+a); - Highlights.markArray(1, start+b); - Writes.changeAuxWrites(2); - Delays.sleep(1); - Highlights.clearAllMarks(); - } - - public void arrayListWrite(ArrayList arr, int at, int value, int pos) { - arr.set(at, value); - Writes.changeAuxWrites(1); - Highlights.markArray(1, pos+at); - Delays.sleep(1); - Highlights.clearAllMarks(); - } - - public void arrayListReversal(ArrayList array, int start, int length, int pos) { - Writes.changeReversals(1); - - for(int i = start; i < start + ((length - start + 1) / 2); i++) { - this.arrayListSwap(array, i, start + length - i, pos); - } - } - - private void mergeUp(ArrayList array, int leftStart, int rightStart, int end, int[] copied, int start) { - for (int i = 0; i < rightStart - leftStart; i++) { - Highlights.markArray(1, i + leftStart); - Writes.write(copied, i, array.get(i + leftStart), 1, false, false); - } - - int left = leftStart; - int right = rightStart; - for(int nxt = 0; nxt < end - leftStart; nxt++){ - if(left >= rightStart && right >= end) break; - - Highlights.markArray(1, nxt + leftStart); - Highlights.markArray(2, right); - - if(left < rightStart && right >= end){ - Highlights.clearMark(2); - this.arrayListWrite(array, nxt + leftStart, copied[(left++) - leftStart], start+leftStart); - } - else if(left >= rightStart && right < end){ - Highlights.clearMark(1); - break; - } - else if(Reads.compareValues(copied[left - leftStart], array.get(right)) <= 0){ - this.arrayListWrite(array, nxt + leftStart, copied[(left++) - leftStart], start+leftStart); - } - else { - this.arrayListWrite(array, nxt + leftStart, array.get(right++), start+leftStart); - } - } - - Highlights.clearAllMarks(); - - } - - private void mergeDown(ArrayList array, int leftStart, int rightStart, int end, int[] copied, int start) { - for (int i = 0; i < end - rightStart; i++) { - Highlights.markArray(1, i + rightStart); - Writes.write(copied, i, array.get(i + rightStart), 1, false, false); - } - - int left = rightStart - 1; - int right = end; - for (int nxt = end - leftStart - 1; nxt >= 0; nxt--) { - if (left <= leftStart && right <= rightStart) break; - - Highlights.markArray(1, leftStart + nxt); - Highlights.markArray(2, (int)Math.max(left, 0)); - - if (left < leftStart && right >= leftStart) { - Highlights.clearMark(2); - this.arrayListWrite(array, leftStart + nxt, copied[(right--) - rightStart - 1], start+leftStart); - } - else if ((left >= leftStart && right < leftStart) || right < rightStart + 1) { - Highlights.clearMark(1); - break; - } - else if (Reads.compareValues(array.get(left), copied[right - rightStart - 1]) <= 0) { - this.arrayListWrite(array, leftStart + nxt, copied[(right--) - rightStart - 1], start+leftStart); - } - else { - this.arrayListWrite(array, leftStart + nxt, array.get(left--), start+leftStart); - } - } - - Highlights.clearAllMarks(); - } - - private void merge(ArrayList array, int leftStart, int rightStart, int end, int[] aux, int start) { - if (end - rightStart < rightStart - leftStart) { - mergeDown(array, leftStart, rightStart, end, aux, start); - } else { - mergeUp(array, leftStart, rightStart, end, aux, start); - } - } - - private boolean compare(int a, int b) { - return Reads.compareValues(a, b) <= 0; - } - - private int identifyRun(ArrayList array, int index, int maxIndex, int start) { - int startIndex = index; - - Highlights.markArray(1, start+index); - if (index >= maxIndex) { - return -1; - } - - boolean cmp = compare(array.get(index), array.get(index + 1)); - index++; - Highlights.markArray(1, start+index); - - while (index < maxIndex) { - Delays.sleep(1); - boolean checkCmp = compare(array.get(index), array.get(index + 1)); - if (checkCmp != cmp) { - break; - } - index++; - Highlights.markArray(1, start+index); - } - Delays.sleep(1); - if (cmp == false) { - this.arrayListReversal(array, startIndex, index, start); - } - if (index >= maxIndex) { - return -1; - } - return index + 1; - } - - private ArrayList findRuns(ArrayList array, int maxIndex, int start) { - ArrayList runs = new ArrayList<>(); - - int lastRun = 0; - while (lastRun != -1) { - Writes.arrayListAdd(runs, lastRun); - Writes.mockWrite(runs.size(), runs.size() - 1, lastRun, 0); - lastRun = identifyRun(array, lastRun, maxIndex, start); - } - - return runs; - } - - private void pdMergeSort(ArrayList array, int length, int start, int[] aux) { - ArrayList runs = findRuns(array, length - 1, start); - - while (runs.size() > 1) { - for (int i = 0; i < runs.size() - 1; i += 2) { - int end = i + 2 >= runs.size() ? length : (runs.get(i + 2)); - merge(array, runs.get(i), runs.get(i + 1), end, aux, start); - } - for (int i = 1; i < runs.size(); i++) { - Writes.arrayListRemoveAt(runs, i); - } - } - - Writes.deleteArrayList(runs); - } - - public void insertionSort(ArrayList arr, int a, int b, int start) { - for (int i = a + 1; i < b; i++) { - int key = arr.get(i); - int j = i-1; - - while (j >= a && Reads.compareValues(key, arr.get(j)) < 0) { - Highlights.markArray(0, start+j); - this.arrayListWrite(arr, j+1, arr.get(j), start); - j--; - } - this.arrayListWrite(arr, j+1, key, start); - } - Highlights.clearAllMarks(); - } - - public void sortSubList(ArrayList subList, int start, int[] mainArray) { - int l = subList.size(); - if (l > 1) { - if (l <= 16) { - this.insertionSort(subList, 0, l, start); - } else { - this.pdMergeSort(subList, l, start, mainArray); - } - } - } - - public void featureSort(int[] array, int currentLength) { - double max = Reads.analyzeMax(array, currentLength, 0.25, true); - @SuppressWarnings("unchecked") - ArrayList[] pos = new ArrayList[currentLength]; - double posConstant = max / (currentLength + 4); - for (int i = 0; i < currentLength; i++) { - pos[i] = new ArrayList<>(); - } - for (int i = 0; i < currentLength; i++) { - Highlights.markArray(0, i); - int idx = (int)(array[i] * posConstant); - Writes.arrayListAdd(pos[idx], array[i], true, 1); - Writes.changeAuxWrites(1); - } - Highlights.clearAllMarks(); - for (int i = 0; i < pos.length; i++) { - Highlights.markArray(0, i); - Delays.sleep(1); - this.sortSubList(pos[i], i, array); - Highlights.clearAllMarks(); - } - Highlights.clearAllMarks(); - Writes.transcribe(array, pos, 0, true, false); - Writes.deleteExternalArray(pos); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.featureSort(array, currentLength); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/ImmediateShatterSort.java b/src/sorts/distribute/ImmediateShatterSort.java deleted file mode 100644 index 8d8d4201..00000000 --- a/src/sorts/distribute/ImmediateShatterSort.java +++ /dev/null @@ -1,52 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.ShatterSorting; - -/* - * -MIT License - -Copyright (c) 2019 w0rthy - -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 ImmediateShatterSort extends ShatterSorting { - public ImmediateShatterSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Immediate Shatter"); - this.setRunAllSortsName("Immediate Shatter Sort"); - this.setRunSortName("Immediate Shatter Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(false); // It is a bucket sort, but there is no user input - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - this.shatterPartition(array, sortLength, 1); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/InPlaceMSDRadixSort.java b/src/sorts/distribute/InPlaceMSDRadixSort.java deleted file mode 100644 index f86c9b76..00000000 --- a/src/sorts/distribute/InPlaceMSDRadixSort.java +++ /dev/null @@ -1,88 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2019 w0rthy - -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 InPlaceMSDRadixSort extends Sort { - public InPlaceMSDRadixSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("In-Place MSD Radix"); - this.setRunAllSortsName("In-Place MSD Radix Sort, Base 4"); - this.setRunSortName("In-Place MSD Radixsort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(true); - this.setRadixSort(true); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void radixMSD(int[] array, int length, int min, int max, int radix, int pow, double sleep) { - if(min >= max || pow < 0) - return; - - Highlights.markArray(3, max - 1); - Highlights.markArray(4, min); - - int[] indices = new int[radix]; - Writes.changeAllocAmount(indices.length); - for (int i = 0; i < radix; i++) { - indices[i] = min; - } - - for (int i = min; i < max; i++) { - Highlights.markArray(1, i); - int temp = array[i]; - int digit = Reads.getDigit(temp, pow, radix); - for (int j = radix - 1; j > digit; j--) { - if (indices[j] != indices[j - 1]) - Writes.write(array, indices[j], array[indices[j - 1]], sleep, true, false); - Writes.write(indices, j, indices[j] + 1, 0, false, true); - } - Writes.write(array, indices[digit], temp, sleep, true, false); - Writes.write(indices, digit, indices[digit] + 1, 0, false, true); - } - - for (int i = 0; i < radix; i++) { - int subMin = i == 0 ? min : indices[i - 1]; - this.radixMSD(array, length, subMin, indices[i], radix, pow - 1, sleep); - } - - Writes.changeAllocAmount(-indices.length); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - int highestpower = Reads.analyzeMaxLog(array, sortLength, bucketCount, 0.5, true); - - radixMSD(array, sortLength, 0, sortLength, bucketCount, highestpower, 1); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/LMSDRadixSort.java b/src/sorts/distribute/LMSDRadixSort.java deleted file mode 100644 index 51a12f0f..00000000 --- a/src/sorts/distribute/LMSDRadixSort.java +++ /dev/null @@ -1,103 +0,0 @@ -package sorts.distribute; - -import java.util.ArrayList; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -L/MSD Radix Sort Sort 2021 Copyright (C) thatsOven -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -thanks to Tanoshi, that came up with the idea too, and made me make a Java port of the algorithm -*/ - -final public class LMSDRadixSort extends Sort { - - public LMSDRadixSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("L/MSD Radix"); - this.setRunAllSortsName("L/MSD Radix Sort"); - this.setRunSortName("Least/Most Significant Digit Radix Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(true); - this.setRadixSort(true); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - int base; - - public ArrayList[] radixSort(int[] array, int start, int end, int place) { - @SuppressWarnings("unchecked") - ArrayList[] registers = new ArrayList[this.base]; - - for(int i = 0; i < this.base; i++) registers[i] = new ArrayList<>(); - - for (int i = start; i < end; i++) { - Highlights.markArray(1, i); - int digit = Reads.getDigit(array[i], place, this.base); - Writes.arrayListAdd(registers[digit], array[i]); - - Writes.mockWrite(end - start, digit, array[i], 1); - } - - Highlights.clearAllMarks(); - - int counter = start; - for (int i = 0; i < this.base; i++) { - for (int j = 0; j < registers[i].size(); j++) { - Writes.write(array, counter++, registers[i].get(j), 1, true, false); - } - } - - return registers; - } - - - public void lmsdRadixSort(int[] array, int mina, int maxa, int place, int maxPlace) { - if (maxPlace < place || maxa - mina <= 1) return; - - ArrayList[] registers = this.radixSort(array, mina, maxa, place); - Writes.deleteExternalArray(registers); - - if (place != maxPlace) { - registers = this.radixSort(array, mina, maxa, maxPlace); - - int sum = 0; - - for (int i = 0; i < registers.length; i++) { - this.lmsdRadixSort(array, sum + mina, sum + mina + registers[i].size(), place + 1, maxPlace - 1); - - sum += registers[i].size(); - Writes.arrayListClear(registers[i]); - Writes.changeAllocAmount(-registers[i].size()); - } - - Writes.deleteExternalArray(registers); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) throws Exception { - this.setRunAllSortsName("Least/Most Significant Digit Radix Sort, Base " + bucketCount); - this.base = bucketCount; - - int highestpower = Reads.analyzeMaxLog(array, currentLength, bucketCount, 0.5, true); - - this.lmsdRadixSort(array, 0, currentLength, 0, highestpower); - - } -} \ No newline at end of file diff --git a/src/sorts/distribute/OptimizedIndexSort.java b/src/sorts/distribute/OptimizedIndexSort.java deleted file mode 100644 index a6c680b5..00000000 --- a/src/sorts/distribute/OptimizedIndexSort.java +++ /dev/null @@ -1,69 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020-2021 Gaming32 - -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 OptimizedIndexSort extends Sort { - public OptimizedIndexSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Index"); - this.setRunAllSortsName("Optimized Index Sort"); - this.setRunSortName("Optimized Indexsort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - int min = Reads.analyzeMin(array, sortLength, 0.5, true); - - for (int i = 0; i < sortLength; i++) { - Highlights.markArray(2, i); - int current = array[i]; - int cmpCount = 0; - while (Reads.compareValues(i, current - min) != 0 && cmpCount < sortLength) { - int tmp = array[current - min]; - Writes.write(array, current - min, current, 0.5, true, false); - current = tmp; - cmpCount++; - } - if (cmpCount >= sortLength - 1) break; - if (cmpCount > 0) - Writes.write(array, i, current, 0.5, true, false); - Delays.sleep(0.5); - Highlights.clearMark(1); - } - } -} \ No newline at end of file diff --git a/src/sorts/distribute/OptimizedPigeonholeSort.java b/src/sorts/distribute/OptimizedPigeonholeSort.java deleted file mode 100644 index 0bb95a91..00000000 --- a/src/sorts/distribute/OptimizedPigeonholeSort.java +++ /dev/null @@ -1,84 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 OptimizedPigeonholeSort extends Sort { - public OptimizedPigeonholeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Pigeonhole"); - this.setRunAllSortsName("Optimized Pigeonhole Sort"); - this.setRunSortName("Optimized Pigeonhole Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - int min = Integer.MAX_VALUE; - int max = Integer.MIN_VALUE; - - for(int i = 0; i < sortLength; i++) { - if(array[i] < min) { - min = array[i]; - } - if(array[i] > max) { - max = array[i]; - } - } - - int mi = min; - int size = max - mi + 1; - int[] holes = Writes.createExternalArray(size); - - for(int x = 0; x < sortLength; x++) { - Writes.write(holes, array[x] - mi, holes[array[x] - mi] + 1, 1, false, true); - Highlights.markArray(1, x); - } - - int j = 0; - - for(int count = 0; count < size; count++) { - for (int i = 0; i < holes[count]; i++) { - Writes.write(array, j, count + mi, 1, false, false); - - Highlights.markArray(1, j); - j++; - } - } - - Writes.deleteExternalArray(holes); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/QuickBinaryRadixSort.java b/src/sorts/distribute/QuickBinaryRadixSort.java deleted file mode 100644 index 7d49d33e..00000000 --- a/src/sorts/distribute/QuickBinaryRadixSort.java +++ /dev/null @@ -1,132 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -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 QuickBinaryRadixSort extends Sort { - public QuickBinaryRadixSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Quick Binary Radix"); - this.setRunAllSortsName("Quick Binary Radix Sort"); - this.setRunSortName("Quick Binary Radix Sort"); - this.setCategory("Distribution Sorts"); - this.setComparisonBased(false); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public int partition(int[] array, int a, int b, int bit) { - int i = a - 1; - int j = b; - - while(true) { - i++; - while(i < b && !Reads.getBit(array[i], bit)) { - Highlights.markArray(1, i); - Delays.sleep(0.5); - i++; - } - - j--; - while(j >= a && Reads.getBit(array[j], bit)) { - Highlights.markArray(2, j); - Delays.sleep(0.5); - j--; - } - - if(i < j) Writes.swap(array, i, j, 1, true, false); - else return i; - } - } - - private void radixLSD(int[] array, int a, int b, int p, int bit) { - int m = p; - - for(int i = a; i < b; i++) { - if(!Reads.getBit(array[i], bit)) m++; - Highlights.markArray(1, i); - Highlights.markArray(2, m); - Delays.sleep(0.5); - } - - for(int i = a; i < b; i++) { - if(Reads.getBit(array[i], bit)) - Writes.swap(array, i, m++, 0.5, true, false); - else - Writes.swap(array, i, p++, 0.5, true, false); - } - } - - private void radixLSDSort(int[] array, int a, int b, int p, int maxBit) { - int pow = 0, length = b-a; - - while(pow < maxBit) { - this.radixLSD(array, a, b, p, pow++); - - if(pow >= maxBit) { - for(int i = 0; i < length; i++) - Writes.swap(array, a+i, p+i, 0.5, true, false); - return; - } - else this.radixLSD(array, p, p+length, a, pow++); - } - } - - private void quickRadixSort(int[] array, int a, int b, int maxBit) { - int start = a, end = b; - while(maxBit >= 0) { - int p = this.partition(array, start, end, maxBit); - - int left = p-start; - int right = end-p; - - if(left < right) { - this.radixLSDSort(array, start, p, p, maxBit); - start = p; - } - else { - this.radixLSDSort(array, p, end, p-right, maxBit); - end = p; - } - Highlights.clearMark(2); - - maxBit--; - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int maxBit = Reads.analyzeBit(array, length); - this.quickRadixSort(array, 0, length, maxBit); - } -} \ No newline at end of file diff --git a/src/sorts/distribute/SliceBogoSort.java b/src/sorts/distribute/SliceBogoSort.java deleted file mode 100644 index ccde6143..00000000 --- a/src/sorts/distribute/SliceBogoSort.java +++ /dev/null @@ -1,40 +0,0 @@ -package sorts.distribute; - -import main.ArrayVisualizer; -import sorts.templates.BogoSorting; - -/** - * Slice Bogosort repeatedly shuffles random ranges of the array until it is sorted. - */ -public final class SliceBogoSort extends BogoSorting { - public SliceBogoSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Slice Bogo"); - this.setRunAllSortsName("Slice Bogo Sort"); - this.setRunSortName("Slice Bogosort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(10); - this.setBogoSort(true); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - while (!this.isArraySorted(array, length)) { - int index1 = BogoSorting.randInt(0, length), - index2 = BogoSorting.randInt(0, length); - - Highlights.markArray(3, index1); - Highlights.markArray(4, index2); - - if (index1 < index2) - this.bogoSwap(array, index1, index2+1, false); - else - this.bogoSwap(array, index2, index1+1, false); - } - } -} diff --git a/src/sorts/distribute/StaticSort.java b/src/sorts/distribute/StaticSort.java index 1bccf0cc..5d0c0d1f 100644 --- a/src/sorts/distribute/StaticSort.java +++ b/src/sorts/distribute/StaticSort.java @@ -1,7 +1,7 @@ package sorts.distribute; import main.ArrayVisualizer; -import sorts.insert.UnstableInsertionSort; +import sorts.insert.InsertionSort; import sorts.select.MaxHeapSort; import sorts.templates.Sort; @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal final public class StaticSort extends Sort { MaxHeapSort heapSorter; - UnstableInsertionSort insertSorter; + InsertionSort insertSorter; public StaticSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -122,7 +122,7 @@ public void staticSort(int[] array, int a, int b) { if (e - s > 16) heapSorter.customHeapSort(array, s, e, 1); else - insertSorter.unstableInsertionSort(array, s, e); + insertSorter.customInsertSort(array, s, e, 1, false); } Writes.deleteExternalArray(count); @@ -132,7 +132,7 @@ public void staticSort(int[] array, int a, int b) { @Override public void runSort(int[] mainArray, int size, int bucketCount) throws Exception { heapSorter = new MaxHeapSort(this.arrayVisualizer); - insertSorter = new UnstableInsertionSort(this.arrayVisualizer); + insertSorter = new InsertionSort(this.arrayVisualizer); this.staticSort(mainArray, 0, size); } diff --git a/src/sorts/exchange/AwkwardSort.java b/src/sorts/exchange/AwkwardSort.java deleted file mode 100644 index 8483c495..00000000 --- a/src/sorts/exchange/AwkwardSort.java +++ /dev/null @@ -1,54 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author aphitorite - * - */ -public final class AwkwardSort extends Sort { - - public AwkwardSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Awkward"); - setRunAllSortsName("Awkward Sort"); - setRunSortName("Awkward Sort"); - setCategory("Impractical Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(4096); - setBogoSort(false); - - } - - private void awkward(int[] arr, int l, int pos) { - if (l == 1) { - return; - } - awkward(arr, l / 2, pos); - awkward(arr, l / 2 + l % 2, pos + l / 2); - - for (int i = 0; i < l / 2; i++) { - int a = pos + i; - int b = pos + l / 2 + l % 2 + i; - - if (this.Reads.compareIndices(arr, a, b, 0.02D, true) == 1) { - this.Writes.swap(arr, a, b, 0.02D, true, false); - } - } - awkward(arr, l / 2 + l % 2, pos + l / 4); - - awkward(arr, l / 2, pos); - awkward(arr, l / 2 + l % 2, pos + l / 2); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - awkward(array, sortLength, 0); - - } - -} diff --git a/src/sorts/exchange/ChinottoSort.java b/src/sorts/exchange/ChinottoSort.java deleted file mode 100644 index ed643ea5..00000000 --- a/src/sorts/exchange/ChinottoSort.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author yuji - * @author McDude_73 - * - */ -public final class ChinottoSort extends Sort { - - /** - * @param arrayVisualizer - */ - public ChinottoSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Chinotto"); - setRunAllSortsName("Chinotto Sort"); - setRunSortName("Chinottosort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int length, int bucketCount) throws Exception { - boolean done = false; - int gap = 1; - - while (!done) { - int i = 0; - done = true; - while (i + gap < length) { - if (this.Reads.compareValues(array[i], array[i + gap]) == 1) { - done = false; - this.Writes.multiSwap(array, i, i + gap, 0.2D, true, false); - gap++; - } else if (gap >= 2) { - gap--; - } - - i++; - } - while (i - gap > 0) { - if (this.Reads.compareValues(array[i - gap], array[i]) == 1) { - done = false; - this.Writes.multiSwap(array, i, i - gap, 0.2D, true, false); - gap++; - } else if (gap >= 2) { - gap--; - } - - i--; - } - } - - } - -} diff --git a/src/sorts/exchange/CircleMergeSort.java b/src/sorts/exchange/CircleMergeSort.java deleted file mode 100644 index 0ad3a1e3..00000000 --- a/src/sorts/exchange/CircleMergeSort.java +++ /dev/null @@ -1,45 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.IterativeCircleSorting; - -/* - * -Copyright (c) rosettacode.org. -Permission is granted to copy, distribute and/or modify this document -under the terms of the GNU Free Documentation License, Version 1.2 -or any later version published by the Free Software Foundation; -with no Invariant Sections, no Front-Cover Texts, and no Back-Cover -Texts. A copy of the license is included in the section entitled "GNU -Free Documentation License". - * - */ - -final public class CircleMergeSort extends IterativeCircleSorting { - public CircleMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Circle Merge"); - this.setRunAllSortsName("Circle Merge Sort"); - this.setRunSortName("Circle Mergesort"); - this.setCategory("Exchange 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 sortLength, int bucketCount) throws Exception { - this.end = sortLength; - int n = 1; - for(; n <= sortLength; n*=2) { - int numberOfSwaps = 0; - do { - numberOfSwaps = this.circleSortRoutine(array, n, 0.2); - } while (numberOfSwaps != 0); - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/CocktailGrateSort.java b/src/sorts/exchange/CocktailGrateSort.java deleted file mode 100644 index 7416e475..00000000 --- a/src/sorts/exchange/CocktailGrateSort.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude_73 - * @author EilrahcF - * @implNote This sorting algorithm is bad for arrays, but good for linked - * lists. - * - */ -public final class CocktailGrateSort extends Sort { - - /** - * @param arrayVisualizer - */ - public CocktailGrateSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Cocktail Grate"); - setRunAllSortsName("Cocktail Grate Sort"); - setRunSortName("Cocktail Gratesort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(512); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - boolean sorted = false; - while (!sorted) { - sorted = true; - int i; - for (i = 0; i < currentLength - 1; i++) { - for (int j = currentLength - 1; j > i; j--) { - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - this.Delays.sleep(0.25D); - if (this.Reads.compareValues(array[i], array[j]) > 0) { - sorted = false; - this.Writes.swap(array, i, j, 0.1D, true, false); - break; - } - } - } - if (sorted) - break; - for (i = 0; i < currentLength - 1; i++) { - for (int j = i + 1; j < currentLength; j++) { - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - this.Delays.sleep(0.25D); - if (this.Reads.compareValues(array[i], array[j]) > 0) { - this.Writes.swap(array, i, j, 0.1D, true, false); - break; - } - } - } - } - - } - -} diff --git a/src/sorts/exchange/DandelionSort.java b/src/sorts/exchange/DandelionSort.java deleted file mode 100644 index 9c03854c..00000000 --- a/src/sorts/exchange/DandelionSort.java +++ /dev/null @@ -1,49 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author yuji - * @author McDude_73 - * - */ -public final class DandelionSort extends Sort { - - public DandelionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Dandelion"); - setRunAllSortsName("Dandelion Sort"); - setRunSortName("Dandelion Sort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - for (int b = 0; b < sortLength; ) { - this.Highlights.markArray(1, b); - int pointer = b; - boolean anyswap = false; - - while (pointer < sortLength - 1 && this.Reads.compareIndices(array, pointer + 1, pointer, 0.25D, true) < 0) { - this.Writes.swap(array, pointer, pointer + 1, 0.5D, true, false); - anyswap = true; - pointer++; - } - - if (anyswap) { - if (b > 0) b--; continue; - } b++; - } - - - } - -} diff --git a/src/sorts/exchange/FibonacciGnomeSort.java b/src/sorts/exchange/FibonacciGnomeSort.java deleted file mode 100644 index f7f0cdc0..00000000 --- a/src/sorts/exchange/FibonacciGnomeSort.java +++ /dev/null @@ -1,84 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - - -/** - * @author mingyue12 - * - */ -public final class FibonacciGnomeSort extends Sort { - - public FibonacciGnomeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Fibonacci Gnome"); - this.setRunAllSortsName("Fibonacci Gnome Sort"); - this.setRunSortName("Fibonacci Gnome Sort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public int fibonacciSearch(int[] array, int start, int end, int item) { - int fibM2 = 0; - int fibM1 = 1; - int fibM = 1; - while (fibM <= end - start) { - fibM2 = fibM1; - fibM1 = fibM; - fibM = fibM2 + fibM1; - } - - int offset = start - 1; - - while (fibM > 1) { - - int i = Math.min(offset + fibM2, end); - - Highlights.markArray(1, offset + 1); - Highlights.markArray(2, i); - - if (Reads.compareValues(array[i], item) <= 0) { - fibM = fibM1; - fibM1 = fibM2; - fibM2 = fibM - fibM1; - offset = i; - } else { - fibM = fibM2; - fibM1 -= fibM2; - fibM2 = fibM - fibM1; - } - Delays.sleep(0.6); - } - int position = ++offset; - if (Reads.compareValues(array[position], item) <= 0) { - ++position; - } - return position; - } - - public void fibonacciGnomeSort(int[] array, int length) { - for (int i = 1; i < length; i++) { - int tmp = array[i]; - int position = this.fibonacciSearch(array, 0, i - 1, tmp); - int j = i; - while (j > position) { - Writes.swap(array, j, j - 1, 0, true, false); - j--; - } - - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - fibonacciGnomeSort(array, sortLength); - - } - -} diff --git a/src/sorts/exchange/FireSort.java b/src/sorts/exchange/FireSort.java deleted file mode 100644 index 620eb6c4..00000000 --- a/src/sorts/exchange/FireSort.java +++ /dev/null @@ -1,129 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -Fire Sort is an impractical variant of Gnome Sort that forces a worst case. - -Consider that the equation S = 0.5N^2 - 0.5N represents the number of swaps -needed, variable S, for the Gnome process to reverse any list of length N -values. Fire abuses this case by tracking the number of swaps that have -been made. When this count reaches T, an incrementing check number, then it -changes this limit by N. The Gnome process will continue, but in reverse. -Once the swap tracking reaches the new T value, the limit changes by N, and -the Gnome direction reverses again. - -There are some additional situations that this algorithm could face. If the -process target reaches either bound of the list, an internal verification -system will run starting from the left. If the list is sorted, then there's -nothing more the algorithm needs to do. If it is not, the system will run -again for a reversed order. When it finds the list is sorted backward, the -limit trigger will iterate, and the Gnome direction will twist immediately. -If it is not, the verification system will fail out, the target will move -to the opposite side of the list, and continue sorting as normal. - -Reversing a list of N values with Fire Sort will do S = 0.125N^3 + 0.5N^2 -swaps, which is (0.25N(N + 4))/(N - 1) times more swaps than Gnome Sort. - -*/ -final public class FireSort extends Sort { - public FireSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Fire"); - this.setRunAllSortsName("Fire Sort"); - this.setRunSortName("Firesort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(false); - } - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int i = 1; - int twistcheck = 0; - int twistwait = 0; - int twist = -1; - int testi = 1; - boolean testpass = false; - boolean testreverse = false; - boolean anyswaps = false; - while (!testpass) { - if (twistwait < 1) { - twistcheck += currentLength; - twistwait = twistcheck; - twist *= -1; - } else { - twistwait--; - } - anyswaps = false; - while (i + 1 <= currentLength && i >= 1 && !anyswaps) { - if (Reads.compareValues(array[i - 1], array[i]) * twist > 0) { - Writes.swap(array, i - 1, i, 0.005, true, false); - i -= twist; - anyswaps = true; - } else { - i += twist; - } - } - if (i < 1) { - i = currentLength - 1; - testi = 1; - testpass = true; - while (testi != currentLength && testpass) { - if (Reads.compareValues(array[testi - 1], array[testi]) <= 0) { - testi++; - } else { - testpass = false; - testi = 1; - testreverse = true; - while (testi != currentLength && testreverse) { - if (Reads.compareValues(array[testi - 1], array[testi]) >= 0) { - testi++; - } else { - testreverse = false; - } - } - } - } - if (testreverse) { - i = 1; - twistwait = 0; - } - } - if (i + 1 > currentLength) { - i = 1; - testi = 1; - testpass = true; - while (testi != currentLength && testpass) { - if (Reads.compareValues(array[testi - 1], array[testi]) <= 0) { - testi++; - } else { - testpass = false; - testi = 1; - testreverse = true; - while (testi != currentLength && testreverse) { - if (Reads.compareValues(array[testi - 1], array[testi]) >= 0) { - testi++; - } else { - testreverse = false; - } - } - } - } - if (testreverse) { - i = currentLength - 1; - twistwait = 0; - } - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/FloatSort.java b/src/sorts/exchange/FloatSort.java deleted file mode 100644 index e737e448..00000000 --- a/src/sorts/exchange/FloatSort.java +++ /dev/null @@ -1,69 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author Lancewer - * @author McDude_73 - * - */ -public final class FloatSort extends Sort { - - public FloatSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Float"); - setRunAllSortsName("Float Sort"); - setRunSortName("Float Sort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(8192); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - boolean sorted = false; - while (!sorted) { - int h = 0; - sorted = true; - for (int g = length - 1; g > 0; g--) { - int i = h; - int j = h + 1; - - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - - while (i >= 0 && this.Reads.compareValues(array[i], array[j]) > 0) { - this.Writes.swap(array, i, j, 0.5D, true, false); - sorted = false; - i--; - j--; - } - - this.Delays.sleep(0.25D); - - if (i >= 0) { - i++; - j++; - while (j < length && this.Reads.compareValues(array[i], array[j]) > 0) { - this.Writes.swap(array, i, j, 0.5D, true, false); - sorted = false; - i++; - j++; - } - - this.Delays.sleep(0.25D); - } - - h++; - } - } - - } - -} diff --git a/src/sorts/exchange/GambitGnomeSort.java b/src/sorts/exchange/GambitGnomeSort.java deleted file mode 100644 index f8d3d521..00000000 --- a/src/sorts/exchange/GambitGnomeSort.java +++ /dev/null @@ -1,67 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -public final class GambitGnomeSort extends Sort { - - public GambitGnomeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Gambit Gnome"); - setRunAllSortsName("Gambit Gnome Sort"); - setRunSortName("Gambit Gnomesort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - 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; - } - - 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; - while (j > target && array[j] >= tmp) { - this.Writes.swap(array, j - 1, j, 0.125D, true, false); - j--; - } - - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - binInsert(array, sortLength, 0, sortLength); - Highlights.clearAllMarks(); - OptimizedGnomeSort ins = new OptimizedGnomeSort(arrayVisualizer); - ins.customSort(array, 0, sortLength, 0.1D); - - } - -} diff --git a/src/sorts/exchange/GnomeWeaveHighSort.java b/src/sorts/exchange/GnomeWeaveHighSort.java deleted file mode 100644 index 7c56cc31..00000000 --- a/src/sorts/exchange/GnomeWeaveHighSort.java +++ /dev/null @@ -1,91 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -Gnome Weave by Highest Prime is a reversal of Gnome Weave by Lowest Prime. - -The prime check runs to the first result in Lowest Prime, but Highest Prime -runs it to the last result. - -Because the sorting algorithm divides by prime numbers over and over again, -list lengths with recurrent factor trees cause it to act accordingly. Also, -if the length is a prime number itself, it will resort to plain OptiGnome. -Try lengths like 1365, 2310, or 4199, and it will appear more diverse. - -*/ -final public class GnomeWeaveHighSort extends Sort { - public GnomeWeaveHighSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Gnome Weave (High Prime)"); - this.setRunAllSortsName("Gnome Weave Sort (High Prime)"); - this.setRunSortName("Gnome Weave (High Prime)"); - this.setCategory("Exchange 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 currentLength, int bucketCount) { - int gap = currentLength; - int icheck = 1; - int i = 1; - int bound = 1; - int primetesti = 2; - double primetestrunning = 1.0; - boolean primetest = false; - boolean finalgap = false; - while (!finalgap) { - i = icheck; - bound = icheck; - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - while ((i - 1) + gap < currentLength) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - if (Reads.compareValues(array[i - 1], array[(i - 1) + gap]) > 0) { - Writes.swap(array, i - 1, (i - 1) + gap, 0.25, true, false); - if (i - gap > 0) { - i -= gap; - } - } else { - bound += gap; - i = bound; - } - } - if (gap == 1) { - finalgap = true; - } - if (icheck + 1 > gap && !finalgap) { - primetestrunning = gap; - while (primetestrunning != 1) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - gap = gap / primetesti; - icheck = 1; - } else { - icheck++; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/GnomeWeaveLowSort.java b/src/sorts/exchange/GnomeWeaveLowSort.java deleted file mode 100644 index d580e4a8..00000000 --- a/src/sorts/exchange/GnomeWeaveLowSort.java +++ /dev/null @@ -1,108 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -Gnome Weave by Lowest Prime is a mix of OptiGnome Sort and prime numbers. - -The sorting begins with setting up a gap, G. Any items a multiple of G away -from a target item, T, are compared, starting with items T and T + G. After -swapping them as needed, if an item exists at T + 2G, then T + G and T + 2G -are compared. If T + 2G is less than T + G, these two items will swap. If -this new T + G is also less than T, these two swap as well. If the upper -item in either case is greater than the other, no swap is needed and the -sort will skip forward to check for T + 3G. When the next T + XG item does -not exist, the target changes by 1, and so on until T is G - 1. - -Upon reaching the last unique target value, the gap value is tested for the -smallest possible prime number, P, it can evenly divide by. The next value -is then set to G/P, the target resets to the first value, and the sorting -continues. This repeats until G is 1, and after this, the sort is complete. - -The way I coded the prime check in the original Scratch project as well as -in the ArrayV port is a bit crude, because composite numbers are also being -checked for in this gap division. However, I get away with it since running -possible divisions that all occur evenly would attempt the prime factors of -a composite number before the composite number itself. - -Because the sorting algorithm divides by prime numbers over and over again, -list lengths with recurrent factor trees cause it to act accordingly. Also, -if the length is a prime number itself, it will resort to plain OptiGnome. -Try lengths like 1365, 2310, or 4199, and it will appear more diverse. - -*/ -final public class GnomeWeaveLowSort extends Sort { - public GnomeWeaveLowSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Gnome Weave (Low Prime)"); - this.setRunAllSortsName("Gnome Weave Sort (Low Prime)"); - this.setRunSortName("Gnome Weave (Low Prime)"); - this.setCategory("Exchange 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 currentLength, int bucketCount) { - int gap = currentLength; - int icheck = 1; - int i = 1; - int bound = 1; - int primetesti = 2; - double primetestrunning = 1.0; - boolean primetest = false; - boolean finalgap = false; - while (!finalgap) { - i = icheck; - bound = icheck; - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - while ((i - 1) + gap < currentLength) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - if (Reads.compareValues(array[i - 1], array[(i - 1) + gap]) > 0) { - Writes.swap(array, i - 1, (i - 1) + gap, 0.25, true, false); - if (i - gap > 0) { - i -= gap; - } - } else { - bound += gap; - i = bound; - } - } - if (gap == 1) { - finalgap = true; - } - if (icheck + 1 > gap && !finalgap) { - primetestrunning = gap; - while (primetestrunning == gap) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - gap = gap / primetesti; - icheck = 1; - } else { - icheck++; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/GrateSort.java b/src/sorts/exchange/GrateSort.java deleted file mode 100644 index c5eac1fc..00000000 --- a/src/sorts/exchange/GrateSort.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude_73 - * @author EilrahcF - * @implNote This sorting algorithm is bad for arrays, but good for linked - * lists. - * - */ -public final class GrateSort extends Sort { - - /** - * @param arrayVisualizer - */ - public GrateSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Grate"); - setRunAllSortsName("Grate Sort"); - setRunSortName("Gratesort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(512); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) throws Exception { - boolean sorted = false; - while (!sorted) { - sorted = true; - for (int i = 0; i < currentLength - 1; i++) { - for (int j = currentLength - 1; j > i; j--) { - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - this.Delays.sleep(0.25D); - if (this.Reads.compareValues(array[i], array[j]) > 0) { - sorted = false; - this.Writes.swap(array, i, j, 0.1D, true, false); - break; - } - } - } - } - - } - -} diff --git a/src/sorts/exchange/HeadPullSort.java b/src/sorts/exchange/HeadPullSort.java deleted file mode 100644 index c8f54456..00000000 --- a/src/sorts/exchange/HeadPullSort.java +++ /dev/null @@ -1,72 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -U8y3zJTNmM2cVMy2zJLMlMyvQcy4zIHNhVnMts2MzJ4gzLbNg82QzZXMq0HMuM2EzYjMolfMuM2b -zYdBzLXMj8yqWcy2zJHNjMypzKIgzLfNm8yWRsy4zIzMs8yuUsy4zIfMscyiT8y4zYrMhM2FzZVN -zLfNncyeIMy3zYPMvcyzTcy3zJvMrUXMt8yAzLEhzLXMhcymzLogzLfMjsyjScy1zYrNk8ypIMy3 -zJDMnc2WQcy4zILNhsylTMy2zIrMjs2VzK9SzLfNoMyKzJ1FzLfMk8ylQcy2zInNnc2NzKZEzLfM -ic2azJ5ZzLXMicyNzKogzLfMjMySzY1XzLfMi8y6Qcy1zJTNgsyuUsy2zL/MmE7Mt8yHzY5FzLXM -k0TMtsyLzL3MmCDMtMyOzL7MsVnMtsyRzKPMrE/MuMyEzYTMoVXMt82YzZfMvCDMtcyPzIjMmVTM -tMy/zY5IzLTMg8yQzZTMskHMtcyRzIDMnsycVMy3zZDMjs2ZIMy0zIjMu0XMuM2EzIXMqMy7Vsy1 -zYvMoUXMtsyKzZxOzLTNks2bzK4gzLXNoMyvTcy1zYPNhsydWcy4zJDMhs2cIMy0zYTMpcyfT8y2 -zYvMoc2HV8y0zJLMvcy5Tsy1zZvNl8yWIMy1zYrMl03MtsyOzLBFzLXMm82azKlOzLjMklTMuMyB -zYZBzLjNhM2ZTMy0zInNlSDMtcyUzZjNmknMtc2LzL7Mp07MuMybzLvMu1PMtsySzJfMn0HMuM2M -zJDMu07MtcyHzIzNnMyxScy0zZ3Ml1TMuMyAzLDMqlnMtcy9zZMgzLjMgMyszJZJzLjMh8yYU8y0 -zZLNms2NIMy3zaDMu0LMt8yPzLDMo0XMuMyUzZLMoc2ZWcy1zInMp0/Mts2AzZ3Mnk7MtMyOzZDM -rETMts2QzZogzLTMgcy5Tcy4zYLNlVnMt82BzL/MuyDMts2BzIvNnMylQ8y4zYLMh8yjzJlPzLjM -vsywzLJOzLjMhsyLzLLMoFTMtcy/zYXMr1LMt8yQzKdPzLjMjsy8TMy2zYHMo8ylIcy1zZfMkc2N -IMy2zYPMn8ydVMy3zL/Nlcy7SMy4zL7MiMyxQcy4zYPMn8yuVMy1zJPNiM2UJ8y0zIrMlM2ZzLFT -zLfMjc2CzJ/MqyDMtcyazKPNjUjMt8yMzKnNjU/Mt8yRzZzNh1fMuMyBzZLNhSDMt82YzJPNiM2Z -Scy3zIvNoM2cIMy2zL7MnUXMtsyOzJDMosypTsy3zYvMmMyhRMy0zIXNjc2ORcy4zZ3Mv8yWRMy1 -zJXMq8ykIMy2zI7Nnc2VVcy3zL/NksyeUMy1zITMg8ysIMy2zIHMgMydzKRIzLTMvcy6Rcy0zZvN -m8y6Usy4zZjNksyxzZxFzLfMksyrIMy3zIjMvs2NScy4zZvNhMyyzKVOzLjMgcyezKEgzLfMh8yr -zLlUzLjMjc2dzLxIzLbMlM2EzJ9FzLTMisyDzKHNhSDMtcyKzLJGzLXNl8yOzZXNmUnMuMyGzLnM -nVLMt8ybzJrNmsyeU8y3zIrNlVTMuMy9zILNh8yeIMy3zYDMkcyqUMy4zL3Mqc2HTMy4zL7Nis2Z -Qcy1zYvNnMywQ8y2zYHMkM2FRcy2zJLMjSHMt8yTzZfMncywCgogLSBUaGUgTWFkaG91c2UgQ0VP - -*/ -final public class HeadPullSort extends Sort { - public HeadPullSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Head Pull"); - this.setRunAllSortsName("Head Pull Sort"); - this.setRunSortName("Head Pull Sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(32); - this.setBogoSort(false); - } - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int i = 1; - int verifyi = 1; - int pull = 1; - i = 1; - while (i + 1 <= currentLength) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, i); - Delays.sleep(0.1); - if (Reads.compareValues(array[i - 1], array[i]) > 0) { - pull = i; - while (pull > 0) { - Writes.swap(array, pull - 1, pull, 0.1, true, false); - pull--; - } - i = 1; - } else { - i++; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/IndexQuickSort.java b/src/sorts/exchange/IndexQuickSort.java deleted file mode 100644 index f25305d4..00000000 --- a/src/sorts/exchange/IndexQuickSort.java +++ /dev/null @@ -1,137 +0,0 @@ -package sorts.exchange; - -import java.util.Random; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -The MIT License (MIT) - -Copyright (c) 2021 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 IndexQuickSort extends Sort { - public IndexQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Index Quick"); - this.setRunAllSortsName("Index Quick Sort"); - this.setRunSortName("Index Quicksort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void indexSort(int[] array, int[] idx, int a, int b) { - while(a < b) { - Highlights.markArray(2, a); - - if(Reads.compareOriginalValues(a, idx[a]) != 0) { - int t = array[a]; - int i = a, nxt = idx[a]; - - do { - Writes.write(array, i, array[nxt], 0, true, false); - Writes.write(idx, i, i, 0.5, false, true); - - i = nxt; - nxt = idx[nxt]; - } - while(Reads.compareOriginalValues(nxt, a) != 0); - - Writes.write(array, i, t, 0, true, false); - Writes.write(idx, i, i, 0.5, false, true); - } - a++; - } - } - - private int partition(int[] array, int[] idx, int a, int b) { - int c0 = a, c1 = c0, i; - - Random r = new Random(); - int m = a + r.nextInt(b-a); - - Highlights.markArray(2, m); - for(i = a; i < m; i++) { - Highlights.markArray(1, i); - Delays.sleep(0.25); - - if(Reads.compareValues(array[i], array[m]) <= 0) - c1++; - } - i++; - c1++; - for(; i < b; i++) { - Highlights.markArray(1, i); - Delays.sleep(0.25); - - if(Reads.compareValues(array[i], array[m]) < 0) - c1++; - } - - int p = c1-1; - - for(i = a; i < m; i++) { - Highlights.markArray(1, i); - - if(Reads.compareValues(array[i], array[m]) <= 0) - Writes.write(idx, c0++, i, 0.25, false, true); - - else Writes.write(idx, c1++, i, 0.25, false, true); - } - Highlights.markArray(1, i); - Writes.write(idx, p, i++, 0.25, false, true); - - for(; i < b; i++) { - Highlights.markArray(1, i); - - if(Reads.compareValues(array[i], array[m]) < 0) - Writes.write(idx, c0++, i, 0.25, false, true); - - else Writes.write(idx, c1++, i, 0.25, false, true); - } - - this.indexSort(array, idx, a, b); - return p; - } - - private void sort(int[] array, int[] idx, int a, int b) { - if(b-a < 2) return; - - int p = this.partition(array, idx, a, b); - this.sort(array, idx, a, p); - this.sort(array, idx, p+1, b); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int[] idx = Writes.createExternalArray(length); - this.sort(array, idx, 0, length); - Writes.deleteExternalArray(idx); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/IterativeQuickSort.java b/src/sorts/exchange/IterativeQuickSort.java deleted file mode 100644 index 9ce569dd..00000000 --- a/src/sorts/exchange/IterativeQuickSort.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude_73 - * - */ -public final class IterativeQuickSort extends Sort { - - /** - * @param arrayVisualizer - */ - public IterativeQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Iterative Quick"); - setRunAllSortsName("Iterative Quick Sort"); - setRunSortName("Iterative Quicksort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - private int partition(int[] array, int lowValue, int highValue) { - int pivot = array[highValue]; - - int i = lowValue - 1; - for (int j = lowValue; j <= highValue - 1; j++) { - if (this.Reads.compareValues(array[j], pivot) <= 0) { - i++; - this.Writes.swap(array, i, j, 1.0D, true, false); - } - } - this.Writes.swap(array, i + 1, highValue, 1.0D, true, false); - return i + 1; - } - - private void quickSort(int[] array, int startIndex, int endIndex) { - int len = endIndex - startIndex + 1; - int[] stack = new int[len]; - Writes.changeAllocAmount(len); - int top = -1; - - this.Writes.write(stack, ++top, startIndex, 0.0D, true, true); - this.Writes.write(stack, ++top, endIndex, 0.0D, true, true); - - while (top >= 0) { - endIndex = stack[top--]; - startIndex = stack[top--]; - - int p = partition(array, startIndex, endIndex); - - if (this.Reads.compareValues(p - 1, startIndex) == 1) { - this.Writes.write(stack, ++top, startIndex, 0.0D, false, true); - this.Writes.write(stack, ++top, p - 1, 0.0D, false, true); - } - - if (this.Reads.compareValues(p + 1, endIndex) == -1) { - this.Writes.write(stack, ++top, p + 1, 0.0D, false, true); - this.Writes.write(stack, ++top, endIndex, 0.0D, false, true); - } - } - Writes.changeAllocAmount(-len); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - quickSort(array, 0, sortLength - 1); - - } - -} diff --git a/src/sorts/exchange/LLQuickSortMiddlePivot.java b/src/sorts/exchange/LLQuickSortMiddlePivot.java deleted file mode 100644 index 67880e36..00000000 --- a/src/sorts/exchange/LLQuickSortMiddlePivot.java +++ /dev/null @@ -1,69 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class LLQuickSortMiddlePivot extends Sort { - public LLQuickSortMiddlePivot(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Left/Left Quick (Middle Pivot)"); - this.setRunAllSortsName("Quick Sort, Left/Left Pointers (Middle Pivot)"); - this.setRunSortName("Left/Left Quicksort (Middle Pivot)"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int partition(int[] array, int a, int b) { - int i = a, j = i, m = (a+b)/2; - - Highlights.markArray(3, m); - while(j < m) { - Highlights.clearMark(1); - Highlights.markArray(2, j); - Delays.sleep(0.5); - - if(Reads.compareValues(array[j], array[m]) <= 0) - Writes.swap(array, i++, j, 1, true, false); - - j++; - } - - Writes.swap(array, i, m, 1, true, false); - j = m+1; - m = i++; - - Highlights.markArray(3, m); - while(j < b) { - Highlights.clearMark(1); - Highlights.markArray(2, j); - Delays.sleep(0.5); - - if(Reads.compareValues(array[j], array[m]) < 0) - Writes.swap(array, i++, j, 1, true, false); - - j++; - } - - Writes.swap(array, --i, m, 1, true, false); - return i; - } - - private void quickSort(int[] array, int a, int b) { - if(b-a > 1) { - int p = this.partition(array, a, b); - this.quickSort(array, a, p); - this.quickSort(array, p+1, b); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.quickSort(array, 0, currentLength); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/LazyStableQuickSort.java b/src/sorts/exchange/LazyStableQuickSort.java deleted file mode 100644 index a868d527..00000000 --- a/src/sorts/exchange/LazyStableQuickSort.java +++ /dev/null @@ -1,161 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.insert.BinaryDoubleInsertionSort; -import sorts.insert.InsertionSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020-2021 Gaming32 (Josiah Glosson) - -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 LazyStableQuickSort extends Sort { - ReverseLazyStableSort rotater; - InsertionSort inserter; - BinaryDoubleInsertionSort fallback; - - public LazyStableQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Lazy Stable Quick"); - this.setRunAllSortsName("Lazy Stable Quick Sort"); - this.setRunSortName("Lazy Stable Quicksort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int log2(int x) { - int n = 1; - while (1 << n < x) n++; - if (1 << n > x) n--; - return n; - } - - private int findPivot(int[] array, int start, int mid, int end) { - if (Reads.compareIndices(array, start, mid, 0.5, true) < 0) { - if (Reads.compareIndices(array, mid, end, 0.5, true) < 0) { - return mid; - } else if (Reads.compareIndices(array, start, end, 0.5, true) < 0) { - return end; - } - return start; - } else { - if (Reads.compareIndices(array, mid, end, 0.5, true) > 0) { - return mid; - } else if (Reads.compareIndices(array, start, end, 0.5, true) < 0) { - return start; - } - return end; - } - } - - private int stablePartition(int[] array, int start, int end) { - int mid = start + (end - start) / 2; - int pivotPos = findPivot(array, start, mid, end - 1); - while (true) { - int pivot = array[pivotPos]; - - int ltLeft = start; - int runstart = start; - int runsize = 0; - int equalCount = 0; - for (int i = start; i < end; i++) { - Highlights.markArray(1, i); - Delays.sleep(0.5); - int comp = Reads.compareValues(array[i], pivot); - if (comp < 0) { - runsize++; - continue; - } else if (comp == 0) { - equalCount++; - } - if (runsize > 0 && runstart > start) { - rotater.rotateSmart(array, runstart, ltLeft, runsize); - } - ltLeft += runsize; - runstart = i + 1; - runsize = 0; - } - - if (equalCount == end - start) return -1; - - // Necessary if the run is at the end - if (runsize > 0) { - rotater.rotateSmart(array, runstart, ltLeft, runsize); - ltLeft += runsize; - } - - if (ltLeft == start) { - pivotPos++; - if (pivotPos == end) { - pivotPos = start; - } - while (Reads.compareValueIndex(array, pivot, pivotPos, 1, true) == 0) { - pivotPos++; - if (pivotPos == end) { - pivotPos = start; - } - } - continue; - } - - return ltLeft; - } - } - - private void stableQuickSort(int [] array, int start, int end, int depth) { - while (end - start > 16) { - if (depth == 0) { - fallback.customDoubleInsert(array, start, end, 1); - return; - } - int pivotIndex = this.stablePartition(array, start, end); - if (pivotIndex == -1) return; - int left = pivotIndex - start, right = end - pivotIndex; - if (left > right) { - this.stableQuickSort(array, pivotIndex, end, --depth); - end = pivotIndex; - } else { - this.stableQuickSort(array, start, pivotIndex, --depth); - start = pivotIndex; - } - } - inserter.customInsertSort(array, start, end, 1, false); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - rotater = new ReverseLazyStableSort(arrayVisualizer); - inserter = new InsertionSort(arrayVisualizer); - fallback = new BinaryDoubleInsertionSort(arrayVisualizer); - this.stableQuickSort(array, 0, length, 2 * log2(length)); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/LinkedIterativeQuickSort.java b/src/sorts/exchange/LinkedIterativeQuickSort.java deleted file mode 100644 index c0e9c74d..00000000 --- a/src/sorts/exchange/LinkedIterativeQuickSort.java +++ /dev/null @@ -1,96 +0,0 @@ -package sorts.exchange; - -import java.util.LinkedList; -import java.util.Queue; -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author aphitorite - * - */ -public final class LinkedIterativeQuickSort extends Sort { - - public LinkedIterativeQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Linked Iterative Quick"); - setRunAllSortsName("Linked Iterative Quick Sort, Left/Right Pointers"); - setRunSortName("Linked Iterative Left/Right Quicksort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - private class Task { - int p; - int r; - - public Task(int p, int r) { - this.p = p; - this.r = r; - } - } - - private void quickSort(int[] a, int p, int r) { - Queue tasks = new LinkedList<>(); - tasks.add(new Task(p, r)); - - while (!tasks.isEmpty()) { - Task task = tasks.remove(); - - int i = task.p; - int j = task.r; - int pivot = task.p + (task.r - task.p) / 2; - int x = a[pivot]; - - this.Highlights.markArray(3, pivot); - - while (i <= j) { - while (this.Reads.compareValues(a[i], x) == -1) { - i++; - this.Highlights.markArray(1, i); - this.Delays.sleep(0.5D); - } - while (this.Reads.compareValues(a[j], x) == 1) { - j--; - this.Highlights.markArray(2, j); - this.Delays.sleep(0.5D); - } - - if (i <= j) { - - if (i == pivot) { - this.Highlights.markArray(3, j); - } - if (j == pivot) { - this.Highlights.markArray(3, i); - } - - this.Writes.swap(a, i, j, 1.0D, true, false); - - i++; - j--; - } - } - - if (task.p < j) { - tasks.add(new Task(task.p, j)); - } - if (i < task.r) { - tasks.add(new Task(i, task.r)); - } - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - quickSort(array, 0, sortLength - 1); - - } - -} diff --git a/src/sorts/exchange/MarkovSort.java b/src/sorts/exchange/MarkovSort.java deleted file mode 100644 index 1575f00b..00000000 --- a/src/sorts/exchange/MarkovSort.java +++ /dev/null @@ -1,44 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.BogoSorting; - -/** - * Markov Sort is like Gnome Sort, but the next element to be inserted - * randomly walks within the sorted section until it is in the correct position. - * - * @author invented by Blasterfreund - * @author implemented in Java by Sam Walko (Anonymous0726) - * @author refactored by EmeraldBlock - */ -final public class MarkovSort extends BogoSorting { - public MarkovSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Markov"); - this.setRunAllSortsName("Markov Sort"); - this.setRunSortName("Markov sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(true); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - for (int i = 0; i < length-1; ++i) { - int walk = i+1; - while ( - (walk==0 ? false : Reads.compareIndices(array, walk-1, walk, this.delay, true)>0) - || (walk>i ? false : Reads.compareIndices(array, walk, walk+1, this.delay, true)>0) - ) { - int c = (walk==0 || walk<=i && BogoSorting.randBoolean()) ? 1 : -1; - Writes.swap(array, walk, walk+c, this.delay, true, false); - walk += c; - } - } - } -} diff --git a/src/sorts/exchange/MeanQuickSort.java b/src/sorts/exchange/MeanQuickSort.java deleted file mode 100644 index 7c7d40fe..00000000 --- a/src/sorts/exchange/MeanQuickSort.java +++ /dev/null @@ -1,159 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License -Copyright (c) 2020 Gaming32 -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 MeanQuickSort extends Sort { - public MeanQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Mean Quick"); - this.setRunAllSortsName("Mean Quick Sort"); - this.setRunSortName("Mean Quick Sort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int compareIntDouble(int left, double right) { - Reads.addComparison(); - - int cmpVal = 0; - - Writes.startLap(); - - if(left > right) cmpVal = 1; - else if(left < right) cmpVal = -1; - else cmpVal = 0; - - Writes.stopLap(); - - return cmpVal; - } - - private void partition(int[] array, int start, int end, int sum) { - int left = start; - int right = end; - Highlights.markArray(1, left); - Highlights.markArray(2, right); - - int count = right - left + 1; - if (count <= 1) { - return; - } - - double mean = (double)sum / (double)count; - - int lsum = 0; - int rsum = 0; - - while (left <= right) { - // while (this.compareIntDouble(array[right], mean) >= 0) { - // rsum += array[right]; - // right--; - // Delays.sleep(0.5); - // Highlights.markArray(4, right); - // } - - // if (left > right) { - // break; - // } - - // if (this.compareIntDouble(array[left], mean) >= 0) { - // Writes.swap(array, left, right, 1, false, false); - // rsum += array[right]; - // right--; - // Highlights.markArray(4, right); - // } - // Reads.addComparison(); - - // if (left > right || left >= highestEnd) { - // break; - // } - - // lsum += array[left]; - // left++; - // Highlights.markArray(3, left); - while (compareIntDouble(array[left], mean) == -1) { - lsum += array[left]; - left++; - Highlights.markArray(1, left); - Delays.sleep(0.5); - } - while (compareIntDouble(array[right], mean) == 1) { - rsum += array[right]; - right--; - Highlights.markArray(2, right); - Delays.sleep(0.5); - } - - if (left <= right) { - Writes.swap(array, left, right, 1, true, false); - - lsum += array[left]; - rsum += array[right]; - left++; - right--; - } - } - - // Highlights.clearAllMarks(); - // partition(array, start, left + 1, lsum, highestEnd); - // partition(array, left, end, rsum, highestEnd); - if (start < right) { - partition(array, start, right, lsum); - } - if (left < end) { - partition(array, left, end, rsum); - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - int sum = 0; - for (int i = 0; i < sortLength; i++) { - Highlights.markArray(1, i); - Writes.startLap(); - sum += array[i]; - Writes.stopLap(); - Delays.sleep(0.5); - } - Highlights.clearMark(1); - - partition(array, 0, sortLength - 1, sum); - - // for (int i = 1; i < sortLength; i++) { - // int j = i; - // while (j > 0 && Reads.compareIndices(array, j, j - 1, 0.25, true) == -1) { - // Writes.swap(array, j, j - 1, 0.25, true, false); - // j--; - // } - // } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/MerryGoRoundSort.java b/src/sorts/exchange/MerryGoRoundSort.java deleted file mode 100644 index 321f35f6..00000000 --- a/src/sorts/exchange/MerryGoRoundSort.java +++ /dev/null @@ -1,71 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -*/ -final public class MerryGoRoundSort extends Sort { - public MerryGoRoundSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Merry-Go-Round"); - this.setRunAllSortsName("Merry-Go-Round Sort"); - this.setRunSortName("Merry-Go-Round Sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(false); - } - @Override - public void runSort(int[] seats, int time, int nausea) { - int im = 1; - int very = 1; - int dizzy = 1; - boolean can_someone_please = false; - boolean stop_the_ride = false; - while (!stop_the_ride) { - im = very; - can_someone_please = false; - while (im + 1 <= time) { - Highlights.markArray(1, im - 1); - Highlights.markArray(2, im); - Delays.sleep(0.001); - if (Reads.compareValues(seats[im - 1], seats[im]) > 0) { - can_someone_please = true; - dizzy = im; - while (dizzy + 1 <= time) { - Writes.swap(seats, dizzy - 1, dizzy, 0.001, true, false); - dizzy += 2; - } - if (im > 1) { - im--; - } - } else { - im += 2; - } - } - if (!can_someone_please) { - very = 1; - stop_the_ride = true; - while (very != time && stop_the_ride) { - Highlights.markArray(1, very - 1); - Highlights.markArray(2, very); - Delays.sleep(0.001); - if (Reads.compareValues(seats[very - 1], seats[very]) <= 0) { - very++; - } else { - stop_the_ride = false; - } - } - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/NapoleonSort.java b/src/sorts/exchange/NapoleonSort.java deleted file mode 100644 index f0850aff..00000000 --- a/src/sorts/exchange/NapoleonSort.java +++ /dev/null @@ -1,161 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class NapoleonSort extends Sort { - public NapoleonSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Napoleon"); - this.setRunAllSortsName("Napoleon Sort"); - this.setRunSortName("Napoleon sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(8); - this.setBogoSort(false); - } - - private void tilsit(int[] array, int currentLen) { - for(int i = 0, j = currentLen - 1; i < currentLen/2; i++, j--) { - Highlights.markArray(1, i); - Highlights.markArray(2, j); - Delays.sleep(0.01); - if(Reads.compareIndices(array, i, j, 0.01, true) > 0) - Writes.swap(array, i, j, 0.01, true, false); - } - } - - private void napoleon(int[] array, int end) { - int lo = 0; - int hi = end; - - int prev = array[0]; - boolean goSmaller = true; - boolean noneFound = false; - - while(hi > lo) { - if(goSmaller) { - int next = lookEast(array, prev, lo+1, hi); - if(next == lo) { // No smaller elements - if(noneFound) { - lo++; - hi--; - } - prev = noneFound ? array[lo] : array[hi]; - goSmaller = noneFound; - noneFound = !noneFound; - } else if(next == hi) { // Next smaller element already at end - prev = array[hi]; - conquer(array, hi, lo); - noneFound = false; - } else { // Next smaller element elsewhere - prev = array[next]; - conquer(array, next, hi); - noneFound = false; - goSmaller = false; - } - } else { - int next = lookWest(array, prev, hi-1, lo); - if(next == hi) { // No larger elements - if(noneFound) { - lo++; - hi--; - } - prev = array[lo]; - goSmaller = true; - noneFound = !noneFound; - } else if(next == lo) { // Next larger element already at start - prev = array[lo]; - conquer(array, lo, hi); - noneFound = false; - } else { // Next larger element elsewhere - prev = array[next]; - conquer(array, next, lo); - noneFound = false; - goSmaller = true; - } - } - } - - } - - private int lookEast(int[] array, int prev, int start, int end) { - for(int i = start; i <= end; i++) { - if(Reads.compareValues(array[i], prev) < 0) - return i; - } - return start - 1; - } - - private int lookWest(int[] array, int prev, int start, int end) { - for(int i = start; i >= end; i--) { - if(Reads.compareValues(array[i], prev) > 0) - return i; - } - return start + 1; - } - - private void conquer(int[] array, int index, int target) { - int blockSize = 1; - while(index + blockSize - 1 < target) { - int marchTo = recruit(array, array[index], index+blockSize, target); - int spaceBetween = marchTo - index - blockSize + 1; - march(array, index, blockSize, spaceBetween); - index += spaceBetween; - blockSize++; - } - while(index - blockSize + 1 > target) { - int marchTo = recruit(array, array[index], index-blockSize, target); - int spaceBetween = index - blockSize - marchTo + 1; - march(array, marchTo, spaceBetween, blockSize); - index -= spaceBetween; - blockSize++; - } - } - - private int recruit(int[] array, int identicalTo, int start, int end) { - if(start= end; i--) { - if(Reads.compareValues(array[i], identicalTo) == 0) - return i + 1; - } - return end; - } - - private void march(int[] array, int index, int len1, int len2) { - while(len1 != 0 && len2 != 0) { - if(len1 <= len2) { - attack(array, index, index+len1, len1); - index += len1; - len2 -= len1; - } else { - attack(array, index + len1, index + len1 - len2, len2); - len1 -= len2; - } - } - } - - private void attack(int[] array, int startA, int startB, int swapsLeft) { - while(swapsLeft != 0) { - Writes.swap(array, startA++, startB++, 1, true, false); - swapsLeft--; - } - } - - @Override - public void runSort(int[] array, int currentLen, int bucketCount) { - tilsit(array, currentLen); - Highlights.clearAllMarks(); - napoleon(array, currentLen - 1); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/OddEvenWeaveHighSort.java b/src/sorts/exchange/OddEvenWeaveHighSort.java deleted file mode 100644 index 50abf3fc..00000000 --- a/src/sorts/exchange/OddEvenWeaveHighSort.java +++ /dev/null @@ -1,129 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -*/ -final public class OddEvenWeaveHighSort extends Sort { - public OddEvenWeaveHighSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Odd-Even Weave (High Prime)"); - this.setRunAllSortsName("Odd-Even Weave Sort (High Prime)"); - this.setRunSortName("Odd-Even Weave (High Prime)"); - this.setCategory("Exchange 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 currentLength, int bucketCount) { - int check = 1; - int lastbound = 2; - int lastmove = currentLength; - int move = currentLength; - int noswapsfor = 0; - int gap = currentLength; - int primetesti = 2; - int i = 1; - int boundi = 1; - double primetestrunning = 1.0; - boolean primetest = false; - boolean anyswaps = false; - boolean testpass = false; - boolean visualaesthetic = true; - while (!testpass) { - i = check; - anyswaps = false; - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - while ((i - 1) + gap < currentLength) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - if (Reads.compareValues(array[i - 1], array[(i - 1) + gap]) > 0) { - Writes.swap(array, i - 1, (i - 1) + gap, 0.25, true, false); - anyswaps = true; - } - i += move; - } - if (!anyswaps && gap != 1) { - noswapsfor++; - if (noswapsfor == move) { - noswapsfor = 0; - lastmove = move; - primetestrunning = move; - if (!visualaesthetic) { - while (primetestrunning != 1) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - move = move / primetesti; - } - visualaesthetic = false; - if (move != 1) { - primetestrunning = move; - while (primetestrunning != 1) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - gap = move / primetesti; - } else { - move = lastmove; - gap = 1; - } - check = 0; - } - } else { - noswapsfor = 0; - } - if (gap == 1) { - if (lastbound > 1) { - boundi = lastbound - 1; - } else { - boundi = 1; - } - testpass = true; - while (boundi < currentLength && testpass) { - Highlights.markArray(1, boundi - 1); - Highlights.markArray(2, boundi); - Delays.sleep(0.25); - if (Reads.compareValues(array[boundi - 1], array[boundi]) <= 0) { - boundi++; - } else { - testpass = false; - lastbound = boundi; - check = boundi; - } - } - } else { - check = (check % move) + 1; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/OddEvenWeaveLowSort.java b/src/sorts/exchange/OddEvenWeaveLowSort.java deleted file mode 100644 index 8425c922..00000000 --- a/src/sorts/exchange/OddEvenWeaveLowSort.java +++ /dev/null @@ -1,129 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -*/ -final public class OddEvenWeaveLowSort extends Sort { - public OddEvenWeaveLowSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Odd-Even Weave (Low Prime)"); - this.setRunAllSortsName("Odd-Even Weave Sort (Low Prime)"); - this.setRunSortName("Odd-Even Weave (Low Prime)"); - this.setCategory("Exchange 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 currentLength, int bucketCount) { - int check = 1; - int lastbound = 2; - int lastmove = currentLength; - int move = currentLength; - int noswapsfor = 0; - int gap = currentLength; - int primetesti = 2; - int i = 1; - int boundi = 1; - double primetestrunning = 1.0; - boolean primetest = false; - boolean anyswaps = false; - boolean testpass = false; - boolean visualaesthetic = true; - while (!testpass) { - i = check; - anyswaps = false; - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - while ((i - 1) + gap < currentLength) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, (i - 1) + gap); - Delays.sleep(0.25); - if (Reads.compareValues(array[i - 1], array[(i - 1) + gap]) > 0) { - Writes.swap(array, i - 1, (i - 1) + gap, 0.25, true, false); - anyswaps = true; - } - i += move; - } - if (!anyswaps && gap != 1) { - noswapsfor++; - if (noswapsfor == move) { - noswapsfor = 0; - lastmove = move; - primetestrunning = move; - if (!visualaesthetic) { - while (primetestrunning == move) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - move = move / primetesti; - } - visualaesthetic = false; - if (move != 1) { - primetestrunning = move; - while (primetestrunning == move) { - primetest = false; - primetesti = 2; - while (!primetest) { - if (Math.floor(primetestrunning / primetesti) == primetestrunning / primetesti) { - primetestrunning = primetestrunning / primetesti; - primetest = true; - } else { - primetesti++; - } - } - } - gap = move / primetesti; - } else { - move = lastmove; - gap = 1; - } - check = 0; - } - } else { - noswapsfor = 0; - } - if (gap == 1) { - if (lastbound > 1) { - boundi = lastbound - 1; - } else { - boundi = 1; - } - testpass = true; - while (boundi < currentLength && testpass) { - Highlights.markArray(1, boundi - 1); - Highlights.markArray(2, boundi); - Delays.sleep(0.25); - if (Reads.compareValues(array[boundi - 1], array[boundi]) <= 0) { - boundi++; - } else { - testpass = false; - lastbound = boundi; - check = boundi; - } - } - } else { - check = (check % move) + 1; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/PatternDefeatingGnomeSort.java b/src/sorts/exchange/PatternDefeatingGnomeSort.java deleted file mode 100644 index ef9a0931..00000000 --- a/src/sorts/exchange/PatternDefeatingGnomeSort.java +++ /dev/null @@ -1,53 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author Yuri-chan2007 - * - */ -public final class PatternDefeatingGnomeSort extends Sort { - - public PatternDefeatingGnomeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Pattern-Defeating Gnome"); - this.setRunAllSortsName("Pattern-Defeating Gnome Sort"); - this.setRunSortName("Pattern-Defeating Gnomesort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void gnomeSort(int[] array, int a, int b, double sleep, boolean auxwrite) { - int i = a + 1; - if(Reads.compareIndices(array, i - 1, i++, sleep, true) == 1) { - while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) == 1) i++; - Writes.reversal(array, a, i - 1, sleep, true, auxwrite); - } - else while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) <= 0) i++; - - Highlights.clearMark(2); - - while(i < b) { - - int pos = i; - while(pos > a && Reads.compareValues(array[pos - 1], array[pos]) > 0){ - Writes.swap(array, pos - 1, pos, sleep, true, auxwrite); - pos--; - } - i++; - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - gnomeSort(array, 0, sortLength, 1.0D, false); - - } - -} diff --git a/src/sorts/exchange/PseudoHeapSort.java b/src/sorts/exchange/PseudoHeapSort.java deleted file mode 100644 index e6a34926..00000000 --- a/src/sorts/exchange/PseudoHeapSort.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author yuji - * @author fungamer2 - * - */ -public final class PseudoHeapSort extends Sort { - - /** - * @param arrayVisualizer - */ - public PseudoHeapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Iterative Pseudo-Heap"); - setRunAllSortsName("Iterative Pseudo-Heap Sort"); - setRunSortName("Iterative Pseudo-Heapsort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - private boolean sift_down(int[] array, int start, int length, int root) { - boolean swapped = false; - int j = root; - while (2 * j < length) { - int k = 2 * j; - if (k < length && this.Reads.compareValues(array[start + k - 1], array[start + k]) == 1) { - k++; - } - if (this.Reads.compareIndices(array, start + j - 1, start + k - 1, 1.0D, true) == 1) { - this.Writes.swap(array, start + j - 1, start + k - 1, 1.0D, true, false); - j = k; - swapped = true; - continue; - } - break; - } - return swapped; - } - - private boolean sift(int[] array, int start, int end) { - return sift_down(array, start, end - start + 1, 1); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - boolean swapped = true; - while (swapped) { - swapped = false; - for (int i = length - 2; i >= 0; i--) { - if (sift(array, i, length)) - swapped = true; - } - } - - } - -} diff --git a/src/sorts/exchange/ReflectionSort.java b/src/sorts/exchange/ReflectionSort.java deleted file mode 100644 index 2fa58185..00000000 --- a/src/sorts/exchange/ReflectionSort.java +++ /dev/null @@ -1,70 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -Reflection Sort is an algorithm developed with unwritten impracticalities. - -Reflection Sort starts running a Cocktail Shaker. When a swap is found, the -target position is then set to a reflection across the length of the list. -Upon reaching either bound of the list, exactly like Cocktail Shaker Sort, -the search direction is turned opposite, and the swap hunt restarts. This -continues until a sweep in descending order can be made without any swaps. - -*/ -final public class ReflectionSort extends Sort { - public ReflectionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Reflection"); - this.setRunAllSortsName("Reflection Sort"); - this.setRunSortName("Reflection Sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(2048); - this.setBogoSort(false); - } - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - boolean anyswap = true; - int i = 0; - while(anyswap) { - i = 0; - anyswap = false; - while(i < currentLength) { - Highlights.markArray(1, i); - Highlights.markArray(2, i + 1); - Delays.sleep(0.125); - if(Reads.compareValues(array[i], array[i + 1]) > 0) { - Writes.swap(array, i, i + 1, 0.125, true, false); - i = (currentLength - 1) - i; - anyswap = true; - } else { - i++; - } - } - i = currentLength; - anyswap = false; - while (i > 1) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, i); - Delays.sleep(0.125); - if(Reads.compareValues(array[i - 1], array[i]) > 0) { - Writes.swap(array, i - 1, i, 0.125, true, false); - i = (currentLength - 1) - i; - anyswap = true; - } else { - i--; - } - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/ReverseBubbleSort.java b/src/sorts/exchange/ReverseBubbleSort.java deleted file mode 100644 index f4d9099f..00000000 --- a/src/sorts/exchange/ReverseBubbleSort.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author mingyue12 - * - */ -public final class ReverseBubbleSort extends Sort { - - /** - * @param arrayVisualizer - */ - public ReverseBubbleSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - // TODO Auto-generated constructor stub - this.setSortListName("Reverse Bubble"); - this.setRunAllSortsName("Reverse Bubble Sort"); - this.setRunSortName("Reverse Bubblesort"); - this.setCategory("Exchange 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 sortLength, int bucketCount) throws Exception { - // TODO Auto-generated method stub - - for(int i = 0; i < sortLength - 1; i++) { - boolean sorted = true; - for(int j = sortLength - 1; j > i; j--) { - if(Reads.compareIndices(array, j - 1, j, 0.05, true) == 1){ - Writes.swap(array, j - 1, j, 0.075, true, false); - sorted = false; - } - } - if(sorted) break; - } - - } - -} diff --git a/src/sorts/exchange/ReverseGnomeSort.java b/src/sorts/exchange/ReverseGnomeSort.java deleted file mode 100644 index e93d171b..00000000 --- a/src/sorts/exchange/ReverseGnomeSort.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* -* -MIT License -Copyright (c) 2021 mingyue12 -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. -* -*/ - -/** - * This is the reversed variant of Optimized Gnomesort. - * @author mingyue12 - * - */ -public final class ReverseGnomeSort extends Sort { - - /** - * @param arrayVisualizer - */ - public ReverseGnomeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - // TODO Auto-generated constructor stub - this.setSortListName("Reverse Gnome"); - this.setRunAllSortsName("Reverse Gnome Sort"); - this.setRunSortName("Reverse Gnomesort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - - } - private void reverseGnomeSort(int[] array, int lowerBound, int upperBound, double sleep) { - int pos = lowerBound; - - while(pos < upperBound && Reads.compareValues(array[pos], array[pos + 1]) == 1) { - Writes.swap(array, pos, pos + 1, sleep, true, false); - pos++; - } - } - public void customSort(int[] array, int low, int high, double sleep) { - for(int i = high; i >= low; i--) { - reverseGnomeSort(array, i, high, sleep); - } - } - - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - // TODO Auto-generated method stub - for(int i = sortLength - 1; i >= 0; i--) { - reverseGnomeSort(array, i, sortLength - 1, 0.05); - } - - } - -} diff --git a/src/sorts/exchange/ReverseGrateSort.java b/src/sorts/exchange/ReverseGrateSort.java deleted file mode 100644 index 761a73dc..00000000 --- a/src/sorts/exchange/ReverseGrateSort.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude_73 - * @author EilrahcF - * @implNote This sorting algorithm is bad for arrays, but good for linked - * lists. - * - */ -public final class ReverseGrateSort extends Sort { - - /** - * @param arrayVisualizer - */ - public ReverseGrateSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Reverse Grate"); - setRunAllSortsName("Reverse Grate Sort"); - setRunSortName("Reverse Gratesort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(512); - setBogoSort(false); - - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - boolean sorted = false; - while (!sorted) { - sorted = true; - for (int i = 0; i < currentLength - 1; i++) { - for (int j = i + 1; j < currentLength; j++) { - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - this.Delays.sleep(0.25D); - if (this.Reads.compareValues(array[i], array[j]) > 0) { - sorted = false; - this.Writes.swap(array, i, j, 0.1D, true, false); - break; - } - } - } - } - - } - -} diff --git a/src/sorts/exchange/SafeBogoSort.java b/src/sorts/exchange/SafeBogoSort.java deleted file mode 100644 index 0fe04e3a..00000000 --- a/src/sorts/exchange/SafeBogoSort.java +++ /dev/null @@ -1,66 +0,0 @@ -package sorts.exchange; - -import java.util.Random; - -import sorts.templates.Sort; -import main.ArrayVisualizer; - -/* - * -MIT License - -Copyright (c) 2021 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 SafeBogoSort extends Sort { - public SafeBogoSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Safe Bogo"); - this.setRunAllSortsName("Safe Bogo Sort"); - this.setRunSortName("Safe Bogosort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(false); - } - - private int findLastSorted(int[] array, int length) { - int i = 1; - for(; i < length && Reads.compareValues(array[i-1], array[i]) <= 0; i++); - return i-1; - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - Random r = new Random(); - int p = this.findLastSorted(array, length); - - while(p < length-1) { - Writes.swap(array, p, p + r.nextInt(length-p), 0, true, false); - p = this.findLastSorted(array, length); - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/SplitCenterSort.java b/src/sorts/exchange/SplitCenterSort.java deleted file mode 100644 index f89a7630..00000000 --- a/src/sorts/exchange/SplitCenterSort.java +++ /dev/null @@ -1,46 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -*/ -final public class SplitCenterSort extends Sort { - public SplitCenterSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Split Center"); - this.setRunAllSortsName("Split Center Sort"); - this.setRunSortName("Split Center Sort"); - this.setCategory("Exchange 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 currentLength, int bucketCount) { - int way = 1; - int i = 1; - for (int r = 1; r < currentLength; r++) { - i = (int) Math.floor(currentLength / 2); - i -= way; - while (i <= currentLength && i >= 1) { - Highlights.markArray(1, i - 1); - Highlights.markArray(2, i); - Delays.sleep(0.005); - if (Reads.compareValues(array[i - 1], array[i]) > 0) { - Writes.swap(array, i - 1, i, 0.005, true, false); - } - i += way; - } - way *= -1; - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/StableQuickSortMiddlePivot.java b/src/sorts/exchange/StableQuickSortMiddlePivot.java deleted file mode 100644 index aafad1d0..00000000 --- a/src/sorts/exchange/StableQuickSortMiddlePivot.java +++ /dev/null @@ -1,116 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import utils.ArrayVList; - -/* - * -MIT License - -Copyright (c) 2017 Rodney Shaghoulian -Copyright (c) 2020 Josiah Glosson - -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 StableQuickSortMiddlePivot extends Sort { - private int length; - - public StableQuickSortMiddlePivot(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stable Quick (Middle Pivot)"); - this.setRunAllSortsName("Stable Quick Sort (Middle Pivot)"); - this.setRunSortName("Stable Quicksort (Middle Pivot)"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - // Author: Rodney Shaghoulian - // Github: github.com/RodneyShag - - // Author: Josiah Glosson - // Github: github.com/gaming32 - - private void copy(ArrayVList list, int [] array, int startIndex) { - for (int num : list) { - Writes.write(array, startIndex++, num, 0.25, false, false); - Highlights.markArray(1, startIndex); - } - } - - /* Partition/Quicksort "Stable Sort" version using O(n) space */ - private int stablePartition(int[] array, int start, int end) { - int pivot = start + (end - start) / 2; - int pivotValue = array[pivot]; - Highlights.markArray(3, start); - Highlights.markArray(4, pivot); - - ArrayVList leftList = Writes.createArrayList(this.length); - ArrayVList rightList = Writes.createArrayList(this.length); - - for (int i = start ; i <= end; i++) { - Highlights.markArray(1, i); - if (i == pivot) continue; - - if (Reads.compareValues(array[i], pivotValue) == -1) { - Writes.arrayListAdd(leftList, array[i], true, 0.25); - } - else { - Writes.arrayListAdd(rightList, array[i], true, 0.25); - } - } - - /* Recreate array */ - this.copy(leftList, array, start); - - int newPivotIndex = start + leftList.size(); - - Writes.write(array, newPivotIndex, pivotValue, 0.25, false, false); - Highlights.markArray(1, newPivotIndex); - - this.copy(rightList, array, newPivotIndex + 1); - - Writes.deleteArrayList(leftList); - Writes.deleteArrayList(rightList); - - return newPivotIndex; - } - - private void stableQuickSort(int [] array, int start, int end) { - if (start < end) { - int pivotIndex = this.stablePartition(array, start, end); - this.stableQuickSort(array, start, pivotIndex - 1); - this.stableQuickSort(array, pivotIndex + 1, end); - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.length = length; - this.stableQuickSort(array, 0, length - 1); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/StableStoogeSort.java b/src/sorts/exchange/StableStoogeSort.java deleted file mode 100644 index 1ba24e6d..00000000 --- a/src/sorts/exchange/StableStoogeSort.java +++ /dev/null @@ -1,39 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class StableStoogeSort extends Sort { - public StableStoogeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stable Stooge"); - this.setRunAllSortsName("Stable Stooge Sort"); - this.setRunSortName("Stable Stoogesort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(false); - } - - private void stableStooge(int[] array, int start, int end) { - if (end - start + 1 == 2) { - if (Reads.compareIndices(array, start, end, 0.0025, true) == 1) { - Writes.swap(array, start, end, 0.005, true, false); - } - } else if (end - start + 1 > 2) { - int third = (end - start + 1) / 3; - stableStooge(array, start, end - third); - stableStooge(array, start + third, end); - stableStooge(array, start, end - third); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - stableStooge(array, 0, currentLength - 1); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/StacklessQuickSort.java b/src/sorts/exchange/StacklessQuickSort.java deleted file mode 100644 index 2050ea61..00000000 --- a/src/sorts/exchange/StacklessQuickSort.java +++ /dev/null @@ -1,156 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2021 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 StacklessQuickSort extends Sort { - public StacklessQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stackless Quick"); - this.setRunAllSortsName("Stackless Quick Sort"); - this.setRunSortName("Stackless Quicksort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void medianOfThree(int[] array, int a, int b) { - int m = a+(b-1-a)/2; - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - Writes.swap(array, a, m, 1, true, false); - - if(Reads.compareIndices(array, m, b-1, 1, true) == 1) { - Writes.swap(array, m, b-1, 1, true, false); - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - return; - } - - Writes.swap(array, a, m, 1, true, false); - } - - private int partition(int[] array, int a, int b) { - int i = a, j = b; - - this.medianOfThree(array, a, b); - Highlights.markArray(3, a); - - do { - do { - i++; - Highlights.markArray(1, i); - Delays.sleep(0.5); - } - while(i < j && Reads.compareIndices(array, i, a, 0, false) < 0); - - do { - j--; - Highlights.markArray(2, j); - Delays.sleep(0.5); - } - while(j >= i && Reads.compareIndices(array, j, a, 0, false) >= 0); - - if(i < j) Writes.swap(array, i, j, 1, true, false); - else { - Writes.swap(array, a, j, 1, true, false); - Highlights.clearMark(3); - return j; - } - } - while(true); - } - - private int leftBinSearch(int[] array, int a, int b, int p) { - while(a < b) { - int m = a+(b-a)/2; - - if(Reads.compareIndices(array, p, m, 0.5, true) <= 0) - b = m; - else - a = m+1; - } - - return a; - } - - private void quickSort(int[] array, int a, int b) { - int max = array[a]; - - for(int i = a+1; i < b; i++) { - Highlights.markArray(1, i); - Delays.sleep(0.5); - - if(Reads.compareValues(array[i], max) > 0) max = array[i]; - } - for(int i = b-1; i >= 0; i--) { - Highlights.markArray(1, i); - Delays.sleep(0.5); - - if(Reads.compareValues(array[i], max) == 0) - Writes.swap(array, i, --b, 1, true, false); - } - - int b1 = b; - - do { - while(b1-a > 2) { - int p = this.partition(array, a, b1); - Writes.swap(array, p, b, 1, true, false); - - b1 = p; - } - - if(b1-a == 2 && Reads.compareIndices(array, a, a+1, 0.5, true) == 1) - Writes.swap(array, a, a+1, 1, true, false); - - a = b1+1; - if(a >= b) { - if(a-1 < b) Writes.swap(array, a-1, b, 1, true, false); - return; - } - - b1 = this.leftBinSearch(array, a, b, a-1); - Writes.swap(array, a-1, b, 1, true, false); - - while(a < b1 && Reads.compareIndices(array, a-1, a, 0.5, true) == 0) a++; - } - while(true); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.quickSort(array, 0, length); - } -} \ No newline at end of file diff --git a/src/sorts/exchange/StupidFireSort.java b/src/sorts/exchange/StupidFireSort.java deleted file mode 100644 index fafccf94..00000000 --- a/src/sorts/exchange/StupidFireSort.java +++ /dev/null @@ -1,118 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -Stupid Fire Sort is a variant of Fire Sort, in turn variant to Gnome Sort. - -Where Fire Sort increments the swap check value by the length of the list, -Stupid Fire Sort only changes it by 1. This tiny difference lands the total -number of needed swaps, variable S, to reverse a list of N count values at -an unimaginable S = 0.125N^4 - 0.25N^3 + 1.125N^2 - N. Supposing we use 128 -items, that's over a hundred times worse than regular Fire Sort, and four -thousand times worse than Gnome Sort! Now that's impractical! - -RE8gTk9UIFVTRSBUSElTIFNPUlQsIEVWRVIh - -*/ -final public class StupidFireSort extends Sort { - public StupidFireSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Stupid Fire"); - this.setRunAllSortsName("Stupid Fire Sort"); - this.setRunSortName("Stupid Firesort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(128); - this.setBogoSort(false); - } - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int i = 1; - int twistcheck = 0; - int twistwait = 0; - int twist = -1; - int testi = 1; - boolean testpass = false; - boolean testreverse = false; - boolean anyswaps = false; - while (!testpass) { - if (twistwait < 1) { - twistcheck++; - twistwait = twistcheck; - twist *= -1; - } else { - twistwait--; - } - anyswaps = false; - while (i + 1 <= currentLength && i >= 1 && !anyswaps) { - if (Reads.compareValues(array[i - 1], array[i]) * twist > 0) { - Writes.swap(array, i - 1, i, 0.001, true, false); - i -= twist; - anyswaps = true; - } else { - i += twist; - } - } - if (i < 1) { - i = currentLength - 1; - testi = 1; - testpass = true; - while (testi != currentLength && testpass) { - if (Reads.compareValues(array[testi - 1], array[testi]) <= 0) { - testi++; - } else { - testpass = false; - testi = 1; - testreverse = true; - while (testi != currentLength && testreverse) { - if (Reads.compareValues(array[testi - 1], array[testi]) >= 0) { - testi++; - } else { - testreverse = false; - } - } - } - } - if (testreverse) { - i = 1; - twistwait = 0; - } - } - if (i + 1 > currentLength) { - i = 1; - testi = 1; - testpass = true; - while (testi != currentLength && testpass) { - if (Reads.compareValues(array[testi - 1], array[testi]) <= 0) { - testi++; - } else { - testpass = false; - testi = 1; - testreverse = true; - while (testi != currentLength && testreverse) { - if (Reads.compareValues(array[testi - 1], array[testi]) >= 0) { - testi++; - } else { - testreverse = false; - } - } - } - } - if (testreverse) { - i = currentLength - 1; - twistwait = 0; - } - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/SwapMapSort.java b/src/sorts/exchange/SwapMapSort.java deleted file mode 100644 index f6806a95..00000000 --- a/src/sorts/exchange/SwapMapSort.java +++ /dev/null @@ -1,72 +0,0 @@ -package sorts.exchange; - -import java.util.ArrayList; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2019 w0rthy - -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 SwapMapSort extends Sort { - public SwapMapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Swap Map"); - this.setRunAllSortsName("Swap Map Sort"); - this.setRunSortName("Swap Map Sort"); - this.setCategory("Exchange 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 sortLength, int bucketCount) throws Exception { - ArrayList map = new ArrayList<>(); - - while (true) { - for (int i = 0; i < sortLength - 1; i++) { - if (Reads.compareIndices(array, i, i + 1, 0.001, true) == 1) { - Writes.arrayListAdd(map, i); - Writes.mockWrite(map.size(), map.size() - 1, i, 0); - } - } - - if (map.size() == 0) { - break; - } - - for (int i = 0; i < map.size(); i++) { - Writes.swap(array, map.get(i), map.get(i) + 1, 0.01, true, false); - } - Writes.arrayListClear(map); - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/TernarySlowSort.java b/src/sorts/exchange/TernarySlowSort.java deleted file mode 100644 index 2d2f3eee..00000000 --- a/src/sorts/exchange/TernarySlowSort.java +++ /dev/null @@ -1,62 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author fungamer2 - * - */ -public final class TernarySlowSort extends Sort { - - public TernarySlowSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Ternary Slow"); - setRunAllSortsName("Ternary Slow Sort"); - setRunSortName("Ternary Slowsort"); - setCategory("Impractical Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(512); - setBogoSort(false); - - } - - private void compareSwap(int[] array, int start, int end, int sleep) { - if (this.Reads.compareValues(array[start], array[end]) == 1) { - this.Writes.swap(array, start, end, sleep, true, false); - } - this.Highlights.markArray(1, start); - this.Highlights.markArray(2, end); - this.Delays.sleep(sleep); - } - - public void ternarySlowSort(int[] array, int start, int end) { - if (end - start + 1 == 2) { - compareSwap(array, start, end, 1); - } else if (end - start + 1 > 2) { - int third = (end - start + 1) / 3; - int mid1 = start + third; - int mid2 = start + 2 * third; - - ternarySlowSort(array, start, mid1 - 1); - ternarySlowSort(array, mid1, mid2 - 1); - ternarySlowSort(array, mid2 - 1, end); - - compareSwap(array, mid1 - 1, end, 1); - compareSwap(array, mid1 - 1, mid2 - 1, 1); - compareSwap(array, mid2 - 1, end, 1); - - ternarySlowSort(array, start, end - 1); - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - ternarySlowSort(array, 0, sortLength - 1); - - } - -} diff --git a/src/sorts/exchange/TriSearchGnomeSort.java b/src/sorts/exchange/TriSearchGnomeSort.java deleted file mode 100644 index 23c072f5..00000000 --- a/src/sorts/exchange/TriSearchGnomeSort.java +++ /dev/null @@ -1,77 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author thatsOven - for the TriSearch algorithm - * @author mingyue12 - for the rest of this sorting algorithm - * - */ -public final class TriSearchGnomeSort extends Sort { - - /** - * @param arrayVisualizer - */ - public TriSearchGnomeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("TriSearch Gnome"); - this.setRunAllSortsName("TriSearch Gnome Sort"); - this.setRunSortName("TriSearch Gnomesort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public int triSearch(int[] arr, int l, int h, int val, double sleep) { - int mid = l + ((h-l) / 2); - Highlights.markArray(0, l); - Highlights.markArray(1, h); - Highlights.markArray(2, mid); - Delays.sleep(sleep); - if (Reads.compareValues(val, arr[l]) < 0) { - return l; - } else { - if (Reads.compareValues(val, arr[h]) < 0) { - if (Reads.compareValues(val, arr[mid]) < 0) { - return this.triSearch(arr, l+1, mid-1, val, sleep); - } else { - return this.triSearch(arr, mid+1, h-1, val, sleep); - } - } else { - return h+1; - } - } - } - public void triGnomeSort(int[] array, int start, int end, double compSleep, double writeSleep) { - for (int i = start+1; i < end; i++) { - int num = array[i]; - int lo = start; - - lo = this.triSearch(array, start, i-1, num, compSleep); - Highlights.clearAllMarks(); - int j = i ; - while(j>lo) { - Writes.swap(array, j, j - 1, writeSleep, true, false); - j--; - } - Highlights.clearAllMarks(); - - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount){ - triGnomeSort(array, 0, length, 40, 1); - - } - -} diff --git a/src/sorts/exchange/WiggleSort.java b/src/sorts/exchange/WiggleSort.java deleted file mode 100644 index f0d374b8..00000000 --- a/src/sorts/exchange/WiggleSort.java +++ /dev/null @@ -1,83 +0,0 @@ -/** - * - */ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude_73 - * @author aphitorite - * @author EilrahcF - * - */ -public final class WiggleSort extends Sort { - - /** - * @param arrayVisualizer - */ - public WiggleSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Wiggle"); - setRunAllSortsName("Wiggle Sort"); - setRunSortName("Wigglesort"); - setCategory("Exchange Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - private void wiggleSort(int[] array, int arrLen, int start, int end) { - if (end - start < 2) - return; - int leftPoint = start; - int rightPoint = end; - - int midPoint = (leftPoint + rightPoint) / 2; - - boolean startLeft = true; - int j = midPoint; - - for (int i = leftPoint; i < midPoint; i++) { - for (int k = midPoint; k < end; k++) { - this.Highlights.markArray(1, i); - this.Highlights.markArray(2, j); - if (this.Reads.compareValues(array[i], array[j]) >= 0) { - this.Writes.swap(array, i, j, 1.0D, true, false); - } else { - this.Delays.sleep(0.025D); - } - - if (startLeft) { - j++; - } else { - j--; - } - - } - if (startLeft) { - j--; - startLeft = false; - } else { - - j++; - startLeft = true; - } - } - - wiggleSort(array, arrLen, start, midPoint); - wiggleSort(array, arrLen, midPoint, end); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) throws Exception { - wiggleSort(array, length, 0, length); - - } - -} diff --git a/src/sorts/exchange/XSort.java b/src/sorts/exchange/XSort.java deleted file mode 100644 index 706402a1..00000000 --- a/src/sorts/exchange/XSort.java +++ /dev/null @@ -1,66 +0,0 @@ -package sorts.exchange; -import main.ArrayVisualizer; -import sorts.templates.Sort; -/* - -PORTED TO ARRAYV BY PCBOYGAMES - ------------------------------- -- SORTING ALGORITHM MADHOUSE - ------------------------------- - -*/ -final public class XSort extends Sort { - public XSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("X Pattern"); - this.setRunAllSortsName("X Pattern Sort"); - this.setRunSortName("X Pattern Sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(1024); - this.setBogoSort(false); - } - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int gap = currentLength; - int sortbase = 1; - int i = 1; - int xleft = 1; - int xright = 1; - boolean anyswaps = false; - boolean testpass = false; - while (!testpass) { - anyswaps = false; - i = 1; - while ((i - 1) + gap <= currentLength) { - if (Reads.compareValues(array[i - 1], array[(i - 1) + gap]) > 0) { - Writes.swap(array, i - 1, (i - 1) + gap, 0.001, true, false); - anyswaps = true; - xleft = i + 1; - xright = i + gap - 1; - if (gap != 1) { - for (int r = 0; r < gap - 1; r++) { - if (Reads.compareValues(array[xleft - 1], array[xright - 1]) > 0) { - Writes.swap(array, xleft - 1, xright - 1, 0.001, true, false); - } - xleft++; - xright--; - } - } - } - i++; - } - if (gap == 1 && !anyswaps) { - testpass = true; - } else { - if (gap != 1 && !anyswaps) { - gap--; - } - } - } - } -} \ No newline at end of file diff --git a/src/sorts/exchange/ooPQuicksort.java b/src/sorts/exchange/ooPQuicksort.java deleted file mode 100644 index d4daa1aa..00000000 --- a/src/sorts/exchange/ooPQuicksort.java +++ /dev/null @@ -1,174 +0,0 @@ -package sorts.exchange; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class ooPQuicksort extends Sort { - - public ooPQuicksort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("OopsQuick"); - this.setRunAllSortsName("Out of Place Stable QuickSort (by Control)"); - this.setRunSortName("Out of Place Stable Quicksort"); - this.setCategory("Exchange Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - // Based on Ideas by Scandum and Control - private void ooPquickSort(int[] array, int[] swap, int start, int end, boolean forwards, boolean fromSwap) { - - int a = start; - int e = end; - int c = start + (end - start + 1) / 2; - int b = start + (c - start + 1) / 2; - int d = c + (end - c + 1) / 2; - int temp = 0; - - if (end - start > 10) { - if (array[a] > array[b]) { - temp = a; - a = b; - b = temp; - } - Highlights.markArray(1, a); - Highlights.markArray(2, b); - Delays.sleep(0.25); - if (array[c] > array[d]) { - temp = c; - c = d; - d = temp; - } - Highlights.markArray(1, c); - Highlights.markArray(2, d); - Delays.sleep(0.25); - if (array[b] > array[d]) { - temp = b; - b = d; - d = temp; - } - Highlights.markArray(1, b); - Highlights.markArray(2, d); - Delays.sleep(0.25); - if (array[a] > array[c]) { - temp = a; - a = c; - c = temp; - } - Highlights.markArray(1, a); - Highlights.markArray(2, c); - Delays.sleep(0.25); - if (array[c] > array[e]) { - temp = c; - c = e; - e = temp; - } - Highlights.markArray(1, c); - Highlights.markArray(2, e); - Delays.sleep(0.25); - if (array[b] > array[c]) { - temp = b; - b = c; - c = temp; - } - Highlights.markArray(1, b); - Highlights.markArray(2, c); - Delays.sleep(0.25); - } - - int pivotpos = array[c]; - Highlights.markArray(1, pivotpos); - - int pivot = array[pivotpos]; - - int leftswappointer = start; - Highlights.markArray(1, leftswappointer); - int rightswappointer = end; - Highlights.markArray(1, rightswappointer); - - Writes.visualClear(array, pivotpos); - - if (forwards) { - int pointer = start; - Highlights.markArray(1, pointer); - while ((leftswappointer < rightswappointer)) { - if (pointer == pivotpos) { - pointer++; - } - if (Reads.compareValues(array[pointer], pivot) < 0) { - Writes.write(swap, leftswappointer, array[pointer], 0.05, false, fromSwap); - leftswappointer++; - } else if (Reads.compareValues(array[pointer], pivot) > 0) { - Writes.write(swap, rightswappointer, array[pointer], 0.05, false, fromSwap); - rightswappointer--; - } else if (Reads.compareValues(pointer, pivotpos) < 0) { - Writes.write(swap, leftswappointer, array[pointer], 0.05, false, fromSwap); - leftswappointer++; - } else { - Writes.write(swap, rightswappointer, array[pointer], 0.05, false, fromSwap); - rightswappointer--; - } - Writes.visualClear(array, pointer); - pointer++; - Highlights.markArray(1, pointer); - Highlights.markArray(2, leftswappointer); - Highlights.markArray(3, rightswappointer); - Delays.sleep(0.25); - } - } else if (!forwards) { - int pointer = end; - Highlights.markArray(1, pointer); - while ((leftswappointer < rightswappointer)) { - if (pointer == pivotpos) { - pointer--; - } - if (Reads.compareValues(array[pointer], pivot) < 0) { - Writes.write(swap, leftswappointer, array[pointer], 0.05, false, fromSwap); - leftswappointer++; - } else if (Reads.compareValues(array[pointer], pivot) > 0) { - Writes.write(swap, rightswappointer, array[pointer], 0.05, false, fromSwap); - rightswappointer--; - } else if (Reads.compareValues(pointer, pivotpos) > 0) { - Writes.write(swap, leftswappointer, array[pointer], 0.05, false, fromSwap); - leftswappointer++; - } else { - Writes.write(swap, rightswappointer, array[pointer], 0.05, false, fromSwap); - rightswappointer--; - } - Writes.visualClear(array, pointer); - pointer--; - Highlights.markArray(1, pointer); - Highlights.markArray(2, leftswappointer); - Highlights.markArray(3, rightswappointer); - Delays.sleep(0.25); - } - } - - if (!fromSwap) { - Writes.write(array, rightswappointer, pivot, 0.05, true, false); - } else { - Writes.write(swap, rightswappointer, pivot, 0.05, true, false); - } - - // recursively calls itself - if (end - leftswappointer >= 1) { - ooPquickSort(swap, array, leftswappointer + 1, end, false, !fromSwap); - } - if (rightswappointer - start >= 1) { - ooPquickSort(swap, array, start, rightswappointer - 1, true, !fromSwap); - } - - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int[] swaparray = Writes.createExternalArray(currentLength); - this.ooPquickSort(array, swaparray, 0, currentLength - 1, true, false); - Writes.deleteExternalArray(swaparray); - } -} diff --git a/src/sorts/hybrid/BaseNMergeSort.java b/src/sorts/hybrid/BaseNMergeSort.java deleted file mode 100644 index a4abc867..00000000 --- a/src/sorts/hybrid/BaseNMergeSort.java +++ /dev/null @@ -1,220 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 BaseNMergeSort extends Sort { - int[] tmp; - - private InsertionSort insertSorter; - - public BaseNMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Base-N Merge"); - this.setRunAllSortsName("Base-N Merge Sort, 4 Bases"); - this.setRunSortName("Base-N Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(true); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - this.setQuestion("Enter the number of bases for this sort:", 4); - } - - private int[] copyStarts(int[] starts, int baseCount) { - int[] copy = new int[baseCount]; - Writes.changeAllocAmount(copy.length); - for (int i = 0; i < baseCount; i++) { - copy[i] = starts[i]; - Writes.write(copy, i, starts[i], 0, false, true); - } - return copy; - } - - private void highlightMerge(int[] starts, int[] copiedStarts, int end, int baseCount) { - for (int i = 0; i < baseCount; i++) { - int startEnd = i == baseCount - 1 ? end : starts[i + 1]; - if (copiedStarts[i] < startEnd) { - Highlights.markArray(i + 1, copiedStarts[i]); - } - else { - Highlights.clearMark(i + 1); - } - } - } - - private void merge(int[] array, int[] starts, int end, int baseCount) { - int[] copiedStarts = copyStarts(starts, baseCount); - - int length = end - starts[0]; - - for (int nxt = 0; nxt < length; nxt++) { - highlightMerge(starts, copiedStarts, end, baseCount); - int minValue = Integer.MAX_VALUE; - int minIndex = 0; - for (int i = 0; i < baseCount; i++) { - int startEnd = i == baseCount - 1 ? end : starts[i + 1]; - if (copiedStarts[i] >= startEnd) { - continue; - } - if (Reads.compareValues(array[copiedStarts[i]], minValue) == -1) { - minValue = array[copiedStarts[i]]; - minIndex = i; - } - } - Writes.write(tmp, nxt, minValue, 1, false, true); - Writes.write(copiedStarts, minIndex, copiedStarts[minIndex] + 1, 0, false, true); - } - Highlights.clearAllMarks(); - - for(int i = 0; i < length; i++){ - Writes.write(array, starts[0] + i, tmp[i], 1, true, false); - } - - Writes.changeAllocAmount(-copiedStarts.length); - } - - private void mergeRun(int[] array, int[] starts, int end, int baseCount) { - if (end - starts[0] < baseCount) { - insertSorter.customInsertSort(array, starts[0], end, 1, false); - Writes.deleteExternalArray(starts); - return; - } - - for (int i = 0; i < baseCount; i++) { - int[] subStarts = calculateStarts(starts[i], starts[1] - starts[0], baseCount); - int startEnd = i == baseCount - 1 ? end : starts[i + 1]; - mergeRun(array, subStarts, startEnd, baseCount); - } - - merge(array, starts, end, baseCount); - - Writes.changeAllocAmount(-starts.length); - } - - private int[] calculateStarts(int start, int length, int baseCount) { - int[] starts = new int[baseCount]; - Writes.changeAllocAmount(starts.length); - int size = length / baseCount; - int current = start; - - for (int i = 0; i < baseCount; i++) { - Writes.write(starts, i, current, 0, false, true); - current += size; - } - - return starts; - } - - private double logBase(int value, int base) { - double result; - Writes.startLap(); - result = Math.log(value) / Math.log(base); - Writes.stopLap(); - return result; - } - - // Copied from MergeSorting.java:52 - // Used for merging wrong powers - private void mergeBase2(int[] array, int start, int mid, int end) { - int length = end - start; - - int low = start; - int high = mid; - - for(int nxt = 0; nxt < length; nxt++){ - if(low >= mid && high >= end) break; - - Highlights.markArray(1, low); - Highlights.markArray(2, high); - - if(low < mid && high >= end){ - Highlights.clearMark(2); - Writes.write(tmp, nxt, array[low++], 1, false, true); - } - else if(low >= mid && high < end){ - Highlights.clearMark(1); - Writes.write(tmp, nxt, array[high++], 1, false, true); - } - else if(Reads.compareValues(array[low], array[high]) == -1){ - Writes.write(tmp, nxt, array[low++], 1, false, true); - } - else{ - Writes.write(tmp, nxt, array[high++], 1, false, true); - } - } - Highlights.clearMark(2); - - for(int i = 0; i < length; i++){ - Writes.write(array, start + i, tmp[i], 1, true, false); - } - } - - private void baseNMerge(int[] array, int length, int baseCount) { - boolean useBinary = false; - double logBaseCount = logBase(baseCount, 2); - if (Math.pow(2, logBaseCount) == Math.pow(2, (int)logBaseCount)) { - useBinary = true; - } - - int start = 0; - - int lengthBase = useBinary ? 2 : baseCount; - double logLength = logBase(length, lengthBase); - if (Math.pow(lengthBase, (int)logLength) < Math.pow(lengthBase, logLength)) { - start = (int)(length - Math.pow(lengthBase, (int)logLength)); - } - - int[] starts = calculateStarts(start, length - start, baseCount); - - mergeRun(array, starts, length, baseCount); - - if (start > 0) { - baseNMerge(array, start, baseCount); - mergeBase2(array, 0, start, length); - } - } - - @Override - public void runSort(int[] array, int length, int baseCount) throws Exception { - this.setRunAllSortsName("Base-N Merge Sort, " + baseCount + " Bases"); - - insertSorter = new InsertionSort(arrayVisualizer); - - tmp = Writes.createExternalArray(length); - - baseNMerge(array, length, baseCount); - - Writes.deleteExternalArray(tmp); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/BinaryPDMergeSort.java b/src/sorts/hybrid/BinaryPDMergeSort.java deleted file mode 100644 index 2022cd8d..00000000 --- a/src/sorts/hybrid/BinaryPDMergeSort.java +++ /dev/null @@ -1,93 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; - -/* - * -MIT License - -Copyright (c) 2021 Gaming32 - -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. - * - */ - -public class BinaryPDMergeSort extends OptimizedPDMergeSort { - public BinaryPDMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Binary Pattern-Defeating Merge"); - this.setRunAllSortsName("Binary Pattern-Defeating Merge Sort"); - this.setRunSortName("Binary Pattern-Defeating Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int monoboundLeft(int[] array, int start, int end, int value) { - int top, mid; - - top = end - start; - - while (top > 1) { - mid = top / 2; - - if (Reads.compareValueIndex(array, value, end - mid, 0.5, true) <= 0) { - end -= mid; - } - top -= mid; - } - - if (Reads.compareValueIndex(array, value, end - 1, 0.5, true) <= 0) { - return end - 1; - } - return end; - } - - private int monoboundRight(int[] array, int start, int end, int value) { - int top, mid; - - top = end - start; - - while (top > 1) { - mid = top / 2; - - if (Reads.compareIndexValue(array, start + mid, value, 0.5, true) <= 0) { - start += mid; - } - top -= mid; - } - - if (Reads.compareIndexValue(array, start, value, 0.5, true) <= 0) { - return start + 1; - } - return start; - } - - protected void merge(int[] array, int start, int mid, int end) { - start = monoboundRight(array, start, mid, array[mid]); - if (start == mid) return; - end = monoboundLeft(array, mid, end, array[mid - 1]); - super.merge(array, start, mid, end); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/BlockSelectionMergeSort.java b/src/sorts/hybrid/BlockSelectionMergeSort.java deleted file mode 100644 index 4e883c6c..00000000 --- a/src/sorts/hybrid/BlockSelectionMergeSort.java +++ /dev/null @@ -1,130 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 BlockSelectionMergeSort extends Sort { - private BinaryInsertionSort binaryInserter; - private ReverseLazyStableSort extraMerger; - - public BlockSelectionMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Block Selection Merge"); - this.setRunAllSortsName("Block Selection Merge Sort"); - this.setRunSortName("Block Selection Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void swapBlocks(int[] array, int left, int right, int length, double sleep) { - for (int i = 0; i < length; i++) { - Writes.swap(array, left + i, right + i, sleep, true, false); - } - } - - public void blockSelection(int[] array, int start, int end, int blockSize, double compSleep, double writeSleep) { - for (int i = start; i < end - blockSize; i += blockSize) { - int lowestindex = i; - - for (int j = i + blockSize; j < end; j += blockSize) { - Highlights.markArray(2, j); - Delays.sleep(compSleep); - - if (Reads.compareValues(array[j], array[lowestindex]) == -1){ - lowestindex = j; - Highlights.markArray(1, lowestindex); - Delays.sleep(compSleep); - } - } - if (lowestindex > i) - swapBlocks(array, i, lowestindex, blockSize, writeSleep); - Delays.sleep(0.5); - } - } - - private void merge(int[] array, int start, int end) { - int blockSize = (end - start) / 16; - while (blockSize >= 4) { - blockSelection(array, start, end, blockSize, 0.01, 0.5); - blockSize /= 8; - } - binaryInserter.customBinaryInsert(array, start, end, 0.333); - } - - private void mergeRun(int[] array, int start, int mid, int end, int minSize) { - if(start == mid) return; - - if (end - start == minSize) { - binaryInserter.customBinaryInsert(array, start, end, 0.333); - return; - } - - mergeRun(array, start, (mid+start)/2, mid, minSize); - mergeRun(array, mid, (mid+end)/2, end, minSize); - - merge(array, start, end); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - extraMerger = new ReverseLazyStableSort(arrayVisualizer); - - Writes.startLap(); - int minSize = (int)(Math.log(length) / Math.log(2)) / 3 + 2; - minSize = (int)Math.pow(2, minSize); - Writes.stopLap(); - if (length <= minSize) { - binaryInserter.customBinaryInsert(array, 0, length, 0.333); - return; - } - - Writes.startLap(); - int useLength = (int)Math.pow(2, Math.floor(Math.log(length) / Math.log(2))); - Writes.stopLap(); - - int start = length - useLength; - int end = length; - int mid = start + ((end - start) / 2); - - mergeRun(array, start, mid, end, minSize); - if (length > useLength) { - runSort(array, length - useLength, bucketCount); - extraMerger.merge(array, 0, length - useLength, end); - } - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/BubbleMergeSort.java b/src/sorts/hybrid/BubbleMergeSort.java deleted file mode 100644 index 6578ffa8..00000000 --- a/src/sorts/hybrid/BubbleMergeSort.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * - */ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import sorts.templates.TimSorting; - -/** - * @author mingyue12 - * - */ -public class BubbleMergeSort extends Sort { - - /* - * TimSort cannot be simply written off as an abstract class, as it creates an instance of itself - * in order to track its state. Plus, it contains both instance and static methods, requiring even - * more refactoring, which would be just doing unnecessary busy work. Instead of what we've done for - * the rest of the algorithms, we'll favor composition over inheritance here and pass "util" objects - * to it. - */ - private TimSorting timSortInstance; - - /** - * @param arrayVisualizer - */ - public BubbleMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Bubble Merge"); - this.setRunAllSortsName("Bubble Merge Sort"); - this.setRunSortName("Bubble Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void bubbleSort(int[] a, int start, int end) { - for(int i = end - 1; i > start; i--) { - for (int j = start; j sortLength) { - bubbleSort(array, i, sortLength); - } - this.timSortInstance = new TimSorting(array, sortLength, this.arrayVisualizer); - TimSorting.sort(this.timSortInstance, array, sortLength); - } - - } - -} diff --git a/src/sorts/hybrid/BubblescanQuickSort.java b/src/sorts/hybrid/BubblescanQuickSort.java deleted file mode 100644 index 0707ac78..00000000 --- a/src/sorts/hybrid/BubblescanQuickSort.java +++ /dev/null @@ -1,101 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.select.MaxHeapSort; -import sorts.templates.Sort; - -/** - * - * @author aphitorite - * @author thatsOven - */ -public final class BubblescanQuickSort extends Sort { - - public BubblescanQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - // TODO Auto-generated constructor stub - this.setSortListName("Bubblescan Quick"); - this.setRunAllSortsName("Bubblescan Quick Sort"); - this.setRunSortName("Bubblescan Quicksort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - private InsertionSort insertSorter; - private MaxHeapSort heapSorter; - - private int partition(int[] array, int a, int b, int val) { - int i = a, j = b - 1; - while (i <= j) { - while (this.Reads.compareValues(array[i], val) < 0) { - i++; - this.Highlights.markArray(1, i); - this.Delays.sleep(0.5D); - } - while (this.Reads.compareValues(array[j], val) > 0) { - j--; - this.Highlights.markArray(2, j); - this.Delays.sleep(0.5D); - } - - if (i <= j) this.Writes.swap(array, i++, j--, 1.0D, true, false); - - } - return i; - } - - private void sort(int[] array, int a, int b, int depthLimit) { - int end = b, length = b - a; - - while (length > 16) { - if (depthLimit == 0) { - this.heapSorter.customHeapSort(array, a, end, 1.0D); - - return; - } - double sum = 0.0D; - boolean swapped = false; - - for (int i = a + 1; i < end; i++) { - this.Highlights.markArray(1, i - 1); - this.Highlights.markArray(2, i); - this.Delays.sleep(0.25D); - - if (this.Reads.compareValues(array[i - 1], array[i]) == 1) { - this.Writes.swap(array, i - 1, i, 0.5D, false, false); - swapped = true; - } - - sum += array[i - 1]; - } - - if (!swapped) - return; - int p = partition(array, a, end - 1, (int)(sum / (length - 1))); - depthLimit--; - sort(array, p, end - 1, depthLimit); - - end = p; - length = end - a; - } - - this.Highlights.clearMark(2); - this.insertSorter.customInsertSort(array, a, end, 0.5D, false); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - // TODO Auto-generated method stub - this.insertSorter = new InsertionSort(this.arrayVisualizer); - this.heapSorter = new MaxHeapSort(this.arrayVisualizer); - sort(array, 0, sortLength, 2 * (int)(Math.log(sortLength) / Math.log(2.0D))); - - - } - -} diff --git a/src/sorts/hybrid/BufferedBlockSelectionMergeSort.java b/src/sorts/hybrid/BufferedBlockSelectionMergeSort.java deleted file mode 100644 index ad465c61..00000000 --- a/src/sorts/hybrid/BufferedBlockSelectionMergeSort.java +++ /dev/null @@ -1,129 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import sorts.insert.BinaryInsertionSort; -import java.util.Arrays; - -/* - * -MIT License - -Copyright (c) 2021 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 BufferedBlockSelectionMergeSort extends Sort { - public BufferedBlockSelectionMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Buffered Block Selection Merge"); - this.setRunAllSortsName("Buffered Block Selection Merge Sort"); - this.setRunSortName("Buffered Block Selection Merge Sort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int sqrt(int n) { - return (int)Math.sqrt(n-1)+1; - } - - private void multiSwap(int[] array, int a, int b, int len) { - for(int i = 0; i < len; i++) - Writes.swap(array, a+i, b+i, 1, true, false); - } - - private boolean blockLesser(int[] array, int a, int b, int bLen) { - int cmp = Reads.compareValues(array[a], array[b]); - return cmp < 0 || (cmp == 0 && Reads.compareValues(array[a+bLen-1], array[b+bLen-1]) < 0); - } - private void mergeAboveBW(int[] array, int a1, int b1, int a, int b) { - int p = (b1--)+(b--)-a; - - while(b >= a && b1 >= a1) { - if(Reads.compareValues(array[b], array[b1]) >= 0) - Writes.swap(array, --p, b--, 1, true, false); - else - Writes.swap(array, --p, b1--, 1, true, false); - } - while(b >= a) Writes.swap(array, --p, b--, 1, true, false); - } - private void mergeBW(int[] array, int a, int m, int b, int p) { - int pLen = b-m; - this.multiSwap(array, m, p, pLen); - - int i = pLen-1, j = m-1, k = b-1; - - while(i >= 0 && j >= a) { - if(Reads.compareValues(array[p+i], array[j]) >= 0) - Writes.swap(array, k--, p+(i--), 1, true, false); - else - Writes.swap(array, k--, j--, 1, true, false); - } - while(i >= 0) Writes.swap(array, k--, p+(i--), 1, true, false); - } - private void inPlaceMergeUnstable(int[] array, int a, int m, int b) { - int bLen = this.sqrt(b-a), a1 = a+(m-a-1)%bLen+1, b1 = b-(b-m)%bLen; - - this.multiSwap(array, a1, this.blockLesser(array, m-bLen, b1-bLen, bLen) ? b1-bLen : m-bLen, bLen); - - for(a1 += bLen; a1 < b1; a1 += bLen) { - int min = a1; - - for(int i = a1+bLen; i < b1; i += bLen) - if(this.blockLesser(array, i, min, bLen)) min = i; - - if(min > a1) this.multiSwap(array, min, a1, bLen); - this.mergeAboveBW(array, a, a1-bLen, a1, a1+bLen); - } - this.mergeAboveBW(array, a, a1-bLen, a1, b); - - BinaryInsertionSort smallSort = new BinaryInsertionSort(this.arrayVisualizer); - smallSort.customBinaryInsert(array, b-bLen, b, 0.25); - - this.mergeBW(array, a+bLen, b-bLen, b, a); - smallSort.customBinaryInsert(array, a, a+bLen, 0.25); - } - - private void mergeSort(int[] array, int a, int b) { - if(b-a < 32) { - BinaryInsertionSort smallSort = new BinaryInsertionSort(this.arrayVisualizer); - smallSort.customBinaryInsert(array, a, b, 0.25); - - return; - } - int m = (a+b)/2; - - this.mergeSort(array, a, m); - this.mergeSort(array, m, b); - this.inPlaceMergeUnstable(array, a, m, b); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.mergeSort(array, 0, length); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/BufferedMergeSort.java b/src/sorts/hybrid/BufferedMergeSort.java deleted file mode 100644 index 10a62c99..00000000 --- a/src/sorts/hybrid/BufferedMergeSort.java +++ /dev/null @@ -1,149 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 BufferedMergeSort extends Sort { - private BinaryInsertionSort binaryInserter; - private ReverseLazyStableSort finalMerger; - private BlockSelectionMergeSort blockSelector; - - public BufferedMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Buffered Merge"); - this.setRunAllSortsName("Buffered Merge Sort"); - this.setRunSortName("Buffered Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private static double log2(double value) { - return Math.log(value) / Math.log(2); - } - - private static int getBufferSize(int length) { - return (int)Math.pow(2, Math.ceil(log2((int)log2(length)))) * 2; - } - - private void mergeUnderBuffer(int[] array, int bufferSize, int start, int mid, int end) { - int bufferPointer = 0; - int left = start, right = mid; - - while (left < mid && right < end) { - if (Reads.compareIndices(array, left, right, 0, true) <= 0) { - Writes.swap(array, bufferPointer, left, 0.25, true, false); - left++; - } - else { - Writes.swap(array, bufferPointer, right, 0.25, true, false); - right++; - } - bufferPointer++; - } - - while (left < mid) { - Writes.swap(array, bufferPointer, left, 0.25, true, false); - left++; - bufferPointer++; - } - while (right < end) { - Writes.swap(array, bufferPointer, right, 0.25, true, false); - right++; - bufferPointer++; - } - - for (int i = 0; i < end - start; i++) { - Writes.swap(array, i, start + i, 0.5, true, false); - } - } - - private void mergeOverBuffer(int[] array, int bufferSize, int start, int mid, int end) { - int blockSize = bufferSize / 2; - blockSelector.blockSelection(array, start, end, blockSize, 0.025, 1); - - int checkStart = start; - while (checkStart < end - blockSize) { - if (Reads.compareIndices(array, checkStart + blockSize - 1, checkStart + blockSize, 1, true) == 1) { - mergeUnderBuffer(array, bufferSize, checkStart, checkStart + blockSize, checkStart + bufferSize); - } - checkStart += blockSize; - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - finalMerger = new ReverseLazyStableSort(arrayVisualizer); - blockSelector = new BlockSelectionMergeSort(arrayVisualizer); - - int bufferSize = BufferedMergeSort.getBufferSize(sortLength); - int length = sortLength - ((sortLength - bufferSize) % (bufferSize / 2)); - if (bufferSize * 2 >= length) { - binaryInserter.customBinaryInsert(array, 0, sortLength, 0.333); - return; - } - - for (int i = bufferSize; i < length - 1; i += 2) { - if (Reads.compareIndices(array, i, i + 1, 0.5, true) == 1) { - Writes.swap(array, i, i + 1, 0.5, true, false); - } - } - - for (int gap = 4; gap <= bufferSize; gap *= 2) { - for (int i = bufferSize; i + gap <= length; i += gap) { - mergeUnderBuffer(array, bufferSize, i, i + gap / 2, i + gap); - } - } - - for (int gap = bufferSize * 2; gap / 2 <= length; gap *= 2) { - int i; - for (i = bufferSize; i + gap <= length; i += gap) { - mergeOverBuffer(array, bufferSize, i, i + gap / 2, i + gap); - } - if (i + gap > length) { - mergeOverBuffer(array, bufferSize, i, i + gap / 2, length); - } - } - - runSort(array, bufferSize, bucketCount); - finalMerger.merge(array, 0, bufferSize, length); - if (sortLength - length > 0) { - binaryInserter.customBinaryInsert(array, length, sortLength, 0.333); - finalMerger.merge(array, 0, length, sortLength); - } - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/FifthMergeSort.java b/src/sorts/hybrid/FifthMergeSort.java index 4a1d6262..15fa8ace 100644 --- a/src/sorts/hybrid/FifthMergeSort.java +++ b/src/sorts/hybrid/FifthMergeSort.java @@ -1,7 +1,7 @@ package sorts.hybrid; import main.ArrayVisualizer; -import sorts.insert.PatternDefeatingInsertionSort; +import sorts.insert.BinaryInsertionSort; import sorts.templates.Sort; /* @@ -38,11 +38,11 @@ public IndexPair(int aEnd, int bEnd) { } } - PatternDefeatingInsertionSort inserter; + BinaryInsertionSort inserter; public FifthMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Fifth Merge"); this.setRunAllSortsName("Fifth Merge Sort"); this.setRunSortName("Fifth Mergesort"); @@ -108,7 +108,7 @@ protected void mergeForwardsWithBuffer(int[] array, int[] buffer, int dest, int Delays.sleep(0.5); if (Reads.compareValueIndex(array, buffer[left], right, 0, false) <= 0) { Writes.write(array, dest++, buffer[left++], 0.5, true, false); - } else { + } else { Writes.write(array, dest++, array[right++], 0.5, true, false); } } @@ -158,10 +158,10 @@ protected void merge(int[] array, int[] buffer, int chunkOffset, int start, int protected void pingPong(int[] array, int[] buffer, int start, int end) { int i; for (i = start; i + 8 < end; i += 8) { - inserter.insertionSort(array, i, i + 8, 0.5, false); + inserter.customBinaryInsert(array, i, i + 8, 0.5); } if (end - i > 1) { - inserter.insertionSort(array, i, end, 0.5, false); + inserter.customBinaryInsert(array, i, end, 0.5); } int length = end - start; @@ -186,9 +186,9 @@ protected void pingPong(int[] array, int[] buffer, int start, int end) { Writes.arraycopy(buffer, 0, array, start, length, 0.5, true, false); } } - + public void fifthMergeSort(int[] array, int currentLength) { - inserter = new PatternDefeatingInsertionSort(arrayVisualizer); + inserter = new BinaryInsertionSort(arrayVisualizer); int fifthLen = currentLength / 5; int bufferLen = currentLength - fifthLen * 4; @@ -214,7 +214,7 @@ public void fifthMergeSort(int[] array, int currentLength) { Writes.deleteExternalArray(buffer); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { fifthMergeSort(array, currentLength); diff --git a/src/sorts/hybrid/ImprovedWeaveMergeSort.java b/src/sorts/hybrid/ImprovedWeaveMergeSort.java deleted file mode 100644 index 670e8c7c..00000000 --- a/src/sorts/hybrid/ImprovedWeaveMergeSort.java +++ /dev/null @@ -1,88 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import sorts.insert.InsertionSort; - -/* - * -MIT License - -Copyright (c) 2019 w0rthy - -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 ImprovedWeaveMergeSort extends Sort { - public ImprovedWeaveMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Improved Weave Merge"); - this.setRunAllSortsName("Improved Weave Merge Sort"); - this.setRunSortName("Improved Weave Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private InsertionSort sort; - - public static int getMaxBit(int n) { - int i; - for(i = 0; (1 << i) <= n; i++); - return i - 1; - } - - //will fail for b-a < 2 - private void weaveMerge(int[] array, int a, int b) { - int n = b-a, m = (n+1)/2; - - for(int j = 1<<(getMaxBit(n-1)-1); j >= 1; j >>= 1) { - int s = m > j ? 1 : 0; - for(int i = a+m+(1-s)*(j<<1); i+j <= b; i += j<<2) - for(int k = 0; k < j; k++) - Writes.swap(array, i-j+k, i+k, 1, true, false); - m -= s*j; - } - - Highlights.clearMark(2); - this.sort.customInsertSort(array, a, b, 0.2, false); - } - - private void weaveMergeSort(int[] array, int a, int b) { - if(b-a > 2) { - int m = a+(b-a+1)/2; - this.weaveMergeSort(array, a, m); - if(b-a > 3) - this.weaveMergeSort(array, m, b); - } - this.weaveMerge(array, a, b); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.sort = new InsertionSort(this.arrayVisualizer); - this.weaveMergeSort(array, 0, currentLength); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/IntroPseudoHeapSort.java b/src/sorts/hybrid/IntroPseudoHeapSort.java deleted file mode 100644 index e275cd88..00000000 --- a/src/sorts/hybrid/IntroPseudoHeapSort.java +++ /dev/null @@ -1,76 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.templates.Sort; - -/** - * @author mingyue12 - * - */ -public final class IntroPseudoHeapSort extends Sort { - - - public IntroPseudoHeapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Iterative Intro Pseudo-Heap"); - setRunAllSortsName("Iterative Introspective Pseudo-Heap Sort"); - setRunSortName("Iterative Introspective Pseudo-Heapsort"); - setCategory("Hybrid Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - setBogoSort(false); - - } - - private boolean sift_down(int[] array, int start, int length, int root) { - boolean swapped = false; - int j = root; - while (2 * j < length) { - int k = 2 * j; - if (k < length && this.Reads.compareValues(array[start + k - 1], array[start + k]) == 1) { - k++; - } - if (this.Reads.compareIndices(array, start + j - 1, start + k - 1, 1.0D, true) == 1) { - this.Writes.swap(array, start + j - 1, start + k - 1, 1.0D, true, false); - j = k; - swapped = true; - continue; - } - break; - } - return swapped; - } - - private boolean sift(int[] array, int start, int end) { - return sift_down(array, start, end - start + 1, 1); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int threshold = 0, n = 1; - for(; n < length; n*=2, threshold++); - - threshold /= 2; - int iterations = 0; - boolean swapped = true; - while (swapped) { - swapped = false; - iterations++; - if(iterations >= threshold) { - InsertionSort ins = new InsertionSort(arrayVisualizer); - ins.customInsertSort(array, 0, length, 0.5, false); - break; - } - for (int i = length - 2; i >= 0; i--) { - if (sift(array, i, length)) - swapped = true; - } - } - - } - -} diff --git a/src/sorts/hybrid/LAQuickSort.java b/src/sorts/hybrid/LAQuickSort.java deleted file mode 100644 index 3cfbd564..00000000 --- a/src/sorts/hybrid/LAQuickSort.java +++ /dev/null @@ -1,182 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.UnstableInsertionSort; -import sorts.select.PoplarHeapSort; -import sorts.templates.Sort; - -/* -Logarithmic Average QuickSort 2020 Copyright (C) thatsOven -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -final public class LAQuickSort extends Sort { - PoplarHeapSort heapSorter; - UnstableInsertionSort insertSorter; - - public LAQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Logarithmic Average Quick"); - this.setRunAllSortsName("Logarithmic Average QuickSort"); - this.setRunSortName("Logarithmic Average QuickSort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public int partition(int[] array, int a, int b, int p) { - int i = a - 1; - int j = b; - Highlights.markArray(3, p); - - while(true) { - i++; - while(i < b && Reads.compareIndices(array, i, p, 0, false) == -1) { - Highlights.markArray(1, i); - Delays.sleep(0.25); - i++; - } - j--; - while(j >= a && Reads.compareIndices(array, j, p, 0, false) == 1) { - Highlights.markArray(2, j); - Delays.sleep(0.25); - j--; - } - if(i < j) Writes.swap(array, i, j, 1, true, false); - else return j; - } - } - - public int ghostPartition(int[] array, int a, int b, int val) { - int i = a; - int j = b - 1; - while (i <= j) { - while (Reads.compareValues(array[i], val) < 0) { - i++; - } - while (Reads.compareValues(array[j], val) > 0) { - j--; - } - if (i <= j) { - Writes.swap(array, i++, j--, 1, true, false); - } - } - Highlights.clearAllMarks(); - return i; - } - - public int log2(int N) { - int result = (int)(Math.log(N) / Math.log(2)); - return result; - } - - private void medianOfThree(int[] array, int a, int b) { - int m = a+(b-1-a)/2; - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - Writes.swap(array, a, m, 1, true, false); - - if(Reads.compareIndices(array, m, b-1, 1, true) == 1) { - Writes.swap(array, m, b-1, 1, true, false); - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - return; - } - - Writes.swap(array, a, m, 1, true, false); - } - - public int logarithmicAverage(int[] arr, int low, int high) { - int sum = 0; - int counter = 0; - int qta = this.log2(high-low); - if (2 > qta) { - qta = 2; - } - for (int i = low; i < high; i += ((high-low) / qta)) { - Highlights.markArray(0, i); - Delays.sleep(1); - sum += arr[i]; - counter++; - } - Highlights.clearAllMarks(); - sum = sum / counter; - return sum; - } - - public boolean getSortedRuns(int[] array, int start, int end) { - Highlights.clearAllMarks(); - boolean reverseSorted = true; - boolean sorted = true; - int comp; - - for (int i = start; i < end-1; i++) { - comp = Reads.compareIndices(array, i, i+1, 0.5, true); - if (comp > 0) sorted = false; - else reverseSorted = false; - if ((!reverseSorted) && (!sorted)) return false; - } - - if (reverseSorted && !sorted) { - Writes.reversal(array, start, end-1, 1, true, false); - sorted = true; - } - - return sorted; - } - - public void quickSort(int[] arr, int low, int high, int depthLimit, int backPivot, boolean logAvg, int equalPivotCount) { - if (this.getSortedRuns(arr, low, high)) return; - if (high-low > 16) { - int pi = low, pivot = low; - if (!logAvg) { - this.medianOfThree(arr, low, high); - pi = this.partition(arr, low, high, low); - int left = pi-low; - int right = high-(pi+1); - if ((left == 0 || right == 0) || (left/right >= 16 || right/left >= 16)) logAvg = true; - else { - Writes.swap(arr, low, pi, 1, true, false); - pivot = arr[pi]; - } - } - if (logAvg) { - pivot = this.logarithmicAverage(arr, low, high); - pi = this.ghostPartition(arr, low, high, pivot); - } - if (backPivot == pivot) equalPivotCount++; - if (depthLimit == 0 || equalPivotCount > 4){ - if (equalPivotCount > 4) equalPivotCount = 0; - heapSorter.heapSort(arr, low, high); - return; - } - depthLimit--; - this.quickSort(arr, low, pi, depthLimit, pivot, logAvg, equalPivotCount); - this.quickSort(arr, pi+(logAvg ? 0 : 1), high, depthLimit, pivot, logAvg, equalPivotCount); - } else { - insertSorter.unstableInsertionSort(arr, low, high); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - heapSorter = new PoplarHeapSort(arrayVisualizer); - insertSorter = new UnstableInsertionSort(arrayVisualizer); - - this.quickSort(array, 0, currentLength, 2*log2(currentLength), array[1], false, 0); - } -} diff --git a/src/sorts/hybrid/LazierSort.java b/src/sorts/hybrid/LazierSort.java deleted file mode 100644 index fd26e3d5..00000000 --- a/src/sorts/hybrid/LazierSort.java +++ /dev/null @@ -1,380 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.select.MaxHeapSort; -import sorts.templates.Sort; - -/* - * -MIT License - -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 LazierSort extends Sort { - public LazierSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Lazier Stable"); - this.setRunAllSortsName("Lazier Stable Sort"); - this.setRunSortName("Lazier Sort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private static int cbrt(int n) { - int r = 1; - for(; r*r*r < n; r++); - return r; - } - - private void shift(int[] array, int a, int m, int b) { - while(m < b) Writes.swap(array, a++, m++, 1, true, false); - } - - private void shiftBW(int[] array, int a, int m, int b) { - while(m > a) Writes.swap(array, --b, --m, 1, true, false); - } - - private void multiSwap(int[] array, int a, int b, int len) { - for(int i = 0; i < len; i++) - Writes.swap(array, a+i, b+i, 1, true, false); - } - - private void rotate(int[] array, int a, int m, int b) { - int l = m-a, r = b-m; - - while(l > 0 && r > 0) { - if(r < l) { - this.multiSwap(array, m-r, m, r); - b -= r; - m -= r; - l -= r; - } - else { - this.multiSwap(array, a, m, l); - a += l; - m += l; - r -= l; - } - } - } - - private void insertTo(int[] array, int a, int b) { - Highlights.clearMark(2); - int temp = array[a]; - while(a > b) Writes.write(array, a, array[(a--)-1], 0.5, true, false); - Writes.write(array, b, temp, 0.5, true, false); - } - - private int leftBinSearch(int[] array, int a, int b, int val) { - while(a < b) { - int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) - b = m; - else - a = m+1; - } - - return a; - } - - private int rightBinSearch(int[] array, int a, int b, int val) { - while(a < b) { - int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) - b = m; - else - a = m+1; - } - - return a; - } - - private int leftExpSearch(int[] array, int a, int b, int val) { - int i = 1; - while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) > 0) i *= 2; - - return this.leftBinSearch(array, a+i/2, Math.min(b, a-1+i), val); - } - - private int findKeys(int[] array, int a, int b, int n) { - int nKeys = 1, p = a, pEnd = a+nKeys; - - Highlights.clearMark(2); - for(int i = pEnd; i < b && nKeys < n; i++) { - Highlights.markArray(1, i); - Delays.sleep(1); - int loc = this.leftBinSearch(array, p, pEnd, array[i]); - - if(pEnd == loc || Reads.compareValues(array[i], array[loc]) != 0) { - this.rotate(array, p, pEnd, i); - int inc = i-pEnd; - loc += inc; - p += inc; - pEnd += inc; - - this.insertTo(array, pEnd, loc); - nKeys++; - pEnd++; - } - } - this.rotate(array, a, p, pEnd); - return nKeys; - } - - private void binaryInsertion(int[] array, int a, int b) { - for(int i = a+1; i < b; i++) - this.insertTo(array, i, this.rightBinSearch(array, a, i, array[i])); - } - - public void inPlaceMerge(int[] array, int a, int m, int b) { - int i = a, j = m, k; - - while(i < j && j < b) { - if(Reads.compareValues(array[i], array[j]) > 0) { - k = this.leftExpSearch(array, j+1, b, array[i]); - this.rotate(array, i, j, k); - - i += k-j; - j = k; - } - else i++; - } - } - - private void lazierBlockMerge(int[] array, int a, int m, int b, int bLen) { - while(m-bLen >= a) { - this.inPlaceMerge(array, m-bLen, m, b); - m -= bLen; - } - this.inPlaceMerge(array, a, m, b); - } - - private void inPlaceMergeSort(int[] array, int a, int b) { - int len = b-a, i, j; - - for(j = len; j >= 32; j = (j+1)/2); - - for(i = a; i+j <= b; i += j) - this.binaryInsertion(array, i, i+j); - this.binaryInsertion(array, i, b); - - for(; j < len; j *= 2) { - for(i = a; i + 2*j <= b; i += 2*j) - this.inPlaceMerge(array, i, i+j, i+2*j); - - if(i + j < b) - this.inPlaceMerge(array, i, i+j, b); - } - } - - private void lazierBlockMergeSort(int[] array, int a, int b, int keys) { - int a1 = a+keys, b1 = b, - len = b-a1, i, j; - - for(j = len; j >= 32; j = (j+1)/2); - - //insertion - for(i = a1; i+j <= b; i += j) - this.binaryInsertion(array, i, i+j); - this.binaryInsertion(array, i, b); - - //build blocks - if(keys >= 2*j) { - for(i = a1; i+2*j <= b; i += 2*j) - this.mergeTo(array, i, i+j, i+2*j, i-2*j); - - if(i+j < b) this.mergeTo(array, i, i+j, b, i-2*j); - else this.shift(array, i-2*j, i, b); - - j *= 2; - a1 -= j; - b1 -= j; - - while(a1-j >= a) { - for(i = a1; i+2*j <= b1; i += 2*j) - this.mergeTo(array, i, i+j, i+2*j, i-j); - - if(i+j < b1) this.mergeTo(array, i, i+j, b1, i-j); - else this.shift(array, i-j, i, b1); - - a1 -= j; - b1 -= j; - j *= 2; - } - - for(i = a1; i+2*j <= b1; i += 2*j); - if(i+j < b1) this.mergeToBW(array, i, i+j, b1, b); - else this.shiftBW(array, i, b1, b); - - for(; i > a1; i -= 2*j) this.mergeToBW(array, i-2*j, i-j, i, i+j); - - a1 += j; - j *= 2; - } - - int c = 1; - for(; j < len; j *= 2) { - while((c*c*c)/(2*j) < (2*j)) c++; - - for(i = a1; i + 2*j <= b; i += 2*j) - this.lazierBlockMerge(array, i, i+j, i+2*j, c); - - if(i + j < b) - this.lazierBlockMerge(array, i, i+j, b, c); - } - - //redist buffer - MaxHeapSort heapSort = new MaxHeapSort(this.arrayVisualizer); - heapSort.customHeapSort(array, a, a1, 1); - this.inPlaceMerge(array, a, a1, b); - } - - private void mergeTo(int[] array, int a, int m, int b, int p) { - int i = a, j = m; - - while(i < m && j < b) { - if(Reads.compareValues(array[i], array[j]) <= 0) - Writes.swap(array, p++, i++, 1, true, false); - - else Writes.swap(array, p++, j++, 1, true, false); - } - if(p < i) while(i < m) Writes.swap(array, p++, i++, 1, true, false); - while(j < b) Writes.swap(array, p++, j++, 1, true, false); - } - - private void mergeToBW(int[] array, int a, int m, int b, int p) { - int i = m-1, j = b-1; p--; - - while(i >= a && j >= m) { - if(Reads.compareValues(array[i], array[j]) > 0) - Writes.swap(array, p--, i--, 1, true, false); - - else Writes.swap(array, p--, j--, 1, true, false); - } - if(p > j) while(j >= m) Writes.swap(array, p--, j--, 1, true, false); - while(i >= a) Writes.swap(array, p--, i--, 1, true, false); - } - - private void mergeFW(int[] array, int a, int p, int m, int b) { - int i = a, j = m, k = p; - - while(j < b) { - while(i < p && Reads.compareValues(array[i], array[j]) <= 0) - Writes.swap(array, k++, i++, 1, true, false); - if(i == p) return; - - int n = this.leftExpSearch(array, j+1, b, array[i]); - while(j < n) Writes.swap(array, k++, j++, 1, true, false); - } - while(i < p) Writes.swap(array, k++, i++, 1, true, false); - } - - protected void lazierSort(int[] array, int a, int b) { - int len = b-a; - - //build blocks dies when length is too small (< 65) - if(len <= 64) { - this.binaryInsertion(array, a, b); - return; - } - - int bLen, mRun; - for(bLen = 1; (bLen*bLen*bLen)/len < len; bLen *= 2); - for(mRun = 1; (mRun*mRun*mRun)/len < len; mRun++); - - mRun = (16*mRun) / bLen; - bLen = (bLen*mRun) / 16; - - int keys = this.findKeys(array, a, b, bLen); - - if(keys < bLen) { - if(keys == 1) return; - else if(keys <= cbrt(len)) this.inPlaceMergeSort(array, a, b); - else this.lazierBlockMergeSort(array, a, b, keys); - return; - } - - int a1 = a+bLen, b1 = b, i, j = mRun; - - //insertion - for(i = a1; i+j <= b; i += j) - this.binaryInsertion(array, i, i+j); - this.binaryInsertion(array, i, b); - - //build blocks - for(i = a1; i+2*j <= b; i += 2*j) - this.mergeTo(array, i, i+j, i+2*j, i-2*j); - - if(i+j < b) this.mergeTo(array, i, i+j, b, i-2*j); - else this.shift(array, i-2*j, i, b); - - j *= 2; - a1 -= j; - b1 -= j; - - while(a1 > a) { - for(i = a1; i+2*j <= b1; i += 2*j) - this.mergeTo(array, i, i+j, i+2*j, i-j); - - if(i+j < b1) this.mergeTo(array, i, i+j, b1, i-j); - else this.shift(array, i-j, i, b1); - - a1 -= j; - b1 -= j; - j *= 2; - } - - //do that merge thing - for(i = a; i+bLen < b1; i += bLen); - this.shiftBW(array, i, b1, b); - - b1 = i+bLen; - i -= bLen; - - while(i >= a) { - this.mergeFW(array, i, i+bLen, b1, b); - - i -= bLen; - b1 -= bLen; - } - - //redist buffer - a1 = a+bLen; - MaxHeapSort heapSort = new MaxHeapSort(this.arrayVisualizer); - heapSort.customHeapSort(array, a, a1, 1); - this.inPlaceMerge(array, a, a1, b); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.lazierSort(array, 0, currentLength); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/MedianOfSixteenAdaptiveQuickSort.java b/src/sorts/hybrid/MedianOfSixteenAdaptiveQuickSort.java deleted file mode 100644 index f491dda9..00000000 --- a/src/sorts/hybrid/MedianOfSixteenAdaptiveQuickSort.java +++ /dev/null @@ -1,217 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.UnstableInsertionSort; -import sorts.select.MaxHeapSort; -import sorts.templates.Sort; - -/* -Copyright (c) 2020-2021 thatsOven -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 MedianOfSixteenAdaptiveQuickSort extends Sort { - MaxHeapSort heapSorter; - UnstableInsertionSort insertSorter; - - public MedianOfSixteenAdaptiveQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Median-of-16 Adaptive Quick"); - this.setRunAllSortsName("Median-Of-16 Adaptive QuickSort"); - this.setRunSortName("Median-Of-16 Adaptive QuickSort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - int[] medianOfSixteenSwaps = new int[] { - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16, - 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16, - 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15, 8, 16, - 6, 11, 7, 10, 4, 13, 14, 15, 8, 12, 2, 3, 5, 9, - 2, 5, 8, 14, 3, 9, 12, 15, 6, 7, 10, 11, - 3, 5, 12, 14, 4, 9, 8, 13, - 7, 9, 11, 13, 4, 6, 8, 10, - 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, - 7, 8, 9, 10 - }; - - int incs[] = {48, 21, 7, 3, 1}; - - public int log2(int N) { - int result = (int)(Math.log(N) / Math.log(2)); - return result; - } - - private void shellSort(int[] array, int lo, int hi) { - Highlights.clearAllMarks(); - - for (int k = 0; k < this.incs.length; k++) { - for (int h = this.incs[k], i = h + lo; i < hi; i++) - { - int v = array[i]; - int j = i; - - while (j >= h + lo && Reads.compareValues(array[j-h], v) == 1) - { - Highlights.markArray(1, j); - - Writes.write(array, j, array[j - h], 1, true, false); - j -= h; - } - Writes.write(array, j, v, 0.5, true, false); - } - } - Highlights.clearAllMarks(); - } - - public int partition(int[] array, int a, int b, int p) { - int i = a - 1; - int j = b; - Highlights.markArray(3, p); - - while(true) { - i++; - while(i < b && Reads.compareIndices(array, i, p, 0, false) == -1) { - Highlights.markArray(1, i); - Delays.sleep(0.25); - i++; - } - j--; - while(j >= a && Reads.compareIndices(array, j, p, 0, false) == 1) { - Highlights.markArray(2, j); - Delays.sleep(0.25); - j--; - } - if(i < j) Writes.swap(array, i, j, 1, true, false); - else return j; - } - } - - private void medianOfThree(int[] array, int a, int b) { - int m = a+(b-1-a)/2; - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - Writes.swap(array, a, m, 1, true, false); - - if(Reads.compareIndices(array, m, b-1, 1, true) == 1) { - Writes.swap(array, m, b-1, 1, true, false); - - if(Reads.compareIndices(array, a, m, 1, true) == 1) - return; - } - - Writes.swap(array, a, m, 1, true, false); - } - - private void compNSwap(int[] array, int a, int b, int gap, int start) { - if (Reads.compareIndices(array, start+(a*gap), start+(b*gap), 2, true) > 0) { - Writes.swap(array, start+(a*gap), start+(b*gap), 2, true, false); - } - } - - private void medianOfSixteen(int[] array, int a, int b) { - int gap = (b - 1 - a) / 16; - - for (int i = 0; i < this.medianOfSixteenSwaps.length; i += 2) - this.compNSwap(array, this.medianOfSixteenSwaps[i], this.medianOfSixteenSwaps[i+1], gap, a); - - Writes.swap(array, a, a + (8 * gap), 1, true, false); - } - - public boolean getSortedRuns(int[] array, int a, int b) { - Highlights.clearAllMarks(); - boolean reverseSorted = true; - boolean sorted = true; - int comp; - - for (int i = a; i < b-1; i++) { - comp = Reads.compareIndices(array, i, i+1, 0.5, true); - if (comp > 0) sorted = false; - else reverseSorted = false; - if ((!reverseSorted) && (!sorted)) return false; - } - - if (reverseSorted && !sorted) { - Writes.reversal(array, a, b-1, 1, true, false); - sorted = true; - } - - return sorted; - } - - public void quickSort(int[] array, int a, int b, int depth, boolean unbalanced) { - while (b - a > 32) { - if (this.getSortedRuns(array, a, b)) return; - if (depth == 0){ - heapSorter.customHeapSort(array, a, b, 1); - return; - } - - int p; - if (!unbalanced) { - this.medianOfThree(array, a, b); - p = this.partition(array, a, b, a); - } else p = a; - - int left = p - a; - int right = b - (p + 1); - if ((left == 0 || right == 0) || (left/right >= 16 || right/left >= 16) || unbalanced) { - if (b - a > 80) { - Writes.swap(array, a, p, 1, true, false); - if (left < right) { - this.quickSort(array, a, p, depth - 1, true); - a = p; - } else { - this.quickSort(array, p + 1, b, depth - 1, true); - b = p; - } - this.medianOfSixteen(array, a, b); - p = this.partition(array, a + 1, b, a); - } else { - this.shellSort(array, a, b); - return; - } - } - - Writes.swap(array, a, p, 1, true, false); - - depth--; - - this.quickSort(array, p+1, b, depth, false); - b = p; - } - insertSorter.unstableInsertionSort(array, a, b); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - heapSorter = new MaxHeapSort(arrayVisualizer); - insertSorter = new UnstableInsertionSort(arrayVisualizer); - - this.quickSort(array, 0, currentLength, 2*log2(currentLength), false); - } -} diff --git a/src/sorts/hybrid/OOPBufferedMergeSort.java b/src/sorts/hybrid/OOPBufferedMergeSort.java deleted file mode 100644 index bdec3962..00000000 --- a/src/sorts/hybrid/OOPBufferedMergeSort.java +++ /dev/null @@ -1,195 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 OOPBufferedMergeSort extends Sort { - private BinaryInsertionSort binaryInserter; - private ReverseLazyStableSort finalMerger; - - private int[] buffer; - - public OOPBufferedMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Out-of-Place Buffered Merge"); - this.setRunAllSortsName("Out-of-Place Buffered Merge Sort"); - this.setRunSortName("Out-of-Place Buffered Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private static double log2(double value) { - return Math.log(value) / Math.log(2); - } - - private static int getBufferSize(int length) { - return (int)Math.pow(2, Math.ceil(log2((int)log2(length)))) * 2; - } - - private void mergeUnderBuffer(int[] array, int bufferSize, int start, int mid, int end) { - int bufferPointer = 0; - int left = start, right = mid; - - while (left < mid && right < end) { - if (Reads.compareIndices(array, left, right, 0, true) <= 0) { - Highlights.markArray(2, left); - Writes.write(this.buffer, bufferPointer, array[left], 0.25, true, true); - left++; - } - else { - Highlights.markArray(2, right); - Writes.write(this.buffer, bufferPointer, array[right], 0.25, true, true); - right++; - } - bufferPointer++; - } - - while (left < mid) { - Highlights.markArray(2, left); - Writes.write(this.buffer, bufferPointer, array[left], 0.25, true, true); - left++; - bufferPointer++; - } - while (right < end) { - Highlights.markArray(2, right); - Writes.write(this.buffer, bufferPointer, array[right], 0.25, true, true); - right++; - bufferPointer++; - } - - for (int i = 0; i < end - start; i++) { - Highlights.markArray(2, i); - Writes.write(array, start + i, this.buffer[i], 0.5, true, false); - } - } - - private void blockCopy(int array[], int start, int end) { - int blockSize = end - start; - for (int i = end - 1; i >= start; i--) { - Writes.write(array, i, array[i - blockSize], 0.125, true, false); - } - } - - private void blockCopyFromBuffer(int array[], int start, int end) { - for (int i = end - 1; i >= start; i--) { - Highlights.markArray(2, i - start); - Writes.write(array, i, buffer[i - start], 0.125, true, false); - } - Highlights.clearMark(2); - } - - private void blockInsertionSort(int[] array, int start, int mid, int end, int blockSize) { - for (int i = mid; i < end; i += blockSize) { - int key = array[i]; - int j = i - blockSize; - - if (Reads.compareValues(key, array[j]) >= 0) { - continue; - } - - for (int k = i; k < i + blockSize; k++) { - Highlights.markArray(2, k); - Writes.write(buffer, k - i, array[k], 0.5, true, true); - } - Highlights.clearMark(2); - - blockCopy(array, j + blockSize, j + 2 * blockSize); - j -= blockSize; - - while (j >= start && Reads.compareValues(key, array[j]) < 0) { - blockCopy(array, j + blockSize, j + 2 * blockSize); - j -= blockSize; - } - blockCopyFromBuffer(array, j + blockSize, j + 2 * blockSize); - } - } - - private void mergeOverBuffer(int[] array, int bufferSize, int start, int mid, int end) { - int blockSize = bufferSize / 2; - blockInsertionSort(array, start, mid, end, blockSize); - - int checkStart = start; - while (checkStart < end - blockSize) { - if (Reads.compareIndices(array, checkStart + blockSize - 1, checkStart + blockSize, 1, true) == 1) { - mergeUnderBuffer(array, bufferSize, checkStart, checkStart + blockSize, checkStart + bufferSize); - } - checkStart += blockSize; - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - finalMerger = new ReverseLazyStableSort(arrayVisualizer); - - int bufferSize = OOPBufferedMergeSort.getBufferSize(sortLength); - int length = sortLength - ((sortLength - bufferSize) % (bufferSize / 2)); - if (bufferSize * 2 >= length) { - binaryInserter.customBinaryInsert(array, 0, sortLength, 0.333); - return; - } - this.buffer = Writes.createExternalArray(bufferSize); - - for (int i = 0; i < length - 1; i += 2) { - if (Reads.compareIndices(array, i, i + 1, 0.5, true) == 1) { - Writes.swap(array, i, i + 1, 0.5, true, false); - } - } - - for (int gap = 4; gap <= bufferSize; gap *= 2) { - for (int i = 0; i + gap <= length; i += gap) { - mergeUnderBuffer(array, bufferSize, i, i + gap / 2, i + gap); - } - } - - for (int gap = bufferSize * 2; gap / 2 <= length; gap *= 2) { - int i; - for (i = 0; i + gap <= length; i += gap) { - mergeOverBuffer(array, bufferSize, i, i + gap / 2, i + gap); - } - if (i + gap > length) { - mergeOverBuffer(array, bufferSize, i, i + gap / 2, length); - } - } - - if (sortLength - length > 0) { - binaryInserter.customBinaryInsert(array, length, sortLength, 0.333); - finalMerger.merge(array, 0, length, sortLength); - } - Writes.deleteExternalArray(this.buffer); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/OptimizedLazyStableSort.java b/src/sorts/hybrid/OptimizedLazyStableSort.java index a4bde038..288bdb1f 100644 --- a/src/sorts/hybrid/OptimizedLazyStableSort.java +++ b/src/sorts/hybrid/OptimizedLazyStableSort.java @@ -1,11 +1,10 @@ package sorts.hybrid; import main.ArrayVisualizer; -import sorts.insert.PatternDefeatingInsertionSort; import sorts.templates.GrailSorting; /* - * + * The MIT License (MIT) Copyright (c) 2013 Andrey Astrelin @@ -42,7 +41,7 @@ this software and associated documentation files (the "Software"), to deal in final public class OptimizedLazyStableSort extends GrailSorting { public OptimizedLazyStableSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Lazy Stable"); this.setRunAllSortsName("Optimized Lazy Stable Sort"); this.setRunSortName("Optimized Lazy Stable Sort"); @@ -55,13 +54,35 @@ public OptimizedLazyStableSort(ArrayVisualizer arrayVisualizer) { this.setBogoSort(false); } + public void insertionSort(int[] array, int a, int b, double sleep, boolean auxwrite) { + int i = a + 1; + if(Reads.compareIndices(array, i - 1, i++, sleep, true) == 1) { + while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) == 1) i++; + Writes.reversal(array, a, i - 1, sleep, true, auxwrite); + } + else while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) <= 0) i++; + + Highlights.clearMark(2); + + while(i < b) { + int current = array[i]; + int pos = i - 1; + while(pos >= a && Reads.compareValues(array[pos], current) > 0){ + Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); + pos--; + } + Writes.write(array, pos + 1, current, sleep, true, auxwrite); + + i++; + } + } + protected void grailLazyStableSort(int[] arr, int pos, int len) { - PatternDefeatingInsertionSort inserter = new PatternDefeatingInsertionSort(arrayVisualizer); int dist; for (dist = 0; dist + 16 < len; dist += 16) - inserter.insertionSort(arr, pos + dist, pos + dist + 16, 1, false); + insertionSort(arr, pos + dist, pos + dist + 16, 1, false); if (dist < len) - inserter.insertionSort(arr, pos + dist, pos + len, 1, false); + insertionSort(arr, pos + dist, pos + len, 1, false); for(int part = 16; part < len; part *= 2) { int left = 0; @@ -78,9 +99,9 @@ protected void grailLazyStableSort(int[] arr, int pos, int len) { } } } - + @Override - public void runSort(int[] array, int length, int bucketCount) { + public void runSort(int[] array, int length, int bucketCount) { this.grailLazyStableSort(array, 0, length); } } \ No newline at end of file diff --git a/src/sorts/hybrid/OptimizedPDMergeSort.java b/src/sorts/hybrid/OptimizedPDMergeSort.java deleted file mode 100644 index 7a29c09a..00000000 --- a/src/sorts/hybrid/OptimizedPDMergeSort.java +++ /dev/null @@ -1,122 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.merge.PDMergeSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2021 Gaming32 - -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. - * - */ - -public class OptimizedPDMergeSort extends PDMergeSort { - final static int MIN_RUN_SIZE = 16; - - public OptimizedPDMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Pattern-Defeating Merge"); - this.setRunAllSortsName("Optimized Pattern-Defeating Merge Sort"); - this.setRunSortName("Optimized Pattern-Defeating Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void insertSort(int[] array, int start, int mid, int end) { - int pos; - int cur; - for (int i = mid; i < end; i++) { - cur = array[i]; - pos = i - 1; - while (pos >= start && Reads.compareValues(array[pos], cur) > 0) { - Writes.write(array, pos + 1, array[pos], 0.5, true, false); - pos--; - } - Writes.write(array, pos + 1, cur, 0.5, true, false); - } - } - - protected int identifyRun(int[] array, int index, int maxIndex) { - int startIndex = index; - - Highlights.markArray(1, index); - if (index >= maxIndex) { - return -1; - } - - boolean cmp = compare(array[index], array[index + 1]); - index++; - Highlights.markArray(1, index); - - while (index < maxIndex) { - Delays.sleep(1); - boolean checkCmp = compare(array[index], array[index + 1]); - if (checkCmp != cmp) { - break; - } - index++; - Highlights.markArray(1, index); - } - Delays.sleep(1); - - if (!cmp) { - // arrayVisualizer.setHeading("PDMerge -- Reversing Run"); - Writes.reversal(array, startIndex, index, 1, true, false); - Highlights.clearMark(2); - // arrayVisualizer.setHeading("PDMerge -- Finding Runs"); - } - int length = index - startIndex + 1; - if (length < MIN_RUN_SIZE) { - int end = startIndex + MIN_RUN_SIZE; - if (end > maxIndex + 1) { - end = maxIndex + 1; - } - insertSort(array, startIndex, index + 1, end); - return end > maxIndex ? -1 : end; - } - if (index >= maxIndex) { - return -1; - } - return index + 1; - } - - protected int[] findRuns(int[] array, int maxIndex) { - int[] runs = Writes.createExternalArray(maxIndex / MIN_RUN_SIZE + 2); - runCount = 0; - - int lastRun = 0; - while (lastRun != -1) { - Writes.write(runs, runCount++, lastRun, 0.5, true, true); - int newRun = identifyRun(array, lastRun, maxIndex); - lastRun = newRun; - } - - return runs; - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/PDLaziestSort.java b/src/sorts/hybrid/PDLaziestSort.java deleted file mode 100644 index 5b952202..00000000 --- a/src/sorts/hybrid/PDLaziestSort.java +++ /dev/null @@ -1,112 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import utils.IndexedRotations; - -/** - * @author Yuri-chan2007 - * - */ -public final class PDLaziestSort extends Sort { - - public PDLaziestSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Pattern-Defeating Laziest Stable"); - this.setRunAllSortsName("Pattern-Defeating Laziest Stable Sort"); - this.setRunSortName("Pattern-Defeating Laziest Sort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void insertionSort(int[] array, int a, int b, double sleep, boolean auxwrite) { - int i = a + 1; - if(Reads.compareIndices(array, i - 1, i++, sleep, true) == 1) { - while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) == 1) i++; - Writes.reversal(array, a, i - 1, sleep, true, auxwrite); - } - else while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) <= 0) i++; - - Highlights.clearMark(2); - - while(i < b) { - int current = array[i]; - int pos = i - 1; - while(pos >= a && Reads.compareValues(array[pos], current) > 0){ - Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); - pos--; - } - Writes.write(array, pos + 1, current, sleep, true, auxwrite); - - i++; - } - } - - private void rotate(int[] array, int a, int m, int b) { - IndexedRotations.cycleReverse(array, a, m, b, 1.0, true, false); - } - - private int leftBinSearch(int[] array, int a, int b, int val) { - while(a < b) { - int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) - b = m; - else - a = m+1; - } - - return a; - } - - private int leftExpSearch(int[] array, int a, int b, int val) { - int i = 1; - while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) > 0) i *= 2; - - return this.leftBinSearch(array, a+i/2, Math.min(b, a-1+i), val); - } - - private void inPlaceMerge(int[] array, int a, int m, int b) { - int i = a, j = m, k; - - while(i < j && j < b){ - if(Reads.compareValues(array[i], array[j]) == 1) { - k = this.leftExpSearch(array, j+1, b, array[i]); - this.rotate(array, i, j, k); - - i += k-j; - j = k; - } - else i++; - } - } - - protected void laziestStableSort(int[] array, int start, int end) { - int len = end - start; - if(len <= 16) { - insertionSort(array, start, end, 0.5, false); - return; - } - int i, blockLen = Math.max(16, (int)Math.sqrt(len)); - for(i = start; i+2*blockLen < end; i+=blockLen) { - insertionSort(array, i, i + blockLen, 0.5, false); - } - insertionSort(array, i, end, 0.5, false); - while(i-blockLen >= start) { - this.inPlaceMerge(array, i-blockLen, i, end); - i-=blockLen; - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - this.laziestStableSort(array, 0, sortLength); - - } - -} diff --git a/src/sorts/hybrid/PairwiseCircleSort.java b/src/sorts/hybrid/PairwiseCircleSort.java deleted file mode 100644 index 0b0ee13b..00000000 --- a/src/sorts/hybrid/PairwiseCircleSort.java +++ /dev/null @@ -1,90 +0,0 @@ -package sorts.hybrid; - -import sorts.templates.Sort; -import sorts.insert.InsertionSort; - -import main.ArrayVisualizer; - -/* - * -MIT License - -Copyright (c) 2020 yuji - -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 PairwiseCircleSort extends Sort { - public PairwiseCircleSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Pairwise-Circle"); - this.setRunAllSortsName("Pairwise-Circle Sort"); - this.setRunSortName("Pairwise-Circle Sort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void pairs(int[] array, int left, int right, int gap) { - if(left+gap >= right) return; - int a = left; - while(a + gap <= right) { - if(Reads.compareIndices(array, a, a+gap, 1, true) == 1) { - Writes.swap(array, a, a+gap, 1, true, false); - } - a += gap*2; - } - this.pairs(array, left, right, gap*2); - this.pairs(array, left+gap, right, gap*2); - } - - private void circle(int[] array, int left, int right) { - int a = left; - int b = right; - while(a < b) { - if(Reads.compareIndices(array, a, b, 1, true) == 1) { - Writes.swap(array, a, b, 1, true, false); - } - a++; - b--; - } - } - - private void pairCircle(int[] array, int left, int right) { - if(left >= right) return; - int mid = (left + right) / 2; - this.pairs(array, left, right, 1); - this.circle(array, left, right); - this.pairCircle(array, left, mid); - this.pairCircle(array, mid+1, right); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.pairCircle(array, 0, length-1); - InsertionSort sort = new InsertionSort(this.arrayVisualizer); - sort.customInsertSort(array, 0, length, 0.1, false); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/PlasmaSort.java b/src/sorts/hybrid/PlasmaSort.java deleted file mode 100644 index 32ae0129..00000000 --- a/src/sorts/hybrid/PlasmaSort.java +++ /dev/null @@ -1,305 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.insert.InsertionSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; -import utils.Highlights; -import utils.Rotations; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 PlasmaSort extends Sort { - int[] keys; - - private InsertionSort insertSorter; - private LazierSort finalMerger; - - public PlasmaSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Plasma"); - this.setRunAllSortsName("Plasma Sort"); - this.setRunSortName("Plasmasort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void rotate(int[] array, int pos, int lenA, int lenB) { - Rotations.holyGriesMills(array, pos, lenA, lenB, 1, true, false); - } - - private int collectKeys(int[] array, int start, int keyCount, int end) { - int minbound = start; - int compindex = start + 1; - int lastGood = compindex; - int badCount = 0; - int count; - for (count = 1; count < keyCount - start; count++) { - Highlights.markArray(3, compindex); - int num = array[compindex]; - int l = minbound, h = lastGood; - int lastBad = badCount; - - while (l < h) { - int m = l + ((h - l) / 2); // avoid int overflow! - Highlights.markArray(2, m); - - Delays.sleep(0.01); - - int comp = Reads.compareValues(num, array[m]); - - if (comp < 0) { - h = m; - } - else if (comp == 0) { - badCount++; - break; - } - else { - l = m + 1; - } - } - - if (badCount > 0) { - if (badCount > lastBad) { - Delays.sleep(0.2); - count--; - compindex++; - if (compindex >= end) { - break; - } - continue; - } - rotate(array, minbound, lastGood - minbound, badCount); - minbound += badCount; - lastGood = compindex; - l += badCount; - badCount = 0; - } - - // item has to go into position lo - int j = compindex - 1; - - while (j >= l) - { - Writes.write(array, j + 1, array[j], 1, true, false); - j--; - } - Writes.write(array, l, num, 1, true, false); - - Highlights.clearAllMarks(); - compindex++; - lastGood++; - - if (compindex >= end) { - break; - } - } - - Highlights.clearMark(4); - if (minbound != start) { - rotate(array, start, minbound - start, count); - } - return count; - } - - private static int getBufferSize(int length) { - int size; - for (size = 1; size * size < length; size *= 2); - return size; - } - - private static int getKeySize(int bufferSize, int length) { - return length / bufferSize; - } - - private void mergeUnderBuffer(int[] array, int bufferSize, int start, int mid, int end, boolean rightPriority) { - if (rightPriority) { - rotate(array, start, mid - start, end - mid); - } - for (int i = 0; i < mid - start; i++) { - Writes.swap(array, i, start + i, 0.5, true, false); - } - - int bufferPointer = 0; - int left = start, right = mid; - - while (left < right && right < end) { - if (Reads.compareIndices(array, bufferPointer, right, 0.1, true) <= 0) { - Writes.swap(array, bufferPointer++, left++, 0.25, true, false); - } - else { - Writes.swap(array, left++, right++, 0.25, true, false); - } - } - - while (left < right) { - Writes.swap(array, bufferPointer++, left++, 0.25, true, false); - } - } - - public void blockSwap(int[] array, int a, int b, int len) { - for (int i = 0; i < len; i++) { - Writes.swap(array, a + i, b + i, 1, true, false); - } - } - - // public void blockSelection(int[] array, int start, int end, int blockSize) { - // for (int i = start; i < end - blockSize; i += blockSize) { - // int lowestindex = i; - // int lowestkey = (lowestindex - start) / blockSize; - - // for (int j = i + blockSize; j < end; j += blockSize) { - // int jkey = (j - start) / blockSize; - // Highlights.markArray(2, j); - // Delays.sleep(0.5); - - // int comp = Reads.compareValues(array[j], array[lowestindex]); - // if (comp == -1 || (comp == 0 && Reads.compareOriginalIndices(keys, jkey, lowestkey, 0.25, true) == -1)) { - // lowestindex = j; - // lowestkey = jkey; - // Highlights.markArray(1, lowestindex); - // Delays.sleep(0.5); - // } - // } - // if (lowestindex > i) { - // blockSwap(array, i, lowestindex, blockSize); - // Writes.swap(keys, (i - start) / blockSize, lowestkey, 1, true, true); - // } - // } - // } - - private void mergeOverBuffer(int[] array, int bufferSize, int start, int mid, int end, int keySize) { - resetKeys(keySize); - int midKey = keys[keySize / 2]; - int blockSize = bufferSize; - // blockSelection(array, start, end, blockSize); - - int i; - for (i = start; i < end - blockSize; i += blockSize) { - int ikey = (i - start) / blockSize; - int lowestindex = i; - int lowestkey = ikey; - - for (int j = i + blockSize; j < end; j += blockSize) { - int jkey = (j - start) / blockSize; - Highlights.markArray(2, j); - Delays.sleep(0.5); - - int comp = Reads.compareValues(array[j], array[lowestindex]); - if (comp == -1 || (comp == 0 && Reads.compareOriginalIndices(keys, jkey, lowestkey, 0.25, true) == -1)) { - lowestindex = j; - lowestkey = jkey; - Highlights.markArray(1, lowestindex); - Delays.sleep(0.5); - } - } - if (lowestindex > i) { - blockSwap(array, i, lowestindex, blockSize); - Writes.swap(keys, (i - start) / blockSize, lowestkey, 1, true, true); - } - if (ikey > 0) { - if (Reads.compareIndices(array, i - 1, i, 0.5, true) > 0) { - int keyIndex = (i - start) / blockSize; - mergeUnderBuffer(array, bufferSize, i - blockSize, i, i + blockSize, keys[keyIndex - 1] > midKey && keys[keyIndex - 1] > keys[keyIndex]); - } - } - } - int keyIndex = keySize - 1; - mergeUnderBuffer(array, bufferSize, i - blockSize, i, i + blockSize, keys[keyIndex - 1] > midKey && keys[keyIndex - 1] > keys[keyIndex]); - - // int checkStart = start; - // while (checkStart < end - blockSize) { - // if (Reads.compareIndices(array, checkStart + blockSize - 1, checkStart + blockSize, 1, true) == 1) { - // int keyIndex = (checkStart - start) / blockSize; - // // int keyIndex2 = (checkStart + blockSize - start) / blockSize; - // // Highlights.markArray(1, keyIndex); - // mergeUnderBuffer(array, bufferSize, checkStart, checkStart + blockSize, checkStart + 2 * blockSize, Reads.compareOriginalValues(keys[keyIndex], keys[keyIndex + 1]) == 1); - // } - // checkStart += blockSize; - // } - } - - private void resetKeys(int count) { - for (int i = 0; i < count; i++) { - Writes.write(keys, i, i, 0.5, true, true); - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - insertSorter = new InsertionSort(arrayVisualizer); - finalMerger = new LazierSort(arrayVisualizer); - - if (sortLength < 24) { - insertSorter.customInsertSort(array, 0, sortLength, 0.333, false); - return; - } - int bufferSize = PlasmaSort.getBufferSize(sortLength); - collectKeys(array, 0, bufferSize, sortLength); - - int i; - for (i = bufferSize + 1; i < sortLength; i += 2) { - if (Reads.compareIndices(array, i - 1, i, 0.5, true) == 1) { - Writes.swap(array, i - 1, i, 0.5, true, false); - } - } - - int gap; - for (gap = 2; gap < bufferSize * 2; gap *= 2) { - for (i = bufferSize; i + 2 * gap <= sortLength; i += 2 * gap) { - mergeUnderBuffer(array, bufferSize, i, i + gap, i + 2 * gap, false); - } - if (i + gap < sortLength) { - mergeUnderBuffer(array, bufferSize, i, i + gap, sortLength, false); - } - } - - int keyCount = PlasmaSort.getKeySize(bufferSize, sortLength); - keys = Writes.createExternalArray(keyCount); - for (; gap <= sortLength - bufferSize; gap *= 2) { - int keySize = 2 * gap / bufferSize; - for (i = bufferSize; i + 2 * gap <= sortLength; i += 2 * gap) { - mergeOverBuffer(array, bufferSize, i, i + gap, i + 2 * gap, keySize); - } - if (i + gap < sortLength) { - mergeOverBuffer(array, bufferSize, i, i + gap, sortLength, keySize); - } - } - - Writes.deleteExternalArray(keys); - // runSort(array, bufferSize, bucketCount); - // finalMerger.inPlaceMerge(array, 0, bufferSize, sortLength); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/QuarterMergeSort.java b/src/sorts/hybrid/QuarterMergeSort.java deleted file mode 100644 index a6bfedae..00000000 --- a/src/sorts/hybrid/QuarterMergeSort.java +++ /dev/null @@ -1,140 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.merge.BlockSwapMergeSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 QuarterMergeSort extends Sort { - private BinaryInsertionSort binaryInserter; - private BlockSwapMergeSort finalMerger; - - public QuarterMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Quarter Merge"); - this.setRunAllSortsName("Quarter Merge Sort"); - this.setRunSortName("Quarter Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void blockSwap(int[] array, int a, int b, int size) { - for (int i = 0; i < size; i++) { - Writes.swap(array, a + i, b + i, 1, true, false); - } - } - - private void merge(int[] array, int bufferSize, int start, int mid, int end) { - blockSwap(array, 0, start, mid - start); - - int bufferPointer = 0; - int left = start, right = mid; - - while (left < right && right < end) { - if (Reads.compareIndices(array, bufferPointer, right, 0.5, true) <= 0) { - Writes.swap(array, bufferPointer++, left++, 0.5, true, false); - } - else { - Writes.swap(array, left++, right++, 0.5, true, false); - } - } - - while (left < right) { - Writes.swap(array, bufferPointer++, left++, 0.5, true, false); - } - } - - private int pow2lte(int value) { - int val; - for (val = 1; val <= value; val <<= 1); - return val >> 1; - } - - public void quarterMergeSort(int[] array, int length) { - if (finalMerger == null) { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - finalMerger = new BlockSwapMergeSort(arrayVisualizer); - } - - if (length <= 32) { - binaryInserter.customBinaryInsert(array, 0, length, 0.333); - return; - } - int quarterSize = length / 4; - int useLength = quarterSize * 4; - - for (int i = quarterSize; i < useLength - 1; i += 2) { - if (Reads.compareIndices(array, i, i + 1, 0.5, true) == 1) { - Writes.swap(array, i, i + 1, 0.5, true, false); - } - } - - int subStart, subEnd = useLength, subLength = useLength - quarterSize; - int gap; - for (int parlen = subLength; parlen >= 2; parlen = subEnd - quarterSize) { - subLength = pow2lte(parlen); - subStart = subEnd - subLength; - - for (gap = 4; gap <= subLength; gap *= 2) { - for (int i = subStart; i + gap <= subEnd; i += gap) { - merge(array, quarterSize, i, i + gap / 2, i + gap); - } - } - if (parlen != useLength - quarterSize) { - merge(array, quarterSize, subStart, subEnd, useLength); - } - subEnd = subStart; - } - - int extra = length - useLength; - if (extra > 0) { - if (extra > 1 && Reads.compareIndices(array, length - 2, length - 1, 0.5, true) == 1) { - Writes.swap(array, length - 2, length - 1, 0.5, true, false); - } - finalMerger.multiSwapMerge(array, quarterSize, useLength, length); - } - - quarterMergeSort(array, quarterSize); - finalMerger.multiSwapMerge(array, 0, quarterSize, length); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - finalMerger = new BlockSwapMergeSort(arrayVisualizer); - - this.quarterMergeSort(array, sortLength); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/QuickSPSort.java b/src/sorts/hybrid/QuickSPSort.java deleted file mode 100644 index 0162b1f7..00000000 --- a/src/sorts/hybrid/QuickSPSort.java +++ /dev/null @@ -1,73 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.templates.GrailSorting; - -final public class QuickSPSort extends GrailSorting { - public QuickSPSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Quick Sandpaper"); - this.setRunAllSortsName("Quick Sandpaper Sort"); - this.setRunSortName("Quick Sandpapersort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void compSwap(int[] array, int a, int b) { - a--; b--; - if (Reads.compareIndices(array, a, b, 0.0125, true) > 0) { - Writes.swap(array, a, b, 0.0125, true, false); - } - } - - private void ipMerge(int[] array, int a, int m, int b) { - int len1 = m - a, len2 = b - m; - this.grailMergeWithoutBuffer(array, a, len1, len2); - } - - private void quickSPSort2(int[] array, int l, int r) { - if (r - l < 12) { - for (int i = l; i <= r; i++) { - for (int j = i; j <= r; j++) { - compSwap(array, i, j); - } - } - } - else { - int rb, min, j; - rb = l + (int)Math.ceil(Math.sqrt(1 + (r - l))); - for (int i = l; i <= rb; i++) { - for (j = i; j <= r; j++) { - compSwap(array, i, j); - } - } - min = array[rb]; - Highlights.markArray(2, rb); - j = 1 + rb; - for (int i = 1 + rb; i <= r; i++) { - Highlights.markArray(1, i - 1); - Delays.sleep(0.0125); - if (Reads.compareValues(array[i - 1], min) < 0) { - min = array[i - 1]; - Highlights.markArray(2, i - 1); - j++; - Writes.swap(array, i - 1, j - 1, 0.125, true, false); - } - } - Writes.reversal(array, rb, j - 1, 1, true, false); - quickSPSort2(array, 1 + j, r); - ipMerge(array, rb, j, r); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - quickSPSort2(array, 1, currentLength); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/RemiSort.java b/src/sorts/hybrid/RemiSort.java index 36a75707..5172aa59 100644 --- a/src/sorts/hybrid/RemiSort.java +++ b/src/sorts/hybrid/RemiSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -30,56 +30,56 @@ of this software and associated documentation files (the "Software"), to deal */ final public class RemiSort extends MultiWayMergeSorting { - public RemiSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Remi"); - this.setRunAllSortsName("Remi Sort"); - this.setRunSortName("Remisort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - + public RemiSort(ArrayVisualizer arrayVisualizer) { + super(arrayVisualizer); + + this.setSortListName("Remi"); + this.setRunAllSortsName("Remi Sort"); + this.setRunSortName("Remisort"); + this.setCategory("Hybrid Sorts"); + this.setComparisonBased(true); + this.setBucketSort(false); + this.setRadixSort(false); + this.setUnreasonablySlow(false); + this.setUnreasonableLimit(0); + this.setBogoSort(false); + } + //stable sorting algorithm that guarantees worst case performance of //O(n log n) comparisons and O(n) moves in O(n^2/3) memory - + private int ceilCbrt(int n) { int a = 0, b = Math.min(1291, n); - + while(a < b) { int m = (a+b)/2; - + if(m*m*m >= n) b = m; else a = m+1; } - + return a; } - + private void siftDown(int[] array, int[] keys, int r, int len, int a, int t) { int j = r; - + while(2*j + 1 < len) { j = 2*j + 1; - + if(j+1 < len) { int cmp = Reads.compareIndices(array, a+keys[j+1], a+keys[j], 0.2, true); - + if(cmp > 0 || (cmp == 0 && Reads.compareOriginalValues(keys[j+1], keys[j]) > 0)) j++; } } for(int cmp = Reads.compareIndices(array, a+t, a+keys[j], 0.2, true); - + cmp > 0 || (cmp == 0 && Reads.compareOriginalValues(t, keys[j]) > 0); - + j = (j-1)/2, cmp = Reads.compareIndices(array, a+t, a+keys[j], 0.2, true)); - + for(int t2; j > r; j = (j-1)/2) { t2 = keys[j]; Highlights.markArray(3, j); @@ -89,13 +89,13 @@ private void siftDown(int[] array, int[] keys, int r, int len, int a, int t) { Highlights.markArray(3, r); Writes.write(keys, r, t, 0.2, false, true); } - + private void tableSort(int[] array, int[] keys, int a, int b) { int len = b-a; - + for(int i = (len-1)/2; i >= 0; i--) this.siftDown(array, keys, i, len, a, keys[i]); - + for(int i = len-1; i > 0; i--) { int t = keys[i]; Highlights.markArray(3, i); @@ -103,44 +103,44 @@ private void tableSort(int[] array, int[] keys, int a, int b) { this.siftDown(array, keys, 0, i, a, t); } Highlights.clearMark(3); - + for(int i = 0; i < len; i++) { Highlights.markArray(2, i); if(Reads.compareOriginalValues(i, keys[i]) != 0) { int t = array[a+i]; int j = i, next = keys[i]; - + do { Writes.write(array, a+j, array[a+next], 1, true, false); Writes.write(keys, j, j, 1, true, true); - + j = next; next = keys[next]; } while(Reads.compareOriginalValues(next, i) != 0); - + Writes.write(array, a+j, t, 1, true, false); Writes.write(keys, j, j, 1, true, true); } } Highlights.clearMark(2); } - + private void blockCycle(int[] array, int[] buf, int[] keys, int a, int bLen, int bCnt) { for(int i = 0; i < bCnt; i++) { if(Reads.compareOriginalValues(i, keys[i]) != 0) { Writes.arraycopy(array, a + i*bLen, buf, 0, bLen, 1, true, true); int j = i, next = keys[i]; - + do { Writes.arraycopy(array, a + next*bLen, array, a + j*bLen, bLen, 1, true, false); Writes.write(keys, j, j, 1, true, true); - + j = next; next = keys[next]; } while(Reads.compareOriginalValues(next, i) != 0); - + Writes.arraycopy(buf, 0, array, a + j*bLen, bLen, 1, true, false); Writes.write(keys, j, j, 1, true, true); } @@ -148,126 +148,124 @@ private void blockCycle(int[] array, int[] buf, int[] keys, int a, int bLen, int } private void kWayMerge(int[] array, int[] buf, int[] keys, int[] heap, int b, int[] pa, int[] p, int bLen, int rLen) { int k = p.length, size = k, a = pa[0], a1 = pa[1]; - + for(int i = 0; i < k; i++) Writes.write(heap, i, i, 0, false, true); for(int i = (k-1)/2; i >= 0; i--) this.siftDown(array, heap, pa, heap[i], i, k); - + for(int i = 0; i < rLen; i++) { int min = heap[0]; - + Highlights.markArray(2, pa[min]); - + Writes.write(buf, i, array[pa[min]], 0, false, true); Writes.write(pa, min, pa[min]+1, 1, false, true); if(pa[min] == Math.min(a + (min+1)*rLen, b)) this.siftDown(array, heap, pa, heap[--size], 0, size); - else + else this.siftDown(array, heap, pa, heap[0], 0, size); } int t = 0, cnt = 0, c = 0; while(pa[c]-p[c] < bLen) c++; - + do { int min = heap[0]; - + Highlights.markArray(2, pa[min]); Highlights.markArray(3, p[c]); - + Writes.write(array, p[c], array[pa[min]], 0, false, false); Writes.write(pa, min, pa[min]+1, 0, false, true); Writes.write(p, c, p[c]+1, 1, false, true); if(pa[min] == Math.min(a + (min+1)*rLen, b)) this.siftDown(array, heap, pa, heap[--size], 0, size); - else + else this.siftDown(array, heap, pa, heap[0], 0, size); - + if(++cnt == bLen) { Writes.write(keys, t++, (c > 0) ? p[c]/bLen-bLen-1 : -1, 0, false, true); - + c = cnt = 0; while(pa[c]-p[c] < bLen) c++; } } while(size > 0); - + Highlights.clearAllMarks(); - + while(cnt-- > 0) { Writes.write(p, c, p[c]-1, 0, false, true); Writes.write(array, --b, array[p[c]], 1, true, false); } Writes.write(pa, k-1, b, 0, false, true); Writes.write(keys, keys.length-1, -1, 0, false, true); - + t = 0; while(keys[t] != -1) t++; - + for(int i = 1, j = a; j < p[0]; i++) { while(p[i] < pa[i]) { Writes.write(keys, t++, p[i]/bLen-bLen, 0, false, true); while(keys[t] != -1) t++; - + Writes.arraycopy(array, j, array, p[i], bLen, 1, true, false); Writes.write(p, i, p[i]+bLen, 0, false, true); - + j += bLen; } } Writes.arraycopy(buf, 0, array, a, rLen, 1, true, false); - + this.blockCycle(array, buf, keys, a1, bLen, (b-a1)/bLen); } - - @Override - public void runSort(int[] array, int length, int bucketCount) { + + @Override + public void runSort(int[] array, int length, int bucketCount) { int a = 0, b = length; - + int bLen = this.ceilCbrt(length); int rLen = bLen*bLen; int rCnt = (length-1)/rLen + 1; - + if(rCnt < 2) { int[] keys = Writes.createExternalArray(length); - + for(int i = 0; i < keys.length; i++) Writes.write(keys, i, i, 1, true, true); - + this.tableSort(array, keys, a, b); - + Writes.deleteExternalArray(keys); return; } - - int bCnt = (length-rLen)/bLen; - + int[] keys = Writes.createExternalArray(rLen); int[] buf = Writes.createExternalArray(rLen); - + int[] heap = new int[rCnt]; int[] p = new int[rCnt]; int[] pa = new int[rCnt]; - + int alloc = 3*rCnt; Writes.changeAllocAmount(alloc); - + for(int i = 0; i < keys.length; i++) Writes.write(keys, i, i, 1, true, true); - + for(int i = a, j = 0; i < b; i += rLen, j++) { this.tableSort(array, keys, i, Math.min(i+rLen, b)); Writes.write(pa, j, i, 0, false, true); } Writes.arraycopy(pa, 0, p, 0, rCnt, 0, false, true); - + this.kWayMerge(array, buf, keys, heap, b, pa, p, bLen, rLen); - + Writes.deleteExternalArray(keys); Writes.deleteExternalArray(buf); Writes.changeAllocAmount(-alloc); - } + } } \ No newline at end of file diff --git a/src/sorts/hybrid/SimpleHybridQuickSort.java b/src/sorts/hybrid/SimpleHybridQuickSort.java deleted file mode 100644 index 4d4ae86f..00000000 --- a/src/sorts/hybrid/SimpleHybridQuickSort.java +++ /dev/null @@ -1,118 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.select.PoplarHeapSort; -import sorts.templates.Sort; - -/** - * @author Yuri-chan2007 - * - */ -public final class SimpleHybridQuickSort extends Sort { - - public SimpleHybridQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Simple Hybrid Quick"); - this.setRunAllSortsName("Simple Hybrid Quick Sort"); - this.setRunSortName("Simple Hybrid Quicksort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - PoplarHeapSort heapSorter; - InsertionSort insertSorter; - - private int medianOfThree(int[] array, int a, int m, int b) { - if (Reads.compareValues(array[m], array[a]) > 0) { - if (Reads.compareValues(array[m], array[b]) < 0) - return m; - - if (Reads.compareValues(array[a], array[b]) > 0) - return a; - - else - return b; - } else { - if (Reads.compareValues(array[m], array[b]) > 0) - return m; - - if (Reads.compareValues(array[a], array[b]) < 0) - return a; - - else - return b; - } - } - - private int ninther(int[] array, int a, int b) { - int s = (b - a) / 9; - - int a1 = this.medianOfThree(array, a, a + s, a + 2 * s); - int m1 = this.medianOfThree(array, a + 3 * s, a + 4 * s, a + 5 * s); - int b1 = this.medianOfThree(array, a + 6 * s, a + 7 * s, a + 8 * s); - - return this.medianOfThree(array, a1, m1, b1); - } - - private int medianOfThreeNinthers(int[] array, int a, int b) { - int s = (b - a + 2) / 3; - - int a1 = this.ninther(array, a, a + s); - int m1 = this.ninther(array, a + s, a + 2 * s); - int b1 = this.ninther(array, a + 2 * s, b); - - return this.medianOfThree(array, a1, m1, b1); - } - - private int partition(int[] array, int a, int b, int val) { - int i = a, j = b; - while (i <= j) { - while (this.Reads.compareValues(array[i], val) < 0) { - i++; - this.Highlights.markArray(1, i); - this.Delays.sleep(0.5D); - } - while (this.Reads.compareValues(array[j], val) > 0) { - j--; - this.Highlights.markArray(2, j); - this.Delays.sleep(0.5D); - } - - if (i <= j) - this.Writes.swap(array, i++, j--, 1.0D, true, false); - - } - return i; - } - - private void sort(int[] array, int a, int b, int depthLimit) { - while (b - a > 16) { - if (depthLimit == 0) { - heapSorter.heapSort(array, a, b); - return; - } - int piv = medianOfThreeNinthers(array, a, b - 1); - int p = partition(array, a, b - 1, array[piv]); - depthLimit--; - sort(array, p, b, depthLimit); - b = p; - } - - insertSorter.customInsertSort(array, a, b, 0.5D, false); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - insertSorter = new InsertionSort(this.arrayVisualizer); - heapSorter = new PoplarHeapSort(arrayVisualizer); - sort(array, 0, sortLength, 2 * (int) (Math.log(sortLength) / Math.log(2.0D))); - - } - -} diff --git a/src/sorts/hybrid/StableQuarterMergeSort.java b/src/sorts/hybrid/StableQuarterMergeSort.java deleted file mode 100644 index 0650e019..00000000 --- a/src/sorts/hybrid/StableQuarterMergeSort.java +++ /dev/null @@ -1,145 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.merge.BlockSwapMergeSort; -import sorts.merge.ReverseLazyStableSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 StableQuarterMergeSort extends Sort { - ReverseLazyStableSort rotater; - QuarterMergeSort sort; - BlockSwapMergeSort fallbackSort; - - public StableQuarterMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stable Quarter Merge"); - this.setRunAllSortsName("Stable Quarter Merge Sort"); - this.setRunSortName("Stable Quarter Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(true); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int ejectDuplicates(int[] array, int start, int mid, int end) { - int minbound = start; - int compindex = start + 1; - int lastGood = compindex; - int badCount = 0; - int count; - for (count = 1; count < mid - start; count++) { - Highlights.markArray(4, compindex); - int num = array[compindex]; - int l = minbound, h = lastGood; - int lastBad = badCount; - - while (l < h) { - int m = l + ((h - l) / 2); // avoid int overflow! - Highlights.markArray(1, l); - Highlights.markArray(2, m); - Highlights.markArray(3, h); - - Delays.sleep(0.01); - - int comp = Reads.compareValues(num, array[m]); - - if (comp < 0) { - h = m; - } - else if (comp == 0) { - badCount++; - break; - } - else { - l = m + 1; - } - } - - Highlights.clearMark(3); - - if (badCount > 0) { - if (badCount > lastBad) { - Delays.sleep(0.2); - count--; - compindex++; - if (compindex >= end) { - break; - } - continue; - } - rotater.rotateCommon(array, lastGood, minbound, badCount, 0.1, false); - minbound += badCount; - lastGood = compindex; - l += badCount; - badCount = 0; - } - - // item has to go into position lo - int j = compindex - 1; - - while (j >= l) - { - Writes.write(array, j + 1, array[j], 0.1, true, false); - j--; - } - Writes.write(array, l, num, 0.1, true, false); - - Highlights.clearAllMarks(); - compindex++; - lastGood++; - - if (compindex >= end) { - break; - } - } - - Highlights.clearMark(4); - if (minbound != start) { - rotater.rotateSmart(array, minbound, start, count); - } - return count; - } - - @Override - public void runSort(int[] array, int length, int baseCount) throws Exception { - rotater = new ReverseLazyStableSort(arrayVisualizer); - sort = new QuarterMergeSort(arrayVisualizer); - fallbackSort = new BlockSwapMergeSort(arrayVisualizer); - - int required = length / 4; - if (ejectDuplicates(array, 0, required, length) < required) - fallbackSort.multiSwapMergeSort(array, 0, length); - else - sort.quarterMergeSort(array, length); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/StacklessTimSort.java b/src/sorts/hybrid/StacklessTimSort.java deleted file mode 100644 index 397c8088..00000000 --- a/src/sorts/hybrid/StacklessTimSort.java +++ /dev/null @@ -1,306 +0,0 @@ -package sorts.hybrid; - -import sorts.insert.BinaryInsertionSort; -import sorts.templates.Sort; -import main.ArrayVisualizer; - -/* - * -MIT License - -Copyright (c) 2021 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 StacklessTimSort extends Sort { - public StacklessTimSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stackless Tim"); - this.setRunAllSortsName("Stackless Tim Sort"); - this.setRunSortName("Stackless Timsort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private final int M = 7; - - private int highlight = 0; - - private int leftBinSearch(int[] array, int a, int b, int val) { - while(a < b) { - int m = a+(b-a)/2; - Highlights.markArray(2, this.highlight+m); - Delays.sleep(0.5); - - if(Reads.compareValues(val, array[m]) <= 0) - b = m; - else - a = m+1; - } - - return a; - } - - private int rightBinSearch(int[] array, int a, int b, int val) { - while(a < b) { - int m = a+(b-a)/2; - Highlights.markArray(2, this.highlight+m); - Delays.sleep(0.5); - - if(Reads.compareValues(val, array[m]) < 0) - b = m; - else - a = m+1; - } - - return a; - } - - private int leftExpSearch(int[] array, int a, int b, int val) { - int i = 1; - while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) > 0) i *= 2; - - return this.leftBinSearch(array, a+i/2, Math.min(b, a-1+i), val); - } - - private int rightExpSearch(int[] array, int a, int b, int val) { - int i = 1; - while(b-i >= a && Reads.compareValues(val, array[b-i]) < 0) i *= 2; - - return this.rightBinSearch(array, Math.max(a, b-i+1), b-i/2, val); - } - - private int leftBoundSearch(int[] array, int a, int b, int val) { - int i = 1; - while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) >= 0) i *= 2; - - return this.rightBinSearch(array, a+i/2, Math.min(b, a-1+i), val); - } - - private int rightBoundSearch(int[] array, int a, int b, int val) { - int i = 1; - while(b-i >= a && Reads.compareValues(val, array[b-i]) <= 0) i *= 2; - - return this.leftBinSearch(array, Math.max(a, b-i+1), b-i/2, val); - } - - private void insertTo(int[] array, int a, int b) { - Highlights.clearMark(2); - - if(a > b) { - int temp = array[a]; - - do Writes.write(array, a, array[--a], 0.25, true, false); - while(a > b); - - Writes.write(array, b, temp, 0.25, true, false); - } - } - - private void buildRuns(int[] array, int a, int b, int mRun) { - int i = a+1, j = a; - - while(i < b) { - if(Reads.compareIndices(array, i-1, i++, 1, true) == 1) { - while(i < b && Reads.compareIndices(array, i-1, i, 1, true) == 1) i++; - Writes.reversal(array, j, i-1, 1, true, false); - } - else while(i < b && Reads.compareIndices(array, i-1, i, 1, true) <= 0) i++; - - if(i < b) j = i - (i-j-1)%mRun - 1; - - while(i-j < mRun && i < b) { - this.insertTo(array, i, this.rightBinSearch(array, j, i, array[i])); - i++; - } - j = i++; - } - } - - //galloping mode code refactored from TimSorting.java - private void mergeFW(int[] array, int[] tmp, int a, int m, int b) { - int len1 = m-a, t = a; - Highlights.clearMark(2); - Writes.arraycopy(array, a, tmp, 0, len1, 1, true, true); - - int i = 0, mGallop = M, l = 0, r = 0; - - while(true) { - do { - if(Reads.compareValues(tmp[i], array[m]) <= 0) { - Writes.write(array, a++, tmp[i++], 1, true, false); - l++; - r = 0; - - if(i == len1) return; - } - else { - Highlights.markArray(2, m); - Writes.write(array, a++, array[m++], 1, true, false); - r++; - l = 0; - - if(m == b) { - while(i < len1) Writes.write(array, a++, tmp[i++], 1, true, false); - return; - } - } - } - while((l | r) < mGallop); - - do { - l = this.leftExpSearch(array, m, b, tmp[i])-m; - - for(int j = 0; j < l; j++) - Writes.write(array, a++, array[m++], 1, true, false); - Writes.write(array, a++, tmp[i++], 1, true, false); - - if(i == len1) return; - - else if(m == b) { - while(i < len1) Writes.write(array, a++, tmp[i++], 1, true, false); - return; - } - - this.highlight = t; - r = this.leftBoundSearch(tmp, i, len1, array[m])-i; - this.highlight = 0; - - for(int j = 0; j < r; j++) - Writes.write(array, a++, tmp[i++], 1, true, false); - Writes.write(array, a++, array[m++], 1, true, false); - - if(i == len1) return; - - else if(m == b) { - while(i < len1) Writes.write(array, a++, tmp[i++], 1, true, false); - return; - } - - mGallop--; - } - while((l | r) >= M); - - if(mGallop < 0) mGallop = 0; - mGallop += 2; - } - } - private void mergeBW(int[] array, int[] tmp, int a, int m, int b) { - int len2 = b-m, t = a; - Highlights.clearMark(2); - Writes.arraycopy(array, m, tmp, 0, len2, 1, true, true); - - int i = len2-1, mGallop = M, l = 0, r = 0; - m--; - - while(true) { - do { - if(Reads.compareValues(tmp[i], array[m]) >= 0) { - Writes.write(array, --b, tmp[i--], 1, true, false); - l++; - r = 0; - - if(i < 0) return; - } - else { - Highlights.markArray(2, m); - Writes.write(array, --b, array[m--], 1, true, false); - r++; - l = 0; - - if(m < a) { - while(i >= 0) Writes.write(array, --b, tmp[i--], 1, true, false); - return; - } - } - } - while((l | r) < mGallop); - - do { - l = (m+1)-this.rightExpSearch(array, a, m+1, tmp[i]); - - for(int j = 0; j < l; j++) - Writes.write(array, --b, array[m--], 1, true, false); - Writes.write(array, --b, tmp[i--], 1, true, false); - - if(i < 0) return; - - else if(m < a) { - while(i >= 0) Writes.write(array, --b, tmp[i--], 1, true, false); - return; - } - - this.highlight = t; - r = (i+1)-this.rightBoundSearch(tmp, 0, i+1, array[m]); - this.highlight = 0; - - for(int j = 0; j < r; j++) - Writes.write(array, --b, tmp[i--], 1, true, false); - Writes.write(array, --b, array[m--], 1, true, false); - - if(i < 0) return; - - else if(m < a) { - while(i >= 0) Writes.write(array, --b, tmp[i--], 1, true, false); - return; - } - } - while((l | r) >= M); - - if(mGallop < 0) mGallop = 0; - mGallop += 2; - } - } - - private void smartMerge(int[] array, int[] tmp, int a, int m, int b) { - if(Reads.compareValues(array[m-1], array[m]) <= 0) return; - - a = this.leftBoundSearch(array, a, m, array[m]); - b = this.rightBoundSearch(array, m, b, array[m-1]); - - if(b-m < m-a) this.mergeBW(array, tmp, a, m, b); - else this.mergeFW(array, tmp, a, m, b); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int[] tmp = Writes.createExternalArray(length/2); - - int mRun = length; - for(; mRun >= 32; mRun = (mRun+1)/2); - - this.buildRuns(array, 0, length, mRun); - - for(int i, j = mRun; j < length; j *= 2) { - for(i = 0; i+2*j <= length; i += 2*j) - this.smartMerge(array, tmp, i, i+j, i+2*j); - - if(i+j < length) this.smartMerge(array, tmp, i, i+j, length); - } - Writes.deleteExternalArray(tmp); - } -} \ No newline at end of file diff --git a/src/sorts/hybrid/StupidQuickSort.java b/src/sorts/hybrid/StupidQuickSort.java deleted file mode 100644 index 5c5683a5..00000000 --- a/src/sorts/hybrid/StupidQuickSort.java +++ /dev/null @@ -1,90 +0,0 @@ -package sorts.hybrid; - -import sorts.templates.Sort; -import sorts.insert.InsertionSort; -import main.ArrayVisualizer; - -/* - * -MIT License -Copyright (c) 2020 fungamer2 & EilrahcF -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 StupidQuickSort extends Sort { - - public StupidQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stupid Quick"); - this.setRunAllSortsName("Stupid Quick Sort"); - this.setRunSortName("Stupid Quicksort"); - this.setCategory("Hybrid 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) { - int times = (int)Math.sqrt(length); - for (int count = 0; count < times; count++) { - int i = 0; - int j = length - 1; - - int pivotPos = (int)(Math.random() * length); - int pivot = array[pivotPos]; - - while (i < j) { - while (Reads.compareValues(array[i], pivot) == -1){ - i++; - Highlights.markArray(1, i); - Delays.sleep(1); - } - while (Reads.compareValues(array[j], pivot) == 1){ - j--; - Highlights.markArray(2, j); - Delays.sleep(1); - } - - if (i < j) { - // Follow the pivot and highlight it. - if(i == pivotPos) { - Highlights.markArray(3, j); - } - if(j == pivotPos) { - Highlights.markArray(3, i); - } - - Writes.swap(array, i, j, 1, true, false); - - i++; - j--; - } - } - } - - Highlights.clearMark(2); - Highlights.clearMark(3); - InsertionSort insertSorter = new InsertionSort(arrayVisualizer); - insertSorter.customInsertSort(array, 0, length, 0.4, false); - } -} diff --git a/src/sorts/hybrid/SwapMergeSort.java b/src/sorts/hybrid/SwapMergeSort.java deleted file mode 100644 index ef0097ee..00000000 --- a/src/sorts/hybrid/SwapMergeSort.java +++ /dev/null @@ -1,108 +0,0 @@ -package sorts.hybrid; - -import main.ArrayVisualizer; -import sorts.insert.BinaryInsertionSort; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 SwapMergeSort extends Sort { - private BinaryInsertionSort binaryInserter; - - public SwapMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("SwapMerge"); - this.setRunAllSortsName("SwapMerge Sort"); - this.setRunSortName("SwapMergeSort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void moveDown(int[] array, int start, int dest) { - for (int i = dest; i < start; i++) { - Writes.swap(array, i, start, 0.025, true, false); - } - } - - private void merge(int[] array, int leftStart, int rightStart, int end) { - int left = leftStart; - int right = rightStart; - - while (left < right) { - if (left >= end || right >= end) { - break; - } - else if (Reads.compareValues(array[left], array[right]) <= 0) { - left += 1; - } - else { - moveDown(array, right, left); - left += 1; - right += 1; - } - } - } - - private void mergeRun(int[] array, int start, int mid, int end) { - if(start == mid) return; - - mergeRun(array, start, (mid+start)/2, mid); - mergeRun(array, mid, (mid+end)/2, end); - - if(end - start < 32) { - return; - } - else if(end - start == 32) { - binaryInserter.customBinaryInsert(array, start, Math.min(array.length, end + 1), 0.333); - } - else { - merge(array, start, mid, end); - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - binaryInserter = new BinaryInsertionSort(arrayVisualizer); - - if(length < 32) { - binaryInserter.customBinaryInsert(array, 0, length, 0.333); - return; - } - - int start = 0; - int end = length; - int mid = start + ((end - start) / 2); - - mergeRun(array, start, mid, end); - } -} diff --git a/src/sorts/hybrid/ThreadedPDMergeSort.java b/src/sorts/hybrid/ThreadedPDMergeSort.java deleted file mode 100644 index 2c1a26ef..00000000 --- a/src/sorts/hybrid/ThreadedPDMergeSort.java +++ /dev/null @@ -1,156 +0,0 @@ -package sorts.hybrid; - -import java.util.concurrent.locks.ReentrantLock; - -import main.ArrayVisualizer; - -/* - * -MIT License - -Copyright (c) 2021 Gaming32 - -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. - * - */ - -public class ThreadedPDMergeSort extends BinaryPDMergeSort { - final int MAX_THREADS = 24; - volatile int threadCount; - volatile ReentrantLock countLock; - - public ThreadedPDMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Threaded Pattern-Defeating Merge"); - this.setRunAllSortsName("Threaded Pattern-Defeating Merge Sort"); - this.setRunSortName("Threaded Pattern-Defeating Mergesort"); - this.setCategory("Hybrid Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - protected void mergeUp(int[] array, int start, int mid, int end) { - for (int i = start; i < mid; i++) { - Highlights.markArray(1, i); - Writes.write(copied, i, array[i], 1, false, true); - } - - int bufferPointer = start; - int left = start; - int right = mid; - - while (left < right && right < end) { - Highlights.markArray(2, right); - if (Reads.compareValues(copied[bufferPointer], array[right]) <= 0) - Writes.write(array, left++, copied[bufferPointer++], 1, true, false); - else - Writes.write(array, left++, array[right++], 1, true, false); - } - Highlights.clearMark(2); - - while (left < right) - Writes.write(array, left++, copied[bufferPointer++], 0.5, true, false); - Highlights.clearAllMarks(); - } - - protected void mergeDown(int[] array, int start, int mid, int end) { - for (int i = mid; i < end; i++) { - Highlights.markArray(1, i); - Writes.write(copied, i, array[i], 1, false, true); - } - - int bufferPointer = end - 1; - int left = mid - 1; - int right = end - 1; - - while (right > left && left >= start) { - Highlights.markArray(2, left); - if (Reads.compareValues(copied[bufferPointer], array[left]) >= 0) - Writes.write(array, right--, copied[bufferPointer--], 1, true, false); - else - Writes.write(array, right--, array[left--], 1, true, false); - } - Highlights.clearMark(2); - - while (left < right) - Writes.write(array, right--, copied[bufferPointer--], 0.5, true, false); - Highlights.clearAllMarks(); - } - - protected void merge(int[] array, int start, int mid, int end) { - countLock.lock(); - if (threadCount < MAX_THREADS) { - threadCount++; - new Thread("ThreadedPDMerge-" + threadCount) { - @Override - public void run() { - ThreadedPDMergeSort.super.merge(array, start, mid, end); - countLock.lock(); - threadCount--; - synchronized (countLock) { - countLock.notify(); - } - countLock.unlock(); - } - }.start(); - countLock.unlock(); - return; - } - countLock.unlock(); - super.merge(array, start, mid, end); - } - - public void runSort(int[] array, int length, int bucketCount) { - threadCount = 0; - countLock = new ReentrantLock(); - - int[] runs = findRuns(array, length - 1); - copied = Writes.createExternalArray(length); - - // arrayVisualizer.setHeading("PDMerge -- Merging Runs"); - while (runCount > 1) { - for (int i = 0; i < runCount - 1; i += 2) { - int end = i + 2 >= runCount ? length : (runs[i + 2]); - merge(array, runs[i], runs[i + 1], end); - } - while (threadCount > 0) { - synchronized (countLock) { - try { - countLock.wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - for (int i = 1, j = 2; i < runCount; i++, j+=2, runCount--) { - Writes.write(runs, i, runs[j], 0.5, true, true); - } - } - - // arrayVisualizer.setHeading("Pattern-Defeating Mergesort"); - - Writes.deleteExternalArray(runs); - Writes.deleteExternalArray(copied); - } -} \ No newline at end of file diff --git a/src/sorts/insert/AdaptiveBinaryInsertionSort.java b/src/sorts/insert/AdaptiveBinaryInsertionSort.java deleted file mode 100644 index 566cba1f..00000000 --- a/src/sorts/insert/AdaptiveBinaryInsertionSort.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * - */ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author PiotrGrochowski - * - */ -public final class AdaptiveBinaryInsertionSort extends Sort { - - /** - * @param arrayVisualizer - */ - public AdaptiveBinaryInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Adaptive Binary Insert"); - this.setRunAllSortsName("Adaptive Binary Insertion Sort"); - this.setRunSortName("Adaptive Binary Insertsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - protected void abinaryinsert(int[] array, int start, int end, double sleep) { - int count = 0; - for (int i = start + 1; i < end; i++) { - int num = array[i]; - int v = (2*count / (i - start)) + 1; //I'VE SOLVED IT!! - int lo = Math.max(i - v, start), hi = i; - while ((lo >= start) && (Reads.compareValues(array[lo], num) == 1)){ - lo -= v; - hi -= v; - } - lo++; - if (lo < start){ - lo = start; - } - while (lo < hi) { - int mid = lo + ((hi - lo) / 2); // avoid int overflow! - Highlights.markArray(2, mid); - - Delays.sleep(sleep); - - if (Reads.compareValues(num, array[mid]) < 0) { // do NOT move equal elements to right of inserted element; this maintains stability! - hi = mid; - } - else { - lo = mid + 1; - } - } - - // item has to go into position lo - count += (i - lo); - - int j = i - 1; - - if (j >= lo){ - while (j >= lo) - { - Writes.write(array, j + 1, array[j], sleep, true, false); - j--; - } - Writes.write(array, lo, num, sleep, true, false); - } - - Highlights.clearAllMarks(); - } - } - - public void customSort(int[] array, int start, int end) { - this.abinaryinsert(array, start, end, 1); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - this.abinaryinsert(array, 0, sortLength, 0.0875); - } - -} diff --git a/src/sorts/insert/AdaptiveInsertionSort.java b/src/sorts/insert/AdaptiveInsertionSort.java deleted file mode 100644 index 76622a84..00000000 --- a/src/sorts/insert/AdaptiveInsertionSort.java +++ /dev/null @@ -1,79 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class AdaptiveInsertionSort extends Sort { - public AdaptiveInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Adaptive Insertion"); - this.setRunAllSortsName("Adaptive Insertion Sort"); - this.setRunSortName("Adaptive Insertsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int findRun(int[] array, int a, int b, double sleep, boolean auxwrite) { - int i = a + 1; - if(Reads.compareIndices(array, i - 1, i++, sleep, true) == 1) { - while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) == 1) i++; - Writes.reversal(array, a, i - 1, sleep, true, false); - } - else while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) <= 0) i++; - Highlights.clearMark(2); - return i; - } - - public void moveFront(int[] array, int a, int m, int b, double sleep, boolean auxwrite) { - int mini = m; - int minv = array[mini]; - Highlights.markArray(2, mini); - for (int i = m + 1; i < b; i++) { - if (Reads.compareIndexValue(array, i, minv, sleep, true) < 0) { - mini = i; - minv = array[i]; - Highlights.markArray(2, mini); - } - } - Highlights.clearMark(2); - m--; - while (mini > m) { - Writes.write(array, mini, array[mini - 1], sleep, true, false); - mini--; - } - --a; - while(mini > a && Reads.compareValues(array[mini], minv) > 0) { - Writes.write(array, mini + 1, array[mini], sleep, true, auxwrite); - mini--; - } - Writes.write(array, mini + 1, minv, sleep, true, auxwrite); - } - - public void insertionSort(int[] array, int a, int b, double sleep, boolean auxwrite) { - int i = findRun(array, a, b, sleep, auxwrite); - if (i < b) { - moveFront(array, a, i++, b, sleep, auxwrite); - while(i < b) { - int current = array[i]; - int pos = i - 1; - while(Reads.compareValues(array[pos], current) > 0) { - Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); - pos--; - } - Writes.write(array, pos + 1, current, sleep, true, auxwrite); - i++; - } - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - insertionSort(array, 0, currentLength, 1, false); - } -} \ No newline at end of file diff --git a/src/sorts/insert/BidirectionalInsertionSort.java b/src/sorts/insert/BidirectionalInsertionSort.java deleted file mode 100644 index b43396bd..00000000 --- a/src/sorts/insert/BidirectionalInsertionSort.java +++ /dev/null @@ -1,97 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 (Josiah Glosson) - -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 BidirectionalInsertionSort extends Sort { - public BidirectionalInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Bidirectional Insertion"); - this.setRunAllSortsName("Bidirectional Insertion Sort"); - this.setRunSortName("Bidirectional Insertsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - protected void insertFw(int[] array, int i, int current, double sleep, boolean auxwrite) { - int pos = i - 1; - while(Reads.compareValues(array[pos], current) > 0){ - Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); - pos--; - } - Writes.write(array, pos + 1, current, sleep, true, auxwrite); - } - - protected void insertBw(int[] array, int i, int current, double sleep, boolean auxwrite) { - int pos = i - 1; - while(Reads.compareValues(array[pos], current) <= 0){ - Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); - pos--; - } - Writes.write(array, pos + 1, current, sleep, true, auxwrite); - } - - public void insertionSort(int[] array, int a, int b, double sleep, boolean auxwrite) { - boolean dir = true; - for (int i = a + 1; i < b; i++) { - int current = array[i]; - if (dir) { - if (Reads.compareValues(current, array[a]) < 0) { - Writes.reversal(array, a, i - 1, sleep, true, auxwrite); - dir = !dir; - Highlights.clearMark(2); - } else { - insertFw(array, i, current, sleep, auxwrite); - } - } else { - if (Reads.compareValues(current, array[a]) >= 0) { - Writes.reversal(array, a, i - 1, sleep, true, auxwrite); - dir = !dir; - Highlights.clearMark(2); - } else { - insertBw(array, i, current, sleep, auxwrite); - } - } - } - if (!dir) { - Writes.reversal(array, a, b - 1, sleep, true, auxwrite); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - insertionSort(array, 0, currentLength, 0.015, false); - } -} \ No newline at end of file diff --git a/src/sorts/insert/CiuraCocktailShellSort.java b/src/sorts/insert/CiuraCocktailShellSort.java deleted file mode 100644 index 9dd7f3a1..00000000 --- a/src/sorts/insert/CiuraCocktailShellSort.java +++ /dev/null @@ -1,92 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author fungamer2 - * - */ - -public final class CiuraCocktailShellSort extends Sort { - - /** - * @param arrayVisualizer - */ - public CiuraCocktailShellSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Cocktail Shell (Ciura Gaps)"); - this.setRunAllSortsName("Cocktail Shell Sort (Ciura Gaps)"); - this.setRunSortName("Cocktail Shellsort (Ciura Gaps)"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - int[] gaps = {1, 4, 10, 23, 57, 132, 301, 701}; - - private int ciura(int n) { - if (n <= gaps.length) { - return gaps[n - 1]; - } - return (int)Math.pow(2.25, n); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - int gap = 1; - int k; - for (k = 1; gap < sortLength; k++) { - gap = ciura(k); - } - - boolean dir = true; - while (--k >= 1) { - gap = ciura(k); - if (dir) { - for (int i = gap; i < sortLength; i++) { - int tmp = array[i]; - int j = i; - while (j >= gap && Reads.compareValues(array[j - gap], tmp) == 1) { - Highlights.markArray(2, j - gap); - Writes.write(array, j, array[j - gap], 0.7, true, false); - j -= gap; - } - - if (j - gap >= 0) { - Highlights.markArray(2, j - gap); - } else { - Highlights.clearMark(2); - } - - Writes.write(array, j, tmp, 0.7, true, false); - } - } else { - for (int i = sortLength - gap; i >= 0; i--) { - int tmp = array[i]; - int j = i; - while (j < sortLength - gap && Reads.compareValues(array[j + gap], tmp) == -1) { - Highlights.markArray(2, j + gap); - Writes.write(array, j, array[j + gap], 0.7, true, false); - j += gap; - } - - if (j + gap < sortLength) { - Highlights.markArray(2, j + gap); - } else { - Highlights.clearMark(2); - } - - Writes.write(array, j, tmp, 0.7, true, false); - } - } - dir = !dir; - } - - } - -} diff --git a/src/sorts/insert/CocktailShellSort.java b/src/sorts/insert/CocktailShellSort.java deleted file mode 100644 index 2ee3586a..00000000 --- a/src/sorts/insert/CocktailShellSort.java +++ /dev/null @@ -1,77 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author fungamer2 - * - */ -public final class CocktailShellSort extends Sort { - - /** - * @param arrayVisualizer - */ - public CocktailShellSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Cocktail Shell"); - this.setRunAllSortsName("Cocktail Shell Sort"); - this.setRunSortName("Cocktail Shellsort"); - 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 sortLength, int bucketCount) throws Exception { - int gap = sortLength / 2; - boolean dir = true; - while (gap >= 1) { - if (dir) { - for (int i = gap; i < sortLength; i++) { - int tmp = array[i]; - int j = i; - while (j >= gap && Reads.compareValues(array[j - gap], tmp) == 1) { - Highlights.markArray(2, j - gap); - Writes.write(array, j, array[j - gap], 0.7, true, false); - j -= gap; - } - - if (j - gap >= 0) { - Highlights.markArray(2, j - gap); - } else { - Highlights.clearMark(2); - } - - Writes.write(array, j, tmp, 0.7, true, false); - } - } else { - for (int i = sortLength - gap; i >= 0; i--) { - int tmp = array[i]; - int j = i; - while (j < sortLength - gap && this.Reads.compareValues(array[j + gap], tmp) == -1) { - Highlights.markArray(2, j + gap); - Writes.write(array, j, array[j + gap], 0.7, true, false); - j += gap; - } - - if (j + gap < sortLength) { - Highlights.markArray(2, j + gap); - } else { - Highlights.clearMark(2); - } - - Writes.write(array, j, tmp, 0.7, true, false); - } - } - gap /= 2; - dir = !dir; - } - - } - -} diff --git a/src/sorts/insert/FibonacciInsertionSort.java b/src/sorts/insert/FibonacciInsertionSort.java deleted file mode 100644 index eb9d09da..00000000 --- a/src/sorts/insert/FibonacciInsertionSort.java +++ /dev/null @@ -1,99 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License -Copyright (c) 2020 fungamer2 -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 FibonacciInsertionSort extends Sort { - - public FibonacciInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Fibonacci Insertion"); - this.setRunAllSortsName("Fibonacci Insertion Sort"); - this.setRunSortName("Fibonacci Insertion Sort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void fibonacciInsertionSort(int[] array, int length) { - for (int i = 1; i < length; i++) { - int tmp = array[i]; - int position = this.fibonacciSearch(array, 0, i - 1, tmp); - int j = i - 1; - while (j >= position) { - Writes.write(array, j + 1, array[j--], 0.15, true, false); - } - Writes.write(array, j + 1, tmp, 0.15, true, false); - } - } - - public int fibonacciSearch(int[] array, int start, int end, int item) { - int fibM2 = 0; - int fibM1 = 1; - int fibM = 1; - while (fibM <= end - start) { - fibM2 = fibM1; - fibM1 = fibM; - fibM = fibM2 + fibM1; - } - - int offset = start - 1; - - while (fibM > 1) { - - int i = Math.min(offset + fibM2, end); - - Highlights.markArray(1, offset + 1); - Highlights.markArray(2, i); - - if (Reads.compareValues(array[i], item) <= 0) { - fibM = fibM1; - fibM1 = fibM2; - fibM2 = fibM - fibM1; - offset = i; - } else { - fibM = fibM2; - fibM1 -= fibM2; - fibM2 = fibM - fibM1; - } - Delays.sleep(0.6); - } - int position = ++offset; - if (Reads.compareValues(array[position], item) <= 0) { - ++position; - } - return position; - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.fibonacciInsertionSort(array, length); - } -} \ No newline at end of file diff --git a/src/sorts/insert/GambitInsertionSort.java b/src/sorts/insert/GambitInsertionSort.java deleted file mode 100644 index 47556cd8..00000000 --- a/src/sorts/insert/GambitInsertionSort.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * - */ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.insert.InsertionSort; -import sorts.templates.Sort; - -/** - * @author MP - * @author McDude_73 - * @author EilrahcF - * - */ -public final class GambitInsertionSort extends Sort { - - /** - * @param arrayVisualizer - */ - public GambitInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Gambit Insertion"); - setRunAllSortsName("Gambit Insertion Sort"); - setRunSortName("Gambit Insertsort"); - setCategory("Insertion Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(false); - setUnreasonableLimit(0); - 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; - } - - 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; - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - binInsert(array, sortLength, 0, sortLength); - Highlights.clearAllMarks(); - InsertionSort ins = new InsertionSort(arrayVisualizer); - ins.customInsertSort(array, 0, sortLength, 0.1D, true); - - } - -} diff --git a/src/sorts/insert/PatternDefeatingInsertionSort.java b/src/sorts/insert/PatternDefeatingInsertionSort.java deleted file mode 100644 index 34206e80..00000000 --- a/src/sorts/insert/PatternDefeatingInsertionSort.java +++ /dev/null @@ -1,49 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class PatternDefeatingInsertionSort extends Sort { - public PatternDefeatingInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Pattern-Defeating Insertion"); - this.setRunAllSortsName("Pattern-Defeating Insertion Sort"); - this.setRunSortName("Pattern-Defeating Insertsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void insertionSort(int[] array, int a, int b, double sleep, boolean auxwrite) { - int i = a + 1; - if(Reads.compareIndices(array, i - 1, i++, sleep, true) == 1) { - while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) == 1) i++; - Writes.reversal(array, a, i - 1, sleep, true, auxwrite); - } - else while(i < b && Reads.compareIndices(array, i - 1, i, sleep, true) <= 0) i++; - - Highlights.clearMark(2); - - while(i < b) { - int current = array[i]; - int pos = i - 1; - while(pos >= a && Reads.compareValues(array[pos], current) > 0){ - Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite); - pos--; - } - Writes.write(array, pos + 1, current, sleep, true, auxwrite); - - i++; - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - insertionSort(array, 0, currentLength, 1, false); - } -} \ No newline at end of file diff --git a/src/sorts/insert/RendezvousSort.java b/src/sorts/insert/RendezvousSort.java deleted file mode 100644 index c919a4bb..00000000 --- a/src/sorts/insert/RendezvousSort.java +++ /dev/null @@ -1,74 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class RendezvousSort extends Sort { - public RendezvousSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Rendezvous"); - this.setRunAllSortsName("Lancewer's Rendezvous Sort"); - this.setRunSortName("Lancewer's Rendezvous Sort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(4096); - this.setBogoSort(false); - } - - public void rendezvousSort(int[] array, int length){ - int len = length; - int i, j, ticker, f, gap; - while (len >= 1) { - i = 0; - j = len; - - while (length >= j) { - ticker = i; - f = 0; - - while (!(i < 1 || !(Reads.compareIndices(array, j, i, 0.01, true) < 0))) { - Writes.swap(array, i, j, 1, true, false); - f = 1; - gap = 1 + (j - i); - i -= gap; - j -= gap; - } - - i++; - j++; - - if (f == 1){ - len /= 0.25; - i = 0; - j = len; - } - } - - len /= 8; - } - i = 0; - j = 1; - - while (length >= j) { - ticker = i; - - while (!(i < 0 || !(Reads.compareIndices(array, j, i, 0.01, true) < 0))){ - Writes.swap(array, i, j, 1, true, false); - i--; - j--; - } - - i = ticker + 1; - j = ticker + 2; - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.rendezvousSort(array, length - 1); - } -} \ No newline at end of file diff --git a/src/sorts/insert/ReverseInsertionSort.java b/src/sorts/insert/ReverseInsertionSort.java deleted file mode 100644 index 575b31ff..00000000 --- a/src/sorts/insert/ReverseInsertionSort.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * - */ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author McDude73 - * - */ -public final class ReverseInsertionSort extends Sort { - - /** - * @param arrayVisualizer - */ - 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); - } - - @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); - } - } - -} diff --git a/src/sorts/insert/RoomSort.java b/src/sorts/insert/RoomSort.java deleted file mode 100644 index ff592728..00000000 --- a/src/sorts/insert/RoomSort.java +++ /dev/null @@ -1,76 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 RoomSort extends Sort { - double delay = 0.015; - InsertionSort insertionSort; - - public RoomSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Room"); - this.setRunAllSortsName("Room Sort"); - this.setRunSortName("Roomsort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void insertTo(int[] array, int a, int b) { - int val = array[a]; - a--; - while (a >= b && Reads.compareValues(array[a], val) > 0) { - Writes.write(array, a + 1, array[a], delay, true, false); - a--; - } - Writes.write(array, a + 1, val, delay, true, false); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - insertionSort = new InsertionSort(arrayVisualizer); - int roomLen = (int)Math.sqrt(currentLength) + 1; - - int end, i; - for (end = currentLength; end > roomLen; end -= roomLen) { - Highlights.clearAllMarks(); - insertionSort.customInsertSort(array, 0, roomLen, delay, false); - for (i = roomLen; i < end; i++) { - insertTo(array, i, i - roomLen); - } - } - insertionSort.customInsertSort(array, 0, end, delay, false); - } -} \ No newline at end of file diff --git a/src/sorts/insert/ShuffledTreeSort.java b/src/sorts/insert/ShuffledTreeSort.java deleted file mode 100644 index 34ea4b99..00000000 --- a/src/sorts/insert/ShuffledTreeSort.java +++ /dev/null @@ -1,132 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -import java.util.Random; - -/* - * -MIT License - -Copyright (c) 2021 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 ShuffledTreeSort extends Sort { - public ShuffledTreeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Shuffled Tree"); - this.setRunAllSortsName("Shuffled Tree Sort"); - this.setRunSortName("Shuffled Treesort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int idx; - - private void stableSwap(int[] array, int[] keys, int a, int b) { - Writes.swap(array, a, b, 0, true, false); - Writes.swap(keys, a, b, 1, false, true); - } - - private void traverse(int[] array, int[] keys, int[] lower, int[] upper, int r) { - Highlights.markArray(2, r); - Delays.sleep(1); - - if(lower[r] != 0) this.traverse(array, keys, lower, upper, lower[r]); - - Writes.write(keys, this.idx++, r, 1, true, true); - - if(upper[r] != 0) this.traverse(array, keys, lower, upper, upper[r]); - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - int[] keys = Writes.createExternalArray(currentLength); - for(int i = 0; i < currentLength; i++) - Writes.write(keys, i, i, 1, true, true); - - Random random = new Random(); - for(int i = 0; i < currentLength; i++){ - int r = random.nextInt(currentLength - i) + i; - this.stableSwap(array, keys, i, r); - } - - int[] lower = Writes.createExternalArray(currentLength); - int[] upper = Writes.createExternalArray(currentLength); - int[] next; - - for(int i = 1; i < currentLength; i++) { - Highlights.markArray(2, i); - int c = 0; - - while(true) { - Highlights.markArray(1, c); - Delays.sleep(0.5); - - int cmp = Reads.compareValues(array[i], array[c]); - next = (cmp < 0 || (cmp == 0 && Reads.compareOriginalValues(keys[i], keys[c]) < 0)) ? lower : upper; - - if(next[c] == 0) { - Writes.write(next, c, i, 0, false, true); - break; - } - else c = next[c]; - } - } - Highlights.clearMark(2); - - this.idx = 0; - this.traverse(array, keys, lower, upper, 0); - - for(int i = 0; i < currentLength-1; i++) { - Highlights.markArray(2, i); - - if(Reads.compareOriginalValues(i, keys[i]) != 0) { - int t = array[i]; - int j = i, k = keys[i]; - - do { - Writes.write(array, j, array[k], 1, true, false); - Writes.write(keys, j, j, 1, true, true); - - j = k; - k = keys[k]; - } - while(Reads.compareOriginalValues(k, i) != 0); - - Writes.write(array, j, t, 1, true, false); - Writes.write(keys, j, j, 1, true, true); - } - } - - Writes.deleteExternalArray(lower); - Writes.deleteExternalArray(upper); - Writes.deleteExternalArray(keys); - } -} \ No newline at end of file diff --git a/src/sorts/insert/StableHanoiSort.java b/src/sorts/insert/StableHanoiSort.java deleted file mode 100644 index 4844e2d5..00000000 --- a/src/sorts/insert/StableHanoiSort.java +++ /dev/null @@ -1,335 +0,0 @@ -package sorts.insert; - -import java.util.Stack; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * A stable variant of Hanoi Sort. - * For more information on this type of sort, see Javadoc on {@link HanoiSort}. - * - * @author Sam Walko (Anonymous0726) - */ -final public class StableHanoiSort extends Sort { - // main array - private int[] array; - // Length of the array - private int length; - // Auxiliary stacks from the classic Tower of Hanoi problem - private Stack stack2, stack3; - // sp is a "stack pointer" for the main array (which isn't actually a stack) - private int sp; - // Where the unsorted portion of the main array begins - private int unsorted; - // These are used in determining end conditions for the hanoi function - private int target; - private int targetMoves; - - public StableHanoiSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Stable Hanoi"); - this.setRunAllSortsName("Stable Hanoi Sort"); - this.setRunSortName("Stable Hanoi sort"); - this.setCategory("Impractical Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(true); - this.setUnreasonableLimit(32); - this.setBogoSort(false); - } - - /** - * Moves an element (or group of identical elements) from the unsorted portion - * of the main array into stack2, without disturbing previously-moved elements' - * reverse-stable pattern, using the hanoi function. - */ - private void removeFromMainStack() { - Highlights.markArray(1, sp); - target = array[sp]; - - int moves = hanoi(2, true, 1); - int height = getHeight(moves + 1); - targetMoves = moves; - - Highlights.clearAllMarks(); - Highlights.markArray(1, sp); - - if(height % 2 == 1) { // Odd height case: moved to stack3 - unsorted += moveFromMain(stack2, false); - hanoi(3, false, 2); - } else { // Even height case: moved to main stack - // Moving to stack3 in this manner allows us to keep stability - hanoi(1, false, 2); - hanoi(2, false, 2); - unsorted += moveFromMain(stack2, false); - hanoi(3, true, 2); - } - } - - /** - * Stably moves all of the elements, currently in order on stack2, back to - * the main array using the hanoi function. Stability achieved by moving - * each disk, which is initially reverse-stable, an odd number of times. - */ - private void returnToMainStack() { - int moves = hanoi(2, true, 3); - int height = getHeight(moves + 1); - boolean oddHeight = height % 2 == 1; - targetMoves = moves; - - if(oddHeight) { // Odd height case: moved to stack3 - hanoi(3, false, 2); // All disks have moved an even number of times - } else { // Even height case: moved to main stack - targetMoves /= 2; - hanoi(1, false, 2); - hanoi(3, false, 2); - oddHeight = true; - // At this point, all disks but the greatest has moved a multiple of four times - // (an even number), and the greatest disk has moved once (an odd number). - } - // In both cases, there is now an odd number of disks, - // each moved an even number of times, on stack2 - while(!stack2.isEmpty()) { - targetMoves /= 2; // Rounds down to correct integer - hanoi(2, !oddHeight, 2); // Move all but the new largest element to stack3 - moveToMain(stack2); // Move the new smallest element to the main stack - hanoi(3, oddHeight, 2); // Move stack3 back to stack2 - oddHeight = !oddHeight; // Change height's odd parity - } - } - - /** - * Iteratively moves elements around the stacks according to the - * Tower of Hanoi problem. Recursion cannot be used because in many - * cases we do not know the initial recursion depth. - * - * @param startStack Which stack we wish to move a tower from - * @param goRight Whether the smallest disk should go right on the pegs - * (1->2, 2->3, 3->1) or left (1->3, 2->1, 3->2) - * @param endCon Used to determine when to end this function - * @return The number of moves performed - */ - private int hanoi(int startStack, boolean goRight, int endCon) { - int moves = 0; - int minPoleLoc = startStack; - - if(!endConMet(endCon, moves)) { - moves++; - switch(minPoleLoc) { - case 1: - if(goRight) { - moveFromMain(stack2, true); - minPoleLoc = 2; - } else { - moveFromMain(stack3, true); - minPoleLoc = 3; - } break; - case 2: - if(goRight) { - moveBetweenStacks(stack2, stack3); - minPoleLoc = 3; - } else { - moveToMain(stack2); - minPoleLoc = 1; - } break; - case 3: - if(goRight) { - moveToMain(stack3); - minPoleLoc = 1; - } else { - moveBetweenStacks(stack3, stack2); - minPoleLoc = 2; - } break; - } - } - - while(!endConMet(endCon, moves)) { - moves += 2; - switch(minPoleLoc) { - case 1: - if(!stack2.isEmpty() && - (stack3.isEmpty() || Reads.compareValues(stack2.peek(), stack3.peek()) < 0)) - moveBetweenStacks(stack2, stack3); - else - moveBetweenStacks(stack3, stack2); - if(goRight) { - moveFromMain(stack2, true); - minPoleLoc = 2; - } else { - moveFromMain(stack3, true); - minPoleLoc = 3; - } break; - case 2: - if(stack3.isEmpty() || - (sp < unsorted && Reads.compareValues(array[sp], stack3.peek()) < 0)) - moveFromMain(stack3, true); - else - moveToMain(stack3); - if(goRight) { - moveBetweenStacks(stack2, stack3); - minPoleLoc = 3; - } else { - moveToMain(stack2); - minPoleLoc = 1; - } break; - case 3: - if(stack2.isEmpty() || - (sp < unsorted && Reads.compareValues(array[sp], stack2.peek()) < 0)) - moveFromMain(stack2, true); - else - moveToMain(stack2); - if(goRight) { - moveToMain(stack3); - minPoleLoc = 1; - } else { - moveBetweenStacks(stack3, stack2); - minPoleLoc = 2; - } break; - } - } - - return moves; - } - - /** - * Determines whether or not the hanoi function should end now - * - * @param endCon Which ending condition is required for the hanoi function to end - * @param moves the moves completed by the hanoi function so far - * @return Whether or not the end condition has been met - */ - private boolean endConMet(int endCon, int moves) { - if(!validNumberMoves(moves)) - return false; - switch (endCon) { - case 1: return (stack2.isEmpty() || Reads.compareValues(target, stack2.peek()) <= 0); - case 2: return moves == targetMoves; - case 3: return stack2.isEmpty(); - default: throw new IllegalArgumentException(); - } - } - - /** - * @param moves The number of moves completed so far - * @return If the moves is of the form (2^n)-1 - */ - private boolean validNumberMoves(int moves) { - if(moves == 0) - return true; - if(moves % 2 == 0) - return false; - return validNumberMoves(moves/2); - } - - /** - * Figures out the height of the pyramid moved based on number of moves - * - * @param movesPlus1 The number of moves performed, plus one - * @return The height of the pyramid moved (equal to log_2(moves + 1)) - */ - private int getHeight(int movesPlus1) { - if(movesPlus1 == 1) - return 0; - return getHeight(movesPlus1 / 2) + 1; - } - - /** - * Moves an element from the main array to another stack, - * then moves any consecutive duplicates of that element with it - * - * @param stack The stack to move the element(s) from the main array to - * @param checkUnsorted Whether or not it is safe to remove elements in - * the unsorted portion of the main array - * @return duplicates The number of consecutive identical elements - * that were popped off the main array in the current "move" - */ - private int moveFromMain(Stack stack, boolean checkUnsorted) { - int duplicates = 1; - // Move element - Writes.changeAuxWrites(1); - Writes.startLap(); - stack.push(array[sp]); - Writes.stopLap(); - sp++; - Highlights.markArray(1, sp); - Delays.sleep(0.25); - // Move any duplicates (endOnLength indicates the relevant portion of - // the main stack is "empty") - boolean endOnLength = (sp >= length) || (checkUnsorted && sp >= unsorted); - while(!endOnLength && Reads.compareValues(array[sp], stack.peek()) == 0) { - duplicates++; - Writes.changeAuxWrites(1); - Writes.startLap(); - stack.push(array[sp]); - Writes.stopLap(); - sp++; - Highlights.markArray(1, sp); - Delays.sleep(0.25); - endOnLength = (sp >= length) || (checkUnsorted && sp >= unsorted); - } - return duplicates; - } - - /** - * Moves an element to the main array from another stack, - * then moves any consecutive duplicates of that element with it - * - * @param stack The stack to move the element(s) to the main array from - */ - private void moveToMain(Stack stack) { - // Move element - sp--; - Highlights.markArray(1, sp); - Writes.write(array, sp, stack.pop(), 0.25, false, false); - // Move any duplicates - while(!stack.isEmpty() && Reads.compareValues(stack.peek(), array[sp]) == 0) { - sp--; - Highlights.markArray(1, sp); - Writes.write(array, sp, stack.pop(), 0.25, false, false); - } - } - - /** - * Moves an element from one stack to another stack, - * then moves any consecutive duplicates of that element with it - * - * @param from the stack to move the element(s) from - * @param to the stack to move the element(s) to - */ - private void moveBetweenStacks(Stack from, Stack to) { - // Move element - Writes.changeAuxWrites(1); - Writes.startLap(); - to.push(from.pop()); - Writes.stopLap(); - Delays.sleep(0.25); - // Move any duplicates - while(!from.isEmpty() && Reads.compareValues(from.peek(), to.peek()) == 0) { - Writes.changeAuxWrites(1); - Writes.startLap(); - to.push(from.pop()); - Writes.stopLap(); - Delays.sleep(0.25); - } - } - - - @Override - public void runSort(int[] array, int length, int bucketCount) { - // Initialize local variables - this.array = array; - this.length = length; - stack2 = new Stack(); - stack3 = new Stack(); - sp = 0; - unsorted = 0; - - while(unsorted < length) - removeFromMainStack(); - - returnToMainStack(); - } -} diff --git a/src/sorts/insert/TriSearchInsertionSort.java b/src/sorts/insert/TriSearchInsertionSort.java deleted file mode 100644 index 3ce66c8a..00000000 --- a/src/sorts/insert/TriSearchInsertionSort.java +++ /dev/null @@ -1,85 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -TriSearch Insertion Sort 2020 Copyright (C) thatsOven -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -final public class TriSearchInsertionSort extends Sort { - public TriSearchInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("TriSearch Insertion"); - this.setRunAllSortsName("thatsOven's TriSearch Insertion Sort"); - this.setRunSortName("thatsOven's TriSearch Insertion Sort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public int triSearch(int[] arr, int l, int h, int val, double sleep) { - int mid = l + ((h-l) / 2); - Highlights.markArray(0, l); - Highlights.markArray(1, h); - Highlights.markArray(2, mid); - Delays.sleep(sleep); - if (Reads.compareValues(val, arr[l]) < 0) { - return l; - } else { - if (Reads.compareValues(val, arr[h]) < 0) { - if (Reads.compareValues(val, arr[mid]) < 0) { - return this.triSearch(arr, l+1, mid-1, val, sleep); - } else { - return this.triSearch(arr, mid+1, h-1, val, sleep); - } - } else { - return h+1; - } - } - } - - public void triInsertSort(int[] array, int start, int end, double compSleep, double writeSleep) { - for (int i = start+1; i < end; i++) { - int num = array[i]; - int lo = start; - - lo = this.triSearch(array, start, i-1, num, compSleep); - Highlights.clearAllMarks(); - - int j = i - 1; - - while (j >= lo) - { - Writes.write(array, j + 1, array[j], writeSleep, true, false); - j--; - } - Writes.write(array, lo, num, writeSleep, true, false); - - Highlights.clearAllMarks(); - } - } - - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.triInsertSort(array, 0, currentLength, 40, 1); - } -} \ No newline at end of file diff --git a/src/sorts/insert/UnstableInsertionSort.java b/src/sorts/insert/UnstableInsertionSort.java deleted file mode 100644 index d0aa50af..00000000 --- a/src/sorts/insert/UnstableInsertionSort.java +++ /dev/null @@ -1,41 +0,0 @@ -package sorts.insert; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - - -final public class UnstableInsertionSort extends Sort { - public UnstableInsertionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Unstable Insertion"); - this.setRunAllSortsName("Unstable Insertion Sort"); - this.setRunSortName("Unstable Insertion Sort"); - this.setCategory("Insertion Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - public void unstableInsertionSort(int[] array, int start, int end) { - for (int i = start + 1; i < end; ++i) { - if (Reads.compareIndices(array, i, start, 1, true) < 0) { - Writes.swap(array, i, start, 1, true, false); - } - int tmp = array[i]; - int j = i - 1; - for (; Reads.compareValues(array[j], tmp) > 0; --j) { - Writes.write(array, j+1, array[j], 1, true, false); - } - Writes.write(array, j+1, tmp, 1, true, false); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.unstableInsertionSort(array, 0, currentLength); - } -} diff --git a/src/sorts/merge/IndexMergeSort.java b/src/sorts/merge/IndexMergeSort.java deleted file mode 100644 index 5de88f55..00000000 --- a/src/sorts/merge/IndexMergeSort.java +++ /dev/null @@ -1,113 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -The MIT License (MIT) - -Copyright (c) 2021 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 IndexMergeSort extends Sort { - public IndexMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Index Merge"); - this.setRunAllSortsName("Index Merge Sort"); - this.setRunSortName("Index Mergesort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void indexSort(int[] array, int[] idx, int a, int b) { - while(a < b) { - Highlights.markArray(2, a); - - if(Reads.compareOriginalValues(a, idx[a]) != 0) { - int t = array[a]; - int i = a, nxt = idx[a]; - - do { - Writes.write(array, i, array[nxt], 0, true, false); - Writes.write(idx, i, i, 0.5, false, true); - - i = nxt; - nxt = idx[nxt]; - } - while(Reads.compareOriginalValues(nxt, a) != 0); - - Writes.write(array, i, t, 0, true, false); - Writes.write(idx, i, i, 0.5, false, true); - } - a++; - } - } - - private void merge(int[] array, int[] idx, int a, int m, int b) { - int i = a, j = m, c = a; - Highlights.clearAllMarks(); - - while(i < m && j < b) { - if(Reads.compareValues(array[i], array[j]) <= 0) { - Highlights.markArray(1, i); - Writes.write(idx, c++, i++, 0.5, false, true); - } - else { - Highlights.markArray(2, j); - Writes.write(idx, c++, j++, 0.5, false, true); - } - } - - while(i < m) { - Highlights.markArray(1, i); - Writes.write(idx, c++, i++, 0.5, false, true); - } - while(j < b) { - Highlights.markArray(2, j); - Writes.write(idx, c++, j++, 0.5, false, true); - } - - this.indexSort(array, idx, a, b); - } - - private void sort(int[] array, int[] idx, int a, int b) { - if(b-a < 2) return; - - int m = (a+b)/2; - this.sort(array, idx, a, m); - this.sort(array, idx, m, b); - this.merge(array, idx, a, m, b); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int[] idx = Writes.createExternalArray(length); - this.sort(array, idx, 0, length); - Writes.deleteExternalArray(idx); - } -} \ No newline at end of file diff --git a/src/sorts/merge/ModuloMergeSort.java b/src/sorts/merge/ModuloMergeSort.java deleted file mode 100644 index e6152a64..00000000 --- a/src/sorts/merge/ModuloMergeSort.java +++ /dev/null @@ -1,70 +0,0 @@ -package sorts.merge; - -import java.lang.Math; -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class ModuloMergeSort extends Sort { - public ModuloMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Modulo Merge"); - this.setRunAllSortsName("Modulo Merge Sort"); - this.setRunSortName("Modulo Merge Sort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void merge(int[] array, int start, int mid, int end, int maxEl) { - int left = start; - int right = mid + 1; - int finalSpot = start; - - Highlights.clearAllMarks(); - - while (left <= mid && right <= end) { - Highlights.markArray(0, left); - Highlights.markArray(1, right); - Delays.sleep(1); - - if (Reads.compareValues(array[left] % maxEl, array[right] % maxEl) <= 0) { - Writes.write(array, finalSpot, array[finalSpot] + (array[left++] % maxEl) * maxEl, 1, true, false); - } else { - Writes.write(array, finalSpot, array[finalSpot] + (array[right++] % maxEl) * maxEl, 1, true, false); - } - finalSpot++; - } - - Highlights.clearAllMarks(); - - while (left <= mid) { - Writes.write(array, finalSpot, array[finalSpot++] + (array[left++] % maxEl) * maxEl, 1, true, false); - } - while (right <= end) { - Writes.write(array, finalSpot, array[finalSpot++] + (array[right++] % maxEl) * maxEl, 1, true, false); - } - - for (int i = start; i <= end; i++) { - Writes.write(array, i, array[i] / maxEl, 1, true, false); - } - } - - public void mergeSort(int[] array, int start, int end, int maxEl) { - if (start < end) { - int mid = start + ((end - start) / 2); - this.mergeSort(array, start, mid, maxEl); - this.mergeSort(array, mid + 1, end, maxEl); - this.merge(array, start, mid, end, maxEl); - } - } - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - this.mergeSort(array, 0, currentLength - 1, Reads.analyzeMax(array, currentLength, 0.5, true) + 1); - } -} \ No newline at end of file diff --git a/src/sorts/merge/NaturalMergeSort.java b/src/sorts/merge/NaturalMergeSort.java deleted file mode 100644 index 1ad212c0..00000000 --- a/src/sorts/merge/NaturalMergeSort.java +++ /dev/null @@ -1,79 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class NaturalMergeSort extends Sort { - int[] merged; - - public NaturalMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Natural Merge"); - this.setRunAllSortsName("Natural Merge Sort"); - this.setRunSortName("Natural Merge Sort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void merge(int[] arr, int left, int right, int stop) { - boolean first = true; - int index = 0; - int start = 0; - int origRight = right; - int origLeft = left; - while (left < origRight && right < stop) - if (Reads.compareIndices(arr, left, right, 1, true) == 1) { - first = false; - Writes.write(merged, index++, arr[right++], 0, false, true); - } else - if (first) { - start++; - index++; - left++; - } else - Writes.write(merged, index++, arr[left++], 0, false, true); - while (left < origRight) - Writes.write(merged, index++, arr[left++], 0, false, true); - Highlights.clearMark(2); - for (int i = start; i < index; i++) - Writes.write(arr, i + origLeft, merged[i], 1, true, false); - } - - @Override - public void runSort(int[] arr, int length, int bucketCount) { - merged = Writes.createExternalArray(length); - boolean done = false; - int start = 0; - int stop = length - 1; - while (!done) { - int prev = 0; - int left = -1; - done = true; - for (int i = start; i < stop; i++) - if (Reads.compareIndices(arr, i, i + 1, 1, true) == 1) { - if (left == -1) { - left = prev; - prev = i + 1; - } else { - merge(arr, left, prev, i + 1); - if (done) - start = i; - prev = i + 1; - left = -1; - done = false; - } - } - if (left != -1) { - merge(arr, left, prev, length); - done = false; - stop = left; - } - } - Writes.deleteExternalArray(merged); - } -} \ No newline at end of file diff --git a/src/sorts/merge/OptimizedPancakeSort.java b/src/sorts/merge/OptimizedPancakeSort.java deleted file mode 100644 index 69248544..00000000 --- a/src/sorts/merge/OptimizedPancakeSort.java +++ /dev/null @@ -1,132 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -The MIT License (MIT) - -Copyright (c) 2021 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 OptimizedPancakeSort extends Sort { - public OptimizedPancakeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Pancake"); - this.setRunAllSortsName("Optimized Pancake Sort"); - this.setRunSortName("Optimized Pancake Sort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - //special thanks to Anonymous0726 !!! - - private void flip(int[] array, int idx) { - Writes.reversal(array, 0, idx, 0.1, true, false); - } - - private void cursedRotate(int[] array, int a, int m, int b) { - this.flip(array, a-1); - this.flip(array, m-1); - this.flip(array, b-1); - this.flip(array, b-m+a-1); - } - - private int binarySearch(int[] array, int a, int b, int value, boolean left) { - while(a < b) { - int m = a+(b-a)/2; - - boolean comp = left ? Reads.compareValues(value, array[m]) <= 0 - : Reads.compareValues(value, array[m]) < 0; - - if(comp) b = m; - else a = m+1; - } - - return a; - } - - private void pancakeMerge(int[] array, int m, int b) { - int m1, m2, m3; - - if(m >= b-m) { - m1 = m/2; - m2 = this.binarySearch(array, m, b, array[m1], false); - m3 = m1+(m2-m); - } - else { - m2 = m+(b-m)/2; - m1 = this.binarySearch(array, 0, m, array[m2], true); - m3 = (m2++)-(m-m1); - } - this.cursedRotate(array, m1, m, m2); - - if(m1 > 0 && m3 > m1) this.pancakeMerge(array, m1, m3); - - m3++; - if(m2 > m3 && b > m2) { - this.cursedRotate(array, 0, m3, b); - this.pancakeMerge(array, m2-m3, b-m3); - this.cursedRotate(array, 0, b-m3, b); - } - } - - private void pancakeMergeSort(int[] array, int n) { - if(n > 1) { - if(Reads.compareIndices(array, 0, 1, 0, true) <= 0) { - int i = 2; - for(; i < n && Reads.compareIndices(array, i-1, i, 0, true) <= 0; i++); - - if(i == n) return; - } - else { - int i = 2; - for(; i < n && Reads.compareIndices(array, i-1, i, 0, true) > 0; i++); - - if(i == n) { - this.flip(array, n-1); - return; - } - } - - int m = n/2; - - this.pancakeMergeSort(array, m); - this.cursedRotate(array, 0, m, n); - m = n-m; - - this.pancakeMergeSort(array, m); - this.pancakeMerge(array, m, n); - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - this.pancakeMergeSort(array, length); - } -} \ No newline at end of file diff --git a/src/sorts/merge/OutOfPlaceWeaveMergeSort.java b/src/sorts/merge/OutOfPlaceWeaveMergeSort.java deleted file mode 100644 index e4f83eda..00000000 --- a/src/sorts/merge/OutOfPlaceWeaveMergeSort.java +++ /dev/null @@ -1,109 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -final public class OutOfPlaceWeaveMergeSort extends Sort { - - public OutOfPlaceWeaveMergeSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Out-of-Place Weave Merge"); - this.setRunAllSortsName("Out-of-Place Weave Merge (by Control)"); - this.setRunSortName("Out-of-Place Weave Merge"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - - //Implemented by Control - //Looks like weave merge, but it really is a normal merge. - //Why? you might ask... - - - //stats: - //best case: θ(n*log(n)) - //average case: θ(n*log(n)) - //worst case: θ(n*log(n)) - //extra memory: θ(n) - //Stable: Yes - - - private void weave(int[] array, int[] aux, int start, int end) { - - for(int i = 0; i<=end-start; i+=2){ - Writes.write(array, start+i, aux[start+i/2], 0.25, true, false); - } - - for(int i = 1; i<=end-start; i+=2){ - Writes.write(array, start+i, aux[start/2+i/2+end/2+1], 0.25, true, false); - } - - } - - private void merge(int[] array, int[] aux, int start, int end) { - - int i = start; - int j = start+1; - int pointer = start; - - while(i <= end && j <= end){ - - if(Reads.compareValues(array[i],array[j])>0){ - Writes.write(aux, pointer, array[j], 0.05, true, true); - j += 2; - }else{ - Writes.write(aux, pointer, array[i], 0.05, true, true); - i += 2; - } - pointer++; - } - - int k; - - if(i < j) {k = i;} - else {k = j;} - - while(k<=end){ - Writes.write(aux, pointer, array[k], 0.05, true, true); - k += 2; - pointer++; - } - } - - - public void controller(int[] array, int[] aux, int start, int end){ - if(end-start>=2){ - controller(array, aux, start, (end+start-1)/2); - controller(array, aux, (end+start+1)/2, end); - } - weave(array, aux, start, end); - merge(array, aux, start, end); - } - - - @Override - public void runSort(int[] array, int currentLength, int bucketCount) { - - - int [] swaparray = Writes.createExternalArray(currentLength); - for(int i = 0; i= rightStart && right >= end) break; - - Highlights.markArray(1, nxt + leftStart); - Highlights.markArray(2, right); - - if(left < rightStart && right >= end){ - Highlights.clearMark(2); - Writes.write(array, nxt + leftStart, copied[(left++) - leftStart], 1, false, false); - } - else if(left >= rightStart && right < end){ - Highlights.clearMark(1); - Writes.write(array, nxt + leftStart, array[right++], 1, false, false); - } - else if(Reads.compareValues(copied[left - leftStart], array[right]) <= 0){ - Writes.write(array, nxt + leftStart, copied[(left++) - leftStart], 1, false, false); - } - else{ - Writes.write(array, nxt + leftStart, array[right++], 1, false, false); - } - } - - Highlights.clearAllMarks(); - - } - - private void mergeRun(int[] array, int[] copied, int start, int mid, int end) { - if(start == mid) return; - - mergeRun(array, copied, start, (mid+start)/2, mid); - mergeRun(array, copied, mid, (mid+end)/2, end); - - merge(array, copied, start, mid, end); - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int[] copied = Writes.createExternalArray(length/2); - int start = 0; - int end = length; - int mid = start + ((end - start) / 2); - - mergeRun(array, copied, start, mid, end); - Writes.deleteExternalArray(copied); - } -} \ No newline at end of file diff --git a/src/sorts/merge/ReverseLazyStableSort.java b/src/sorts/merge/ReverseLazyStableSort.java deleted file mode 100644 index f41eec8c..00000000 --- a/src/sorts/merge/ReverseLazyStableSort.java +++ /dev/null @@ -1,204 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 ReverseLazyStableSort extends Sort { - private double sleep = 0.5; - private boolean auxarr = false; - - public ReverseLazyStableSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Reverse Lazy Stable"); - this.setRunAllSortsName("Reverse Lazy Stable Sort"); - this.setRunSortName("Reverse Lazy Stable Sort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void rotateLeft(int[] array, int start, int dest, int size) { - int amount = start - dest; - if (size > 1) { - while (amount >= size) { - for (int i = start; i > start - size; i--) { - Writes.swap(array, i - 1, i + size - 1, sleep, !auxarr, auxarr); - } - start -= size; - amount -= size; - } - Highlights.clearMark(2); - if (amount > 0) { - rotateSmart(array, start, dest, size / 2); - rotateSmart(array, start + size / 2, dest + size / 2, size - (size / 2)); - } - } - else { - int tmp = array[start]; - for (int i = start; i > dest; i--) { - Writes.write(array, i, array[i - 1], sleep, !auxarr, auxarr); - } - Writes.write(array, dest, tmp, sleep, !auxarr, auxarr); - } - } - - private int rotateRight(int[] array, int start, int dest, int size) { - int amount = dest - start; - int moved = 0; - if (size > 1) { - while (amount >= size) { - for (int i = start; i < start + size; i++) { - Writes.swap(array, i, i + size, 1, true, false); - } - start += size; - amount -= size; - moved += size; - } - Highlights.clearMark(2); - } - else { - int tmp = array[start]; - for (int i = start; i < dest; i++) { - Writes.write(array, i, array[i + 1], sleep, !auxarr, auxarr); - } - Writes.write(array, dest, tmp, sleep, !auxarr, auxarr); - moved += dest - start; - } - return moved; - } - - public void rotateSmart(int[] array, int start, int dest, int size) { - if (size > start - dest) { - int startDest = start - dest; - int moved = rotateRight(array, dest, start + size - startDest, startDest); - size -= moved; - dest = dest + moved; - start = dest + startDest; - } - if (size > 0) { - rotateLeft(array, start, dest, size); - } - } - - public void rotateCommon(int[] array, int start, int dest, int size, double sleep, boolean auxWrite) { - double tsleep = this.sleep; - boolean tauxarr = this.auxarr; - this.sleep = sleep; - this.auxarr = auxWrite; - if (start > dest) { - this.rotateSmart(array, start, dest, size); - } - else { - this.rotateSmart(array, start + size, start, dest - (start + size)); - } - this.sleep = tsleep; - this.auxarr = tauxarr; - } - - // Copied from BinaryInsertionSorting.java (and slightly modified) - private int binSearch(int[] array, int start, int i, int num) { - int lo = start, hi = i; - - while (lo < hi) { - int mid = lo + ((hi - lo) / 2); // avoid int overflow! - Highlights.markArray(1, lo); - Highlights.markArray(2, mid); - Highlights.markArray(3, hi); - - Delays.sleep(0.2); - - if (Reads.compareValues(num, array[mid]) < 0) { // do NOT move equal elements to right of inserted element; this maintains stability! - hi = mid; - } - else { - lo = mid + 1; - } - } - - Highlights.clearAllMarks(); - return lo; - } - - public void merge(int[] array, int start, int mid, int end) { - int binSearchThreshold = 2; - for (int len = mid - start; len >= 1; len /= 2) { - binSearchThreshold++; - } - while (start < mid && mid < end) { - if (Reads.compareIndices(array, start, mid, 0.2, true) == -1) { - start++; - // if (Reads.compareIndices(array, start, mid, 0.2, true) == -1) { - // start = binSearch(array, start + 1, mid, array[mid]); - // } - int i; - for (i = 0; i < binSearchThreshold; i++) { - if (Reads.compareIndices(array, start, mid, 0.2, true) == -1) - start++; - else break; - } - if (i == binSearchThreshold) { - start = binSearch(array, start, mid, array[mid]); - } - } - if (start >= mid) - break; - - int size = binSearch(array, mid, end, array[start]) - mid; - rotateSmart(array, mid, start, size); - - start += size + 1; - mid += size; - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - for (int i = 0; i < length - 1; i += 2) { - if (Reads.compareIndices(array, i, i + 1, 0.5, true) == 1) { - Writes.swap(array, i, i + 1, 0, true, false); - } - } - - int gap; - for (gap = 4; gap <= length; gap *= 2) { - for (int i = 0; i + gap <= length; i += gap) { - merge(array, i, i + gap / 2, i + gap); - } - } - - if (length - gap / 2 > 0) { - merge(array, 0, gap / 2, length); - } - } -} diff --git a/src/sorts/merge/Split16Merge.java b/src/sorts/merge/Split16Merge.java deleted file mode 100644 index f765af10..00000000 --- a/src/sorts/merge/Split16Merge.java +++ /dev/null @@ -1,103 +0,0 @@ -package sorts.merge; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2020 Gaming32 - -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 Split16Merge extends Sort { - public Split16Merge(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Split-16 Merge"); - this.setRunAllSortsName("Split-16 Merge Sort"); - this.setRunSortName("Split-16 Mergesort"); - this.setCategory("Merge Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - int[] medianOfSixteenSwaps = new int[] { - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16, - 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16, - 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15, 8, 16, - 6, 11, 7, 10, 4, 13, 14, 15, 8, 12, 2, 3, 5, 9, - 2, 5, 8, 14, 3, 9, 12, 15, 6, 7, 10, 11, - 3, 5, 12, 14, 4, 9, 8, 13, - 7, 9, 11, 13, 4, 6, 8, 10, - 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, - 7, 8, 9, 10 - }; - - private void compSwap1(int[] array, int a, int b) { - if (Reads.compareIndices(array, a, b, 1, true) > 0) { - Writes.swap(array, a, b, 0.5, true, false); - } - } - - private void compSwap2(int[] array, int a, int b, int gap, int start) { - if (Reads.compareIndices(array, start+(a*gap), start+(b*gap), 1, true) > 0) { - Writes.swap(array, start+(a*gap), start+(b*gap), 0.5, true, false); - } - } - - private void medianOfSixteen(int[] array, int a, int gap) { - for (int i = 0; i < this.medianOfSixteenSwaps.length; i += 2) - compSwap2(array, this.medianOfSixteenSwaps[i] - 1, this.medianOfSixteenSwaps[i+1] - 1, gap, a); - } - - private void merge(int[] array, int start, int size) { - int gap = size / 16; - for (int i = 0; i < gap; i++) { - medianOfSixteen(array, start + i, gap); - } - for (int subgap = gap / 2; subgap > 0; subgap /= 2) { - for (int i = 0; i < size - subgap; i++) { - compSwap1(array, start + i, start + i + subgap); - } - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - for (int i = 0; i < length - 15; i += 16) { - this.medianOfSixteen(array, i, 1); - } - - int gap; - for (gap = 32; gap <= length; gap *= 2) { - for (int i = 0; i + gap <= length; i += gap) { - merge(array, i, gap); - } - } - } -} diff --git a/src/sorts/quick/CubeRootQuickSort.java b/src/sorts/quick/CubeRootQuickSort.java deleted file mode 100644 index 98867834..00000000 --- a/src/sorts/quick/CubeRootQuickSort.java +++ /dev/null @@ -1,61 +0,0 @@ -package sorts.quick; - -import static java.lang.Math.cbrt; -import main.ArrayVisualizer; -import sorts.templates.Sort; - -public class CubeRootQuickSort extends Sort { - private static double DELAY = 0.2; - public CubeRootQuickSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Cube Root Quick"); - this.setRunAllSortsName("Cube Root Quick Sort"); - this.setRunSortName("Cube Root Quick Sort"); - this.setCategory("Quick Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void sort(int[] arr, int start, int stop) { - int len = stop - start; - if (len >= 2) { - int root = (int) cbrt(len); - int newStart = start + root; - this.sort(arr, start, newStart); - int[] pivots = new int[root]; - Writes.changeAllocAmount(pivots.length); - for (int i = 0; i < root; i++) - Writes.write(pivots, i, i + start, 0, false, true); - for (int i = newStart; i < stop; i++) { - int left = 0, right = root; - while (left < right) { - int mid = (right - left) / 2 + left; - if (Reads.compareIndices(arr, pivots[mid], i, DELAY, true) == 1) - right = mid; - else - left = mid + 1; - } - int pos = i; - for (int j = root - 1; j >= left; j--) { - Writes.swap(arr, pivots[j] + 1, pos, DELAY, true, false); - Writes.swap(arr, pos = pivots[j], pivots[j] + 1, DELAY, true, false); - Writes.write(pivots, j, pivots[j] + 1, 0, false, true); - } - } - this.sort(arr, start, pivots[0]); - for (int i = 1; i < root; i++) - this.sort(arr, pivots[i - 1] + 1, pivots[i]); - this.sort(arr, pivots[root - 1] + 1, stop); - Writes.changeAllocAmount(-pivots.length); - } - } - - @Override - public void runSort(int[] arr, int length, int buckets) { - this.sort(arr, 0, length); - } -} \ No newline at end of file diff --git a/src/sorts/select/AnarchySort.java b/src/sorts/select/AnarchySort.java deleted file mode 100644 index 9d3114dc..00000000 --- a/src/sorts/select/AnarchySort.java +++ /dev/null @@ -1,106 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; -import utils.ArrayVList; - -public final class AnarchySort extends Sort { - - public AnarchySort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - setSortListName("Anarchy"); - setRunAllSortsName("Anarchy Sort (By Lancewer & McDude_73)"); - setRunSortName("Anarchy Sort"); - setCategory("Selection Sorts"); - setComparisonBased(true); - setBucketSort(false); - setRadixSort(false); - setUnreasonablySlow(true); - setUnreasonableLimit(1024); - setBogoSort(false); - - } - - private boolean containsValue(ArrayVList list, int value) { - for (int at = 0; at < list.size(); at++) { - this.Delays.sleep(0.001D); - this.Highlights.markArray(1, at); - - this.Writes.startLap(); - boolean comp = list.get(at) == value; - this.Writes.stopLap(); - - if (comp) - return true; - } - return false; - } - - private void swapper(int[] normalArray, int[] auxiliaryArray, int pos1, int pos2) { - this.Writes.swap(auxiliaryArray, pos1, pos2, 1.75D, true, true); - this.Writes.swap(normalArray, pos1, pos2, 1.75D, true, false); - } - - private void convert(int[] array, int[] aux, int length) { - int i = 0; - int j = 0; - - while (i < length) { - if (this.Reads.compareIndices(array, j, i, 0.001D, true) < 0) { - j = i; - } - i++; - } - - this.Writes.swap(array, 0, j, 1.0D, true, true); - - for (int init = 0; init < length; init++) { - this.Writes.write(aux, init, array[init], 0.001D, true, true); - } - - ArrayVList t2 = Writes.createArrayList(length); - int m = 0; - - while (m < length) { - i = 0; - j = 0; - while (i < length) { - if (this.Reads.compareIndices(array, i, j, 0.001D, true) <= 0 && !containsValue(t2, i)) { - j = i; - } - i++; - } - t2.add(j); - this.Highlights.markArray(1, j); - this.Delays.sleep(1.0D); - - this.Writes.write(array, j, m, 1.0D, true, false); - m++; - } - - Writes.deleteArrayList(t2); - } - - private void sortMainAndAux(int[] array, int[] aux, int length) { - for (int i = 0; i < length; i++) { - while (array[i] != i) { - swapper(array, aux, i, array[i]); - this.Highlights.markArray(3, array[i]); - } - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int[] aux =Writes.createExternalArray(length); - - convert(array, aux, length); - sortMainAndAux(array, aux, length); - - for (int i = 0; i < length; i++) - this.Writes.write(array, i, aux[i], 1.0D, true, false); - Writes.deleteExternalArray(aux); - - } - -} diff --git a/src/sorts/select/AsynchronousSort.java b/src/sorts/select/AsynchronousSort.java index 413b6ff5..e88ded6c 100644 --- a/src/sorts/select/AsynchronousSort.java +++ b/src/sorts/select/AsynchronousSort.java @@ -5,10 +5,10 @@ import sorts.templates.Sort; /* - * + * MIT License -Copyright (c) 2019 w0rthy +Copyright (c) 2021 Gaming32 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -30,10 +30,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class AsynchronousSort extends Sort { +final public class AsynchronousSort extends Sort { public AsynchronousSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Asynchronous"); this.setRunAllSortsName("Asynchronous Sort"); this.setRunSortName("Asynchronous Sort"); @@ -45,7 +45,7 @@ public AsynchronousSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int[] ext = Writes.createExternalArray(length); diff --git a/src/sorts/select/BinomialHeapSort.java b/src/sorts/select/BinomialHeapSort.java index fad97332..0ff8de14 100644 --- a/src/sorts/select/BinomialHeapSort.java +++ b/src/sorts/select/BinomialHeapSort.java @@ -6,7 +6,7 @@ final public class BinomialHeapSort extends Sort { public BinomialHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binomial Heap"); this.setRunAllSortsName("Binomial Heap Sort"); this.setRunSortName("Binomial Heapsort"); @@ -22,40 +22,40 @@ public BinomialHeapSort(ArrayVisualizer arrayVisualizer) { @Override public void runSort(int[] array, int length, int bucketCount) { int maxNode, focus, index, depth; - for (index = 2; index <= length; index+=2){ - maxNode = index; - do{ - focus = maxNode; - for (depth = 1; (focus&depth) == 0; depth*=2){ - if (Reads.compareValues(array[focus-depth-1], array[maxNode-1]) > 0) - maxNode = (focus-depth); - } - if (focus != maxNode){ - Writes.swap(array, focus-1, maxNode-1, 1, true, false); - } - } while (focus != maxNode); + for (index = 2; index <= length; index += 2) { + maxNode = index; + do { + focus = maxNode; + for (depth = 1; (focus & depth) == 0; depth *= 2) { + if (Reads.compareValues(array[focus - depth - 1], array[maxNode - 1]) > 0) + maxNode = (focus - depth); + } + if (focus != maxNode) { + Writes.swap(array, focus - 1, maxNode - 1, 1, true, false); + } + } while (focus != maxNode); } - for (index = length; index > 2; index--){ - maxNode = index; - focus = index; - for (depth = 1; focus != 0; depth*=2){ - if ((focus & depth) != 0) { - if (Reads.compareValues(array[focus-1], array[maxNode-1]) > 0) - maxNode = focus; - focus -= depth; - } - } - if (maxNode != index) { - focus = index; - do{ - Writes.swap(array, focus-1, maxNode-1, 1, true, false); - focus = maxNode; - for (depth = 1; (focus&depth) == 0; depth*=2){ - if (Reads.compareValues(array[focus-depth-1], array[maxNode-1]) > 0) - maxNode = (focus-depth); - } - } while (focus != maxNode); - } + for (index = length; index > 2; index--) { + maxNode = index; + focus = index; + for (depth = 1; focus != 0; depth *= 2) { + if ((focus & depth) != 0) { + if (Reads.compareValues(array[focus - 1], array[maxNode - 1]) > 0) + maxNode = focus; + focus -= depth; + } + } + if (maxNode != index) { + focus = index; + do { + Writes.swap(array, focus - 1, maxNode - 1, 1, true, false); + focus = maxNode; + for (depth = 1; (focus & depth) == 0; depth *= 2) { + if (Reads.compareValues(array[focus - depth - 1], array[maxNode - 1]) > 0) + maxNode = (focus - depth); + } + } while (focus != maxNode); + } } } } \ No newline at end of file diff --git a/src/sorts/select/DequeueSort.java b/src/sorts/select/DequeueSort.java deleted file mode 100644 index 22167873..00000000 --- a/src/sorts/select/DequeueSort.java +++ /dev/null @@ -1,118 +0,0 @@ -package sorts.select; - -import java.util.PriorityQueue; -import java.util.Comparator; -import java.util.LinkedList; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -MIT License - -Copyright (c) 2020 Gaming32 - -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 DequeueSort extends Sort { - public DequeueSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Dequeue"); - this.setRunAllSortsName("Dequeue Sort"); - this.setRunSortName("Dequeuesort"); - this.setCategory("Selection Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void queueAddLeft(LinkedList q, int val) { - Writes.startLap(); - q.addFirst(val); - Writes.stopLap(); - } - - private void queueAddRight(LinkedList q, int val) { - Writes.startLap(); - q.addLast(val); - Writes.stopLap(); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - PriorityQueue> heap = new PriorityQueue<>(sortLength / 2, new Comparator>(){ - @Override - public int compare(LinkedList q1, LinkedList q2) { - return Reads.compareValues(q1.peek(), q2.peek()); - } - }); - - int currentValueRight = array[0]; - int currentValueLeft = array[0]; - LinkedList currentQueue = new LinkedList<>(); - this.queueAddRight(currentQueue, currentValueRight); - Writes.changeAllocAmount(1); - - int qCount = 1; - - for (int i = 1; i < sortLength; i++) { - Highlights.markArray(1, i); - Highlights.markArray(2, qCount); - if (Reads.compareValues(array[i], currentValueRight) >= 0) { - currentValueRight = array[i]; - this.queueAddRight(currentQueue, array[i]); - } - else if (Reads.compareValues(array[i], currentValueLeft) <= 0) { - currentValueLeft = array[i]; - this.queueAddLeft(currentQueue, array[i]); - } - else { - currentValueRight = array[i]; - currentValueLeft = array[i]; - heap.add(currentQueue); - currentQueue = new LinkedList<>(); - this.queueAddRight(currentQueue, array[i]); - qCount++; - } - Writes.changeAuxWrites(1); - Writes.changeAllocAmount(1); - Delays.sleep(1); - } - heap.add(currentQueue); - - int j = 0; - while (qCount > 0) { - Highlights.markArray(2, qCount); - LinkedList first = heap.poll(); - Writes.write(array, j++, first.pop(), 1, true, false); - Writes.changeAllocAmount(-1); - if (first.size() > 0) { - heap.add(first); - } - else { - qCount--; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/select/ForcedStableHeapSort.java b/src/sorts/select/ForcedStableHeapSort.java deleted file mode 100644 index b8901211..00000000 --- a/src/sorts/select/ForcedStableHeapSort.java +++ /dev/null @@ -1,76 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/** - * @author Yuri-chan2007 - * - */ -public final class ForcedStableHeapSort extends Sort { - - public ForcedStableHeapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - this.setSortListName("Forced Stable Heap"); - this.setRunAllSortsName("Forced Stable Heap Sort"); - this.setRunSortName("Forced Stable Heapsort"); - this.setCategory("Selection Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private boolean stableComp(int[] array, int[] key, int a, int b) { - int comp = Reads.compareIndices(array, a, b, 0.0, true); - - return comp > 0 || (comp == 0 && Reads.compareOriginalIndices(key, a, b, 0.0, false) > 0); - } - - private void stableSwap(int[] array, int[] key, int a, int b) { - Writes.swap(array, a, b, 0.0, true, false); - Writes.swap(key, a, b, 1.0, false, true); - } - - private void siftDown(int[] array, int[] key, int root, int dist, int start) { - while (root <= dist / 2) { - int leaf = 2 * root; - if (leaf < dist && this.stableComp(array, key, start + leaf, start + leaf - 1)) { - leaf++; - } - if(this.stableComp(array, key, start + leaf - 1, start + root - 1)) { - this.stableSwap(array, key, start + leaf - 1, start + root - 1); - root = leaf; - } - else break; - } - } - - protected void heapify(int[] array, int[] key, int low, int high) { - int length = high - low; - for (int i = length / 2; i >= 1; i--) { - siftDown(array, key, i, length, low); - } - } - - protected void heapSort(int[] array, int[] key, int start, int length) { - heapify(array, key, start, length); - for (int i = length - start; i > 1; i--) { - this.stableSwap(array, key, start, start + i - 1); - siftDown(array, key, 1, i - 1, start); - } - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) { - int[] key = Writes.createExternalArray(sortLength); - for(int i = 0; i < sortLength; i++) - Writes.write(key, i, i, 0.5, true, true); - heapSort(array, key, 0, sortLength); - Writes.deleteExternalArray(key); - - } - -} diff --git a/src/sorts/select/HeavyHeapSort.java b/src/sorts/select/HeavyHeapSort.java deleted file mode 100644 index 6280ba12..00000000 --- a/src/sorts/select/HeavyHeapSort.java +++ /dev/null @@ -1,66 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.HeapSorting; - -/* - * -Copyright (c) rosettacode.org. -Permission is granted to copy, distribute and/or modify this document -under the terms of the GNU Free Documentation License, Version 1.2 -or any later version published by the Free Software Foundation; -with no Invariant Sections, no Front-Cover Texts, and no Back-Cover -Texts. A copy of the license is included in the section entitled "GNU -Free Documentation License". - * - */ - -final public class HeavyHeapSort extends HeapSorting { - public HeavyHeapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Heavy Heap"); - this.setRunAllSortsName("Heavy Heap Sort"); - this.setRunSortName("Heavy Heapsort"); - this.setCategory("Selection Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void bitReversal(int[] array, int a, int b) { - int len = b-a, m = 0; - int d1 = len>>1, d2 = d1+(d1>>1); - - for(int i = 1; i < len-1; i++) { - int j = d1; - - for( - int k = i, n = d2; - (k&1) == 0; - j -= n, k >>= 1, n >>= 1 - ); - m += j; - if(m > i) Writes.swap(array, a+i, a+m, 1, true, false); - } - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - boolean dir = false; - for (int i = 0; i < length; i++) { - this.heapify(array, i, length, 0.002, dir); - dir = !dir; - } - Writes.changeReversals(1); - for (int i = 1, j = length - 1; i < j; i += 2, j -= 2) { - Writes.swap(array, i, j, 1, true, false); - } - bitReversal(array, 0, length); - bitReversal(array, 0, length / 2); - bitReversal(array, length / 2, length); - } -} \ No newline at end of file diff --git a/src/sorts/select/MinMaxHeapSort.java b/src/sorts/select/MinMaxHeapSort.java index 88756576..80010f6b 100644 --- a/src/sorts/select/MinMaxHeapSort.java +++ b/src/sorts/select/MinMaxHeapSort.java @@ -10,7 +10,7 @@ final public class MinMaxHeapSort extends Sort { public MinMaxHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Min-Max Heap"); this.setRunAllSortsName("Min-Max Heap Sort"); this.setRunSortName("Min-max Heapsort"); @@ -42,7 +42,6 @@ public boolean is_min_level(int index) { } public void downheap(int i) { - int i0 = i; boolean cf; if (this.is_min_level(i)) { cf = false; diff --git a/src/sorts/select/OptimizedLazyHeapSort.java b/src/sorts/select/OptimizedLazyHeapSort.java deleted file mode 100644 index d6a62b06..00000000 --- a/src/sorts/select/OptimizedLazyHeapSort.java +++ /dev/null @@ -1,100 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2021 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 OptimizedLazyHeapSort extends Sort { - public OptimizedLazyHeapSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Optimized Lazy Heap"); - this.setRunAllSortsName("Optimized Lazy Heap Sort"); - this.setRunSortName("Optimized Lazy Heapsort"); - this.setCategory("Selection Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private int findMin(int[] array, int p, int a, int b, int s) { - int min = p; - - for(int i = a; i < b; i += s) - if(Reads.compareIndices(array, i, min, 0.1, true) < 0) - min = i; - - return min; - } - - @Override - public void runSort(int[] array, int length, int bucketCount) { - int s = (int)Math.sqrt(length-1)+1; - - int f = (length-1)%s+1; - int fMin = this.findMin(array, 0, 1, f, 1); - - for(int j = f; j < length; j += s) { - int min = this.findMin(array, j, j+1, j+s, 1); - - if(j != min) Writes.swap(array, j, min, 1, true, false); - } - - for(int j = 0; j < length;) { - int min = this.findMin(array, fMin, f, length, s); - - if(min == fMin) { - if(j != min) Writes.swap(array, j, min, 1, true, false); - if(++j == f) f += s; //check for bounds if last block is < s - - fMin = this.findMin(array, j, j+1, f, 1); - } - else { - if(j == fMin) fMin = this.findMin(array, j+1, j+2, f, 1); - - int nMin = this.findMin(array, j, min+1, min+s, 1); - - if(nMin == j) Writes.swap(array, j, min, 1, true, false); - - else { - Highlights.clearMark(2); - - int t = array[j]; - Writes.write(array, j, array[min], 0.5, true, false); - Writes.write(array, min, array[nMin], 0.5, true, false); - Writes.write(array, nMin, t, 0.5, true, false); - } - - if(++j == f) f += s; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/select/QueueSort.java b/src/sorts/select/QueueSort.java deleted file mode 100644 index c4ad0f92..00000000 --- a/src/sorts/select/QueueSort.java +++ /dev/null @@ -1,105 +0,0 @@ -package sorts.select; - -import java.util.PriorityQueue; -import java.util.Comparator; -import java.util.LinkedList; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -MIT License - -Copyright (c) 2020 Gaming32 - -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 QueueSort extends Sort { - public QueueSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Queue"); - this.setRunAllSortsName("Queue Sort"); - this.setRunSortName("Queuesort"); - this.setCategory("Selection Sorts"); - this.setComparisonBased(true); - this.setBucketSort(false); - this.setRadixSort(false); - this.setUnreasonablySlow(false); - this.setUnreasonableLimit(0); - this.setBogoSort(false); - } - - private void queueAdd(LinkedList q, int val) { - Writes.startLap(); - q.add(val); - Writes.stopLap(); - } - - @Override - public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { - PriorityQueue> heap = new PriorityQueue<>(sortLength / 2, new Comparator>(){ - @Override - public int compare(LinkedList q1, LinkedList q2) { - return Reads.compareValues(q1.peek(), q2.peek()); - } - }); - - int currentValue = array[0]; - LinkedList currentQueue = new LinkedList<>(); - this.queueAdd(currentQueue, currentValue); - heap.add(currentQueue); - Writes.changeAllocAmount(1); - - int qCount = 1; - - for (int i = 1; i < sortLength; i++) { - Highlights.markArray(1, i); - Highlights.markArray(2, qCount); - if (Reads.compareValues(array[i], array[i - 1]) >= 0) { - this.queueAdd(currentQueue, array[i]); - } - else { - currentValue = array[i]; - currentQueue = new LinkedList<>(); - this.queueAdd(currentQueue, array[i]); - heap.add(currentQueue); - qCount++; - } - Writes.changeAuxWrites(1); - Writes.changeAllocAmount(1); - Delays.sleep(1); - } - - int j = 0; - while (qCount > 0) { - Highlights.markArray(2, qCount); - LinkedList first = heap.poll(); - Writes.write(array, j++, first.pop(), 1, true, false); - Writes.changeAllocAmount(-1); - if (first.size() > 0) { - heap.add(first); - } - else { - qCount--; - } - } - } -} \ No newline at end of file diff --git a/src/sorts/select/ReverseSandpaperSort.java b/src/sorts/select/ReverseSandpaperSort.java deleted file mode 100644 index 61b22064..00000000 --- a/src/sorts/select/ReverseSandpaperSort.java +++ /dev/null @@ -1,56 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -MIT License - -Copyright (c) 2021 Gaming32 - -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 ReverseSandpaperSort extends Sort { - public ReverseSandpaperSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Reverse Sandpaper"); - this.setRunAllSortsName("Reverse Sandpaper Sort"); - this.setRunSortName("Reverse Sandpapersort"); - this.setCategory("Selection 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 currentLength, int bucketCount) { - for (int i = 0; i < currentLength - 1; i++) { - for (int j = currentLength - 1; j > i; j--) { - if (Reads.compareIndices(array, i, j, 0.05, true) > 0) { - Writes.swap(array, i, j, 0.05, true, false); - } - } - } - } -} diff --git a/src/sorts/select/ReverseSelectionSort.java b/src/sorts/select/ReverseSelectionSort.java deleted file mode 100644 index 62b9fd03..00000000 --- a/src/sorts/select/ReverseSelectionSort.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * - */ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* - * -MIT License - -Copyright (c) 2021 mingyue12 - -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. - * - */ - -/** - * @author mingyue12 - * - */ -public final class ReverseSelectionSort extends Sort { - - /** - * @param arrayVisualizer - */ - public ReverseSelectionSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - // TODO Auto-generated constructor stub - this.setSortListName("Reverse Selection"); - this.setRunAllSortsName("Reverse Selection Sort"); - this.setRunSortName("Reverse Selection Sort"); - this.setCategory("Selection 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 sortLength, int bucketCount) throws Exception { - // TODO Auto-generated method stub - for (int i = sortLength - 1; i >= 0; i--) { - int highestindex = 0; - - for (int j = 1; j < i + 1; j++) { - Highlights.markArray(2, j); - Delays.sleep(0.01); - - if (Reads.compareValues(array[j], array[highestindex]) == 1){ - highestindex = j; - Highlights.markArray(1, highestindex); - Delays.sleep(0.01); - } - } - Writes.swap(array, i, highestindex, 0.02, true, false); - } - - } - -} diff --git a/src/sorts/select/SandpaperSort.java b/src/sorts/select/SandpaperSort.java deleted file mode 100644 index a3c09e00..00000000 --- a/src/sorts/select/SandpaperSort.java +++ /dev/null @@ -1,57 +0,0 @@ -package sorts.select; - -import main.ArrayVisualizer; -import sorts.templates.Sort; - -/* -MIT License - -Copyright (c) 2020 yuji -Copyright (c) 2021 Gaming32 - -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 SandpaperSort extends Sort { - public SandpaperSort(ArrayVisualizer arrayVisualizer) { - super(arrayVisualizer); - - this.setSortListName("Sandpaper"); - this.setRunAllSortsName("Sandpaper Sort"); - this.setRunSortName("Sandpapersort"); - this.setCategory("Selection 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 currentLength, int bucketCount) { - for (int i = 0; i < currentLength - 1; i++) { - for (int j = i + 1; j < currentLength; j++) { - if (Reads.compareIndices(array, i, j, 0.05, true) > 0) { - Writes.swap(array, i, j, 0.05, true, false); - } - } - } - } -} diff --git a/src/threads/RunConcurrentSorts.java b/src/threads/RunConcurrentSorts.java index 9f028ed2..e3ee318c 100644 --- a/src/threads/RunConcurrentSorts.java +++ b/src/threads/RunConcurrentSorts.java @@ -2,7 +2,28 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.concurrent.*; +import sorts.concurrent.BitonicSortIterative; +import sorts.concurrent.BitonicSortParallel; +import sorts.concurrent.BitonicSortRecursive; +import sorts.concurrent.BoseNelsonSortIterative; +import sorts.concurrent.BoseNelsonSortParallel; +import sorts.concurrent.BoseNelsonSortRecursive; +import sorts.concurrent.CreaseSort; +import sorts.concurrent.DiamondSortIterative; +import sorts.concurrent.DiamondSortRecursive; +import sorts.concurrent.FoldSort; +import sorts.concurrent.MatrixSort; +import sorts.concurrent.MergeExchangeSortIterative; +import sorts.concurrent.OddEvenMergeSortIterative; +import sorts.concurrent.OddEvenMergeSortParallel; +import sorts.concurrent.OddEvenMergeSortRecursive; +import sorts.concurrent.PairwiseMergeSortIterative; +import sorts.concurrent.PairwiseMergeSortRecursive; +import sorts.concurrent.PairwiseSortIterative; +import sorts.concurrent.PairwiseSortRecursive; +import sorts.concurrent.WeaveSortIterative; +import sorts.concurrent.WeaveSortParallel; +import sorts.concurrent.WeaveSortRecursive; import sorts.templates.Sort; /* @@ -51,11 +72,9 @@ final public class RunConcurrentSorts extends MultipleSortThread { private Sort DiamondSortIterative; private Sort DiamondSortRecursive; private Sort OddEvenMergeSortParallel; - private Sort OptimizedOddEvenMergeSort; private Sort PairwiseMergeSortIterative; private Sort PairwiseMergeSortRecursive; private Sort WeaveSortParallel; - private Sort ApollyonSort; public RunConcurrentSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -81,11 +100,9 @@ public RunConcurrentSorts(ArrayVisualizer arrayVisualizer) { DiamondSortIterative = new DiamondSortIterative(this.arrayVisualizer); DiamondSortRecursive = new DiamondSortRecursive(this.arrayVisualizer); OddEvenMergeSortParallel = new OddEvenMergeSortParallel(this.arrayVisualizer); - OptimizedOddEvenMergeSort = new OptimizedOddEvenMergeSort(this.arrayVisualizer); PairwiseMergeSortIterative = new PairwiseMergeSortIterative(this.arrayVisualizer); PairwiseMergeSortRecursive = new PairwiseMergeSortRecursive(this.arrayVisualizer); WeaveSortParallel = new WeaveSortParallel(this.arrayVisualizer); - ApollyonSort = new ApollyonSort(this.arrayVisualizer); } @Override @@ -93,9 +110,7 @@ protected synchronized void executeSortList(int[] array) throws Exception { // Other RunConcurrentSorts.this.runIndividualSort(FoldSort, 0, array, 1024, 1, false); RunConcurrentSorts.this.runIndividualSort(CreaseSort, 0, array, 1024, 1, false); - RunConcurrentSorts.this.runIndividualSort(ApollyonSort, 0, array, 1024, 1, false); RunConcurrentSorts.this.runIndividualSort(MatrixSort, 0, array, 256, 0.667, false); - RunConcurrentSorts.this.runIndividualSort(OptimizedOddEvenMergeSort, 0, array, 1024, 1, false); // Recursive RunConcurrentSorts.this.runIndividualSort(BitonicSortRecursive, 0, array, 1024, 1, false); diff --git a/src/threads/RunDistributionSorts.java b/src/threads/RunDistributionSorts.java index a0d64bfd..c997be90 100644 --- a/src/threads/RunDistributionSorts.java +++ b/src/threads/RunDistributionSorts.java @@ -2,7 +2,24 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.distribute.*; +import sorts.distribute.AmericanFlagSort; +import sorts.distribute.BinaryQuickSortIterative; +import sorts.distribute.BinaryQuickSortRecursive; +import sorts.distribute.ClassicGravitySort; +import sorts.distribute.CountingSort; +import sorts.distribute.FlashSort; +import sorts.distribute.GravitySort; +import sorts.distribute.InPlaceLSDRadixSort; +import sorts.distribute.IndexSort; +import sorts.distribute.LSDRadixSort; +import sorts.distribute.MSDRadixSort; +import sorts.distribute.PigeonholeSort; +import sorts.distribute.ShatterSort; +import sorts.distribute.SimpleShatterSort; +import sorts.distribute.StacklessAmericanFlagSort; +import sorts.distribute.StacklessBinaryQuickSort; +import sorts.distribute.StaticSort; +import sorts.distribute.TimeSort; import sorts.templates.Sort; /* @@ -36,27 +53,20 @@ final public class RunDistributionSorts extends MultipleSortThread { private Sort CountingSort; private Sort PigeonholeSort; private Sort OptimizedPigeonholeSort; - private Sort FeatureSort; private Sort GravitySort; private Sort ClassicGravitySort; private Sort StaticSort; private Sort IndexSort; private Sort AmericanFlagSort; - private Sort DivisorSort; private Sort LSDRadixSort; private Sort InPlaceLSDRadixSort; private Sort MSDRadixSort; - private Sort InPlaceMSDRadixSort; private Sort FlashSort; private Sort BinaryQuickSortIterative; private Sort BinaryQuickSortRecursive; private Sort ShatterSort; private Sort SimpleShatterSort; - private Sort ImmediateShatterSort; private Sort TimeSort; - private Sort LMSDRadixSort; - private Sort OptimizedIndexSort; - private Sort QuickBinaryRadixSort; private Sort StacklessAmericanFlagSort; private Sort StacklessBinaryQuickSort; @@ -67,28 +77,20 @@ public RunDistributionSorts(ArrayVisualizer arrayVisualizer) { CountingSort = new CountingSort(this.arrayVisualizer); PigeonholeSort = new PigeonholeSort(this.arrayVisualizer); - OptimizedPigeonholeSort = new OptimizedPigeonholeSort(this.arrayVisualizer); - FeatureSort = new FeatureSort(this.arrayVisualizer); GravitySort = new GravitySort(this.arrayVisualizer); ClassicGravitySort = new ClassicGravitySort(this.arrayVisualizer); StaticSort = new StaticSort(this.arrayVisualizer); IndexSort = new IndexSort(this.arrayVisualizer); AmericanFlagSort = new AmericanFlagSort(this.arrayVisualizer); - DivisorSort = new DivisorSort(this.arrayVisualizer); LSDRadixSort = new LSDRadixSort(this.arrayVisualizer); InPlaceLSDRadixSort = new InPlaceLSDRadixSort(this.arrayVisualizer); MSDRadixSort = new MSDRadixSort(this.arrayVisualizer); - InPlaceMSDRadixSort = new InPlaceMSDRadixSort(this.arrayVisualizer); FlashSort = new FlashSort(this.arrayVisualizer); BinaryQuickSortIterative = new BinaryQuickSortIterative(this.arrayVisualizer); BinaryQuickSortRecursive = new BinaryQuickSortRecursive(this.arrayVisualizer); ShatterSort = new ShatterSort(this.arrayVisualizer); SimpleShatterSort = new SimpleShatterSort(this.arrayVisualizer); - ImmediateShatterSort = new ImmediateShatterSort(this.arrayVisualizer); TimeSort = new TimeSort(this.arrayVisualizer); - LMSDRadixSort = new LMSDRadixSort(this.arrayVisualizer); - OptimizedIndexSort = new OptimizedIndexSort(this.arrayVisualizer); - QuickBinaryRadixSort = new QuickBinaryRadixSort(this.arrayVisualizer); StacklessAmericanFlagSort = new StacklessAmericanFlagSort(this.arrayVisualizer); StacklessBinaryQuickSort = new StacklessBinaryQuickSort(this.arrayVisualizer); } @@ -98,12 +100,10 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunDistributionSorts.this.runIndividualSort(CountingSort, 0, array, 2048, 1.5, false); RunDistributionSorts.this.runIndividualSort(PigeonholeSort, 0, array, 2048, 1.5, false); RunDistributionSorts.this.runIndividualSort(OptimizedPigeonholeSort, 0, array, 2048, 1.5, false); - RunDistributionSorts.this.runIndividualSort(FeatureSort, 0, array, 2048, 0.75, false); RunDistributionSorts.this.runIndividualSort(GravitySort, 0, array, 1024, 0.5, false); RunDistributionSorts.this.runIndividualSort(ClassicGravitySort, 0, array, 1024, 1, false); RunDistributionSorts.this.runIndividualSort(StaticSort, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(IndexSort, 0, array, 2048, 1, false); - RunDistributionSorts.this.runIndividualSort(OptimizedIndexSort, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(AmericanFlagSort, 128, array, 2048, 0.75, false); RunDistributionSorts.this.runIndividualSort(StacklessAmericanFlagSort, 128, array, 2048, 0.75, false); // RunDistributionSorts.this.runIndividualSort(DivisorSort, 128, array, 2048, 0.5, false); @@ -114,16 +114,12 @@ protected synchronized void executeSortList(int[] array) throws Exception { Sounds.toggleSofterSounds(false); RunDistributionSorts.this.runIndividualSort(MSDRadixSort, 4, array, 2048, 1.25, false); - RunDistributionSorts.this.runIndividualSort(InPlaceMSDRadixSort, 4, array, 2048, 1.25, false); - RunDistributionSorts.this.runIndividualSort(LMSDRadixSort, 4, array, 2048, 1.25, false); RunDistributionSorts.this.runIndividualSort(FlashSort, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(BinaryQuickSortIterative, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(BinaryQuickSortRecursive, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(StacklessBinaryQuickSort, 0, array, 2048, 1, false); - RunDistributionSorts.this.runIndividualSort(QuickBinaryRadixSort, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(ShatterSort, 128, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(SimpleShatterSort, 128, array, 2048, 1, false); - RunDistributionSorts.this.runIndividualSort(ImmediateShatterSort, 0, array, 2048, 1, false); RunDistributionSorts.this.runIndividualSort(TimeSort, 10, array, 512, 0.05, false); } diff --git a/src/threads/RunExchangeSorts.java b/src/threads/RunExchangeSorts.java index 2ba6c467..3fc50ed0 100644 --- a/src/threads/RunExchangeSorts.java +++ b/src/threads/RunExchangeSorts.java @@ -2,7 +2,35 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.exchange.*; +import sorts.exchange.BinaryGnomeSort; +import sorts.exchange.BubbleSort; +import sorts.exchange.CircleSortIterative; +import sorts.exchange.CircleSortRecursive; +import sorts.exchange.ClassicThreeSmoothCombSort; +import sorts.exchange.CocktailShakerSort; +import sorts.exchange.CombSort; +import sorts.exchange.DualPivotQuickSort; +import sorts.exchange.ForcedStableQuickSort; +import sorts.exchange.FunSort; +import sorts.exchange.GnomeSort; +import sorts.exchange.LLQuickSort; +import sorts.exchange.LRQuickSort; +import sorts.exchange.LRQuickSortParallel; +import sorts.exchange.OddEvenSort; +import sorts.exchange.OptimizedBubbleSort; +import sorts.exchange.OptimizedCocktailShakerSort; +import sorts.exchange.OptimizedGnomeSort; +import sorts.exchange.OptimizedStoogeSort; +import sorts.exchange.OptimizedStoogeSortStudio; +import sorts.exchange.SlopeSort; +import sorts.exchange.StableQuickSort; +import sorts.exchange.StableQuickSortParallel; +import sorts.exchange.TableSort; +import sorts.exchange.ThreeSmoothCombSortIterative; +import sorts.exchange.ThreeSmoothCombSortParallel; +import sorts.exchange.ThreeSmoothCombSortRecursive; +import sorts.exchange.UnoptimizedBubbleSort; +import sorts.exchange.UnoptimizedCocktailShakerSort; import sorts.templates.Sort; import utils.Shuffles; @@ -38,7 +66,6 @@ final public class RunExchangeSorts extends MultipleSortThread { private Sort UnoptimizedCocktailShakerSort; private Sort CocktailShakerSort; private Sort OddEvenSort; - private Sort SwapMapSort; private Sort OptimizedStoogeSort; private Sort GnomeSort; private Sort OptimizedGnomeSort; @@ -47,42 +74,22 @@ final public class RunExchangeSorts extends MultipleSortThread { private Sort ThreeSmoothCombSortRecursive; private Sort ThreeSmoothCombSortIterative; private Sort CircleSortRecursive; - private Sort CircleMergeSort; private Sort CircleSortIterative; private Sort LLQuickSort; - private Sort LLQuickSortMiddlePivot; private Sort LRQuickSort; private Sort DualPivotQuickSort; - private Sort MeanQuickSort; private Sort StableQuickSort; - private Sort StableQuickSortMiddlePivot; private Sort ForcedStableQuickSort; - private Sort LazyStableQuickSort; private Sort TableSort; private Sort OptimizedBubbleSort; private Sort OptimizedCocktailShakerSort; private Sort OptimizedStoogeSortStudio; - private Sort ooPQuickSort; private Sort FunSort; private Sort ClassicThreeSmoothCombSort; - private Sort IndexQuickSort; private Sort LRQuickSortParallel; - private Sort ReverseBubbleSort; - private Sort ReverseGnomeSort; private Sort StableQuickSortParallel; - private Sort StacklessQuickSort; private Sort ThreeSmoothCombSortParallel; - private Sort ChinottoSort; - private Sort CocktailGrateSort; - private Sort GrateSort; - private Sort ReverseGrateSort; - private Sort GnomeWeaveHighSort; - private Sort GnomeWeaveLowSort; - private Sort IterativeQuickSort; - private Sort PseudoHeapSort; private Sort SlopeSort; - private Sort TriSearchGnomeSort; - private Sort WiggleSort; public RunExchangeSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -96,7 +103,6 @@ public RunExchangeSorts(ArrayVisualizer arrayVisualizer) { CocktailShakerSort = new CocktailShakerSort(this.arrayVisualizer); OptimizedCocktailShakerSort = new OptimizedCocktailShakerSort(this.arrayVisualizer); OddEvenSort = new OddEvenSort(this.arrayVisualizer); - SwapMapSort = new SwapMapSort(this.arrayVisualizer); OptimizedStoogeSort = new OptimizedStoogeSort(this.arrayVisualizer); OptimizedStoogeSortStudio = new OptimizedStoogeSortStudio(this.arrayVisualizer); FunSort = new FunSort(this.arrayVisualizer); @@ -108,37 +114,17 @@ public RunExchangeSorts(ArrayVisualizer arrayVisualizer) { ThreeSmoothCombSortIterative = new ThreeSmoothCombSortIterative(this.arrayVisualizer); CircleSortRecursive = new CircleSortRecursive(this.arrayVisualizer); CircleSortIterative = new CircleSortIterative(this.arrayVisualizer); - CircleMergeSort = new CircleMergeSort(this.arrayVisualizer); LLQuickSort = new LLQuickSort(this.arrayVisualizer); - LLQuickSortMiddlePivot = new LLQuickSortMiddlePivot(this.arrayVisualizer); LRQuickSort = new LRQuickSort(this.arrayVisualizer); DualPivotQuickSort = new DualPivotQuickSort(this.arrayVisualizer); - MeanQuickSort = new MeanQuickSort(this.arrayVisualizer); StableQuickSort = new StableQuickSort(this.arrayVisualizer); - StableQuickSortMiddlePivot = new StableQuickSortMiddlePivot(this.arrayVisualizer); - ooPQuickSort = new ooPQuicksort(this.arrayVisualizer); ForcedStableQuickSort = new ForcedStableQuickSort(this.arrayVisualizer); - LazyStableQuickSort = new LazyStableQuickSort(this.arrayVisualizer); TableSort = new TableSort(this.arrayVisualizer); ClassicThreeSmoothCombSort = new ClassicThreeSmoothCombSort(this.arrayVisualizer); - IndexQuickSort = new IndexQuickSort(this.arrayVisualizer); LRQuickSortParallel = new LRQuickSortParallel(this.arrayVisualizer); - ReverseBubbleSort = new ReverseBubbleSort(this.arrayVisualizer); - ReverseGnomeSort = new ReverseGnomeSort(this.arrayVisualizer); StableQuickSortParallel = new StableQuickSortParallel(this.arrayVisualizer); - StacklessQuickSort = new StacklessQuickSort(this.arrayVisualizer); ThreeSmoothCombSortParallel = new ThreeSmoothCombSortParallel(this.arrayVisualizer); - ChinottoSort = new ChinottoSort(this.arrayVisualizer); - CocktailGrateSort = new CocktailGrateSort(this.arrayVisualizer); - GrateSort = new GrateSort(this.arrayVisualizer); - ReverseGrateSort = new ReverseGrateSort(this.arrayVisualizer); - GnomeWeaveHighSort = new GnomeWeaveHighSort(this.arrayVisualizer); - GnomeWeaveLowSort = new GnomeWeaveLowSort(this.arrayVisualizer); - IterativeQuickSort = new IterativeQuickSort(this.arrayVisualizer); - PseudoHeapSort = new PseudoHeapSort(this.arrayVisualizer); SlopeSort = new SlopeSort(this.arrayVisualizer); - TriSearchGnomeSort = new TriSearchGnomeSort(this.arrayVisualizer); - WiggleSort = new WiggleSort(this.arrayVisualizer); } @Override @@ -146,27 +132,16 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunExchangeSorts.this.runIndividualSort(UnoptimizedBubbleSort, 0, array, 512, 1.5, false); RunExchangeSorts.this.runIndividualSort(BubbleSort, 0, array, 512, 1.5, false); RunExchangeSorts.this.runIndividualSort(OptimizedBubbleSort, 0, array, 512, 1.5, false); - RunExchangeSorts.this.runIndividualSort(ReverseBubbleSort, 0, array, 512, 1.5, false); RunExchangeSorts.this.runIndividualSort(UnoptimizedCocktailShakerSort, 0, array, 512, 1.25, false); RunExchangeSorts.this.runIndividualSort(CocktailShakerSort, 0, array, 512, 1.25, false); RunExchangeSorts.this.runIndividualSort(OptimizedCocktailShakerSort, 0, array, 512, 1.25, false); - RunExchangeSorts.this.runIndividualSort(ChinottoSort, 0, array, 512, 1.25, false); - RunExchangeSorts.this.runIndividualSort(WiggleSort, 0, array, 512, 1.25, false); RunExchangeSorts.this.runIndividualSort(OddEvenSort, 0, array, 512, 1, false); - RunExchangeSorts.this.runIndividualSort(SwapMapSort, 0, array, 512, 0.125, false); RunExchangeSorts.this.runIndividualSort(OptimizedStoogeSort, 0, array, 512, 1, false); RunExchangeSorts.this.runIndividualSort(OptimizedStoogeSortStudio, 0, array, 512, 1, false); - RunExchangeSorts.this.runIndividualSort(GrateSort, 0, array, 64, 1, false); - RunExchangeSorts.this.runIndividualSort(ReverseGrateSort, 0, array, 128, 1, false); - RunExchangeSorts.this.runIndividualSort(CocktailGrateSort, 0, array, 128, 1.5, false); RunExchangeSorts.this.runIndividualSort(FunSort, 0, array, 256, 2, false); RunExchangeSorts.this.runIndividualSort(GnomeSort, 0, array, 128, 0.025, false); RunExchangeSorts.this.runIndividualSort(OptimizedGnomeSort, 0, array, 128, 0.025, false); RunExchangeSorts.this.runIndividualSort(BinaryGnomeSort, 0, array, 128, 0.025, false); - RunExchangeSorts.this.runIndividualSort(TriSearchGnomeSort, 0, array, 128, 1, false); - RunExchangeSorts.this.runIndividualSort(ReverseGnomeSort, 0, array, 128, 0.025, false); - RunExchangeSorts.this.runIndividualSort(GnomeWeaveHighSort, 0, array, 126, 0.025, false); - RunExchangeSorts.this.runIndividualSort(GnomeWeaveLowSort, 0, array, 126, 0.025, false); RunExchangeSorts.this.runIndividualSort(SlopeSort, 0, array, 128, 0.025, false); RunExchangeSorts.this.runIndividualSort(CombSort, 130, array, 1024, 1, false); RunExchangeSorts.this.runIndividualSort(ThreeSmoothCombSortRecursive, 0, array, 1024, 1.25, false); @@ -175,24 +150,15 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunExchangeSorts.this.runIndividualSort(ClassicThreeSmoothCombSort, 0, array, 1024, 1.25, false); RunExchangeSorts.this.runIndividualSort(CircleSortRecursive, 0, array, 1024, 1, false); RunExchangeSorts.this.runIndividualSort(CircleSortIterative, 0, array, 1024, 1, false); - RunExchangeSorts.this.runIndividualSort(CircleMergeSort, 0, array, 1024, 0.75, false); - RunExchangeSorts.this.runIndividualSort(PseudoHeapSort, 0, array, 1024, 1.5, false); RunExchangeSorts.this.runIndividualSort(LLQuickSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1.5 : 5, false); - RunExchangeSorts.this.runIndividualSort(LLQuickSortMiddlePivot, 0, array, 2048, 1.5, false); RunExchangeSorts.this.runIndividualSort(LRQuickSort, 0, array, 2048, 1, false); RunExchangeSorts.this.runIndividualSort(LRQuickSortParallel, 0, array, 2048, 1, false); RunExchangeSorts.this.runIndividualSort(DualPivotQuickSort, 0, array, 2048, 1, false); - RunExchangeSorts.this.runIndividualSort(StacklessQuickSort, 0, array, 2048, 1, false); - RunExchangeSorts.this.runIndividualSort(IterativeQuickSort, 0, array, 2048, 1, false); // RunExchangeSorts.this.runIndividualSort(MeanQuickSort, 0, array, 2048, 1, false); RunExchangeSorts.this.runIndividualSort(StableQuickSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1 : 6.5, false); - RunExchangeSorts.this.runIndividualSort(StableQuickSortMiddlePivot, 0, array, 2048, 1, false); - RunExchangeSorts.this.runIndividualSort(ooPQuickSort, 0, array, 2048, 1, false); RunExchangeSorts.this.runIndividualSort(StableQuickSortParallel, 0, array, 2048, 1, false); RunExchangeSorts.this.runIndividualSort(ForcedStableQuickSort, 0, array, 2048, 1, false); - RunExchangeSorts.this.runIndividualSort(LazyStableQuickSort, 0, array, 256, 0.5, false); RunExchangeSorts.this.runIndividualSort(TableSort, 0, array, 1024, 0.75, false); - RunExchangeSorts.this.runIndividualSort(IndexQuickSort, 0, array, 1024, 0.75, false); } @Override diff --git a/src/threads/RunHybridSorts.java b/src/threads/RunHybridSorts.java index 3f40fe09..83f69a55 100644 --- a/src/threads/RunHybridSorts.java +++ b/src/threads/RunHybridSorts.java @@ -2,8 +2,38 @@ import main.ArrayVisualizer; import panes.JErrorPane; - -import sorts.hybrid.*; +import sorts.hybrid.AdaptiveGrailSort; +import sorts.hybrid.BinaryMergeSort; +import sorts.hybrid.BufferPartitionMergeSort; +import sorts.hybrid.CocktailMergeSort; +import sorts.hybrid.DropMergeSort; +import sorts.hybrid.EctaSort; +import sorts.hybrid.FlanSort; +import sorts.hybrid.GrailSort; +import sorts.hybrid.HybridCombSort; +import sorts.hybrid.ImprovedBlockSelectionSort; +import sorts.hybrid.IntroCircleSortIterative; +import sorts.hybrid.IntroCircleSortRecursive; +import sorts.hybrid.IntroSort; +import sorts.hybrid.KotaSort; +import sorts.hybrid.LaziestSort; +import sorts.hybrid.MedianMergeSort; +import sorts.hybrid.MergeInsertionSort; +import sorts.hybrid.OptimizedBottomUpMergeSort; +import sorts.hybrid.OptimizedDualPivotQuickSort; +import sorts.hybrid.OptimizedWeaveMergeSort; +import sorts.hybrid.PDQBranchedSort; +import sorts.hybrid.PDQBranchlessSort; +import sorts.hybrid.ParallelBlockMergeSort; +import sorts.hybrid.ParallelGrailSort; +import sorts.hybrid.RemiSort; +import sorts.hybrid.SqrtSort; +import sorts.hybrid.StacklessDualPivotQuickSort; +import sorts.hybrid.StacklessHybridQuickSort; +import sorts.hybrid.TimSort; +import sorts.hybrid.UnstableGrailSort; +import sorts.hybrid.WeaveMergeSort; +import sorts.hybrid.WikiSort; import sorts.templates.Sort; import utils.Shuffles; @@ -37,17 +67,11 @@ final public class RunHybridSorts extends MultipleSortThread { private Sort HybridCombSort; private Sort IntroCircleSortRecursive; private Sort IntroCircleSortIterative; - private Sort PairwiseCircleSort; - private Sort QuickSPSort; private Sort BinaryMergeSort; private Sort MergeInsertionSort; - private Sort SwapMergeSort; - private Sort BaseNMergeSort; private Sort WeaveMergeSort; - private Sort ImprovedWeaveMergeSort; private Sort TimSort; private Sort CocktailMergeSort; - private Sort LazierSort; private Sort LaziestSort; private Sort WikiSort; private Sort GrailSort; @@ -55,36 +79,23 @@ final public class RunHybridSorts extends MultipleSortThread { private Sort SqrtSort; private Sort KotaSort; private Sort EctaSort; - private Sort BufferedMergeSort; - private Sort OOPBufferedMergeSort; - private Sort BlockSelectionMergeSort; private Sort ImprovedBlockSelectionSort; private Sort MedianMergeSort; - private Sort ThirdMergeSort; - private Sort StableThirdMergeSort; private Sort IntroSort; private Sort OptimizedBottomUpMergeSort; private Sort OptimizedDualPivotQuickSort; private Sort OptimizedWeaveMergeSort; - private Sort StupidQuickSort; - private Sort LAQuickSort; - private Sort MedianOfSixteenAdaptiveQuickSort; private Sort StacklessHybridQuickSort; private Sort PDQBranchedSort; private Sort PDQBranchlessSort; private Sort DropMergeSort; - private Sort OptimizedPDMergeSort; - private Sort BinaryPDMergeSort; private Sort FlanSort; - private Sort BubblescanQuickSort; private Sort BufferPartitionMergeSort; private Sort ParallelBlockMergeSort; private Sort ParallelGrailSort; private Sort RemiSort; private Sort StacklessDualPivotQuickSort; private Sort AdaptiveGrailSort; - private Sort BubbleMergeSort; - private Sort ThreadedPDMergeSort; public RunHybridSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -94,18 +105,11 @@ public RunHybridSorts(ArrayVisualizer arrayVisualizer) { HybridCombSort = new HybridCombSort(this.arrayVisualizer); IntroCircleSortRecursive = new IntroCircleSortRecursive(this.arrayVisualizer); IntroCircleSortIterative = new IntroCircleSortIterative(this.arrayVisualizer); - PairwiseCircleSort = new PairwiseCircleSort(this.arrayVisualizer); - QuickSPSort = new QuickSPSort(this.arrayVisualizer); BinaryMergeSort = new BinaryMergeSort(this.arrayVisualizer); MergeInsertionSort = new MergeInsertionSort(this.arrayVisualizer); - SwapMergeSort = new SwapMergeSort(this.arrayVisualizer); - BaseNMergeSort = new BaseNMergeSort(this.arrayVisualizer); WeaveMergeSort = new WeaveMergeSort(this.arrayVisualizer); - ImprovedWeaveMergeSort = new ImprovedWeaveMergeSort(this.arrayVisualizer); TimSort = new TimSort(this.arrayVisualizer); CocktailMergeSort = new CocktailMergeSort(this.arrayVisualizer); - OptimizedPDMergeSort = new OptimizedPDMergeSort(this.arrayVisualizer); - LazierSort = new LazierSort(this.arrayVisualizer); LaziestSort = new LaziestSort(this.arrayVisualizer); WikiSort = new WikiSort(this.arrayVisualizer); GrailSort = new GrailSort(this.arrayVisualizer); @@ -114,34 +118,22 @@ public RunHybridSorts(ArrayVisualizer arrayVisualizer) { KotaSort = new KotaSort(this.arrayVisualizer); EctaSort = new EctaSort(this.arrayVisualizer); FlanSort = new FlanSort(this.arrayVisualizer); - BufferedMergeSort = new BufferedMergeSort(this.arrayVisualizer); - OOPBufferedMergeSort = new OOPBufferedMergeSort(this.arrayVisualizer); - BlockSelectionMergeSort = new BlockSelectionMergeSort(this.arrayVisualizer); ImprovedBlockSelectionSort = new ImprovedBlockSelectionSort(this.arrayVisualizer); MedianMergeSort = new MedianMergeSort(this.arrayVisualizer); - ThirdMergeSort = new QuarterMergeSort(this.arrayVisualizer); - StableThirdMergeSort = new StableQuarterMergeSort(this.arrayVisualizer); IntroSort = new IntroSort(this.arrayVisualizer); OptimizedBottomUpMergeSort = new OptimizedBottomUpMergeSort(this.arrayVisualizer); OptimizedDualPivotQuickSort = new OptimizedDualPivotQuickSort(this.arrayVisualizer); OptimizedWeaveMergeSort = new OptimizedWeaveMergeSort(this.arrayVisualizer); - StupidQuickSort = new StupidQuickSort(this.arrayVisualizer); - LAQuickSort = new LAQuickSort(this.arrayVisualizer); - MedianOfSixteenAdaptiveQuickSort = new MedianOfSixteenAdaptiveQuickSort(this.arrayVisualizer); StacklessHybridQuickSort = new StacklessHybridQuickSort(this.arrayVisualizer); PDQBranchedSort = new PDQBranchedSort(this.arrayVisualizer); PDQBranchlessSort = new PDQBranchlessSort(this.arrayVisualizer); DropMergeSort = new DropMergeSort(this.arrayVisualizer); - BubblescanQuickSort = new BubblescanQuickSort(this.arrayVisualizer); BufferPartitionMergeSort = new BufferPartitionMergeSort(this.arrayVisualizer); ParallelBlockMergeSort = new ParallelBlockMergeSort(this.arrayVisualizer); ParallelGrailSort = new ParallelGrailSort(this.arrayVisualizer); RemiSort = new RemiSort(this.arrayVisualizer); StacklessDualPivotQuickSort = new StacklessDualPivotQuickSort(this.arrayVisualizer); AdaptiveGrailSort = new AdaptiveGrailSort(this.arrayVisualizer); - BinaryPDMergeSort = new BinaryPDMergeSort(this.arrayVisualizer); - BubbleMergeSort = new BubbleMergeSort(this.arrayVisualizer); - ThreadedPDMergeSort = new ThreadedPDMergeSort(this.arrayVisualizer); } @Override @@ -149,21 +141,11 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunHybridSorts.this.runIndividualSort(HybridCombSort, 0, array, 1024, 1, false); RunHybridSorts.this.runIndividualSort(IntroCircleSortRecursive, 0, array, 1024, 1, false); RunHybridSorts.this.runIndividualSort(IntroCircleSortIterative, 0, array, 1024, 1, false); - RunHybridSorts.this.runIndividualSort(PairwiseCircleSort, 0, array, 1024, 1.5, false); - RunHybridSorts.this.runIndividualSort(QuickSPSort, 0, array, 512, 0.4, false); RunHybridSorts.this.runIndividualSort(BinaryMergeSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(MergeInsertionSort, 0, array, 2048, 1.75, false); - RunHybridSorts.this.runIndividualSort(SwapMergeSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1.65 : 6.5, false); - RunHybridSorts.this.runIndividualSort(BaseNMergeSort, 4, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(WeaveMergeSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1.65 : 6.5, false); - RunHybridSorts.this.runIndividualSort(ImprovedWeaveMergeSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1.65 : 6.5, false); RunHybridSorts.this.runIndividualSort(TimSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(CocktailMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(BubbleMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(OptimizedPDMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(BinaryPDMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(ThreadedPDMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(LazierSort, 0, array, 1024, 0.4, false); RunHybridSorts.this.runIndividualSort(LaziestSort, 0, array, 1024, 1, false); RunHybridSorts.this.runIndividualSort(WikiSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(GrailSort, 0, array, 2048, 1, false); @@ -177,22 +159,13 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunHybridSorts.this.runIndividualSort(ParallelGrailSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(FlanSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(RemiSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(BufferedMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(OOPBufferedMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(BlockSelectionMergeSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(ImprovedBlockSelectionSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(MedianMergeSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(BufferPartitionMergeSort, 0, array, 2048, 1, false); - RunHybridSorts.this.runIndividualSort(ThirdMergeSort, 0, array, 2048, 0.75, false); - RunHybridSorts.this.runIndividualSort(StableThirdMergeSort, 0, array, 2048, 0.75, false); - RunHybridSorts.this.runIndividualSort(BubblescanQuickSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(IntroSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(OptimizedBottomUpMergeSort, 0, array, 2048, 1, false); RunHybridSorts.this.runIndividualSort(OptimizedDualPivotQuickSort, 0, array, 2048, 0.75, false); RunHybridSorts.this.runIndividualSort(OptimizedWeaveMergeSort, 0, array, 1024, 0.4, false); - RunHybridSorts.this.runIndividualSort(StupidQuickSort, 0, array, 1024, 1, false); - RunHybridSorts.this.runIndividualSort(LAQuickSort, 0, array, 2048, 0.75, false); - RunHybridSorts.this.runIndividualSort(MedianOfSixteenAdaptiveQuickSort, 0, array, 1024, 0.25, false); RunHybridSorts.this.runIndividualSort(StacklessHybridQuickSort, 0, array, 2048, 0.75, false); RunHybridSorts.this.runIndividualSort(StacklessDualPivotQuickSort, 0, array, 2048, 0.75, false); RunHybridSorts.this.runIndividualSort(PDQBranchedSort, 0, array, 2048, 0.75, false); diff --git a/src/threads/RunImpracticalSorts.java b/src/threads/RunImpracticalSorts.java index c5a471f3..1bf83a2b 100644 --- a/src/threads/RunImpracticalSorts.java +++ b/src/threads/RunImpracticalSorts.java @@ -2,38 +2,30 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.select.BadSort; -import sorts.exchange.StoogeSort; -import sorts.exchange.StableStoogeSort; -import sorts.exchange.QuadStoogeSort; -import sorts.exchange.SillySort; -import sorts.exchange.SlowSort; -import sorts.exchange.SnuffleSort; -import sorts.exchange.FireSort; -import sorts.exchange.ReflectionSort; -import sorts.exchange.StupidFireSort; -import sorts.insert.HanoiSort; -import sorts.insert.StableHanoiSort; -import sorts.exchange.NapoleonSort; -import sorts.distribute.SelectionBogoSort; -import sorts.exchange.BubbleBogoSort; +import sorts.distribute.BogoBogoSort; +import sorts.distribute.BogoSort; +import sorts.distribute.BozoSort; import sorts.distribute.CocktailBogoSort; -import sorts.exchange.MarkovSort; +import sorts.distribute.DeterministicBogoSort; +import sorts.distribute.GuessSort; import sorts.distribute.LessBogoSort; -import sorts.exchange.ExchangeBogoSort; import sorts.distribute.MedianQuickBogoSort; -import sorts.distribute.QuickBogoSort; import sorts.distribute.MergeBogoSort; -import sorts.distribute.SmartGuessSort; -import sorts.distribute.BozoSort; -import sorts.distribute.DeterministicBogoSort; -import sorts.distribute.SmartBogoBogoSort; -import sorts.distribute.SliceBogoSort; -import sorts.distribute.BogoSort; import sorts.distribute.OptimizedGuessSort; +import sorts.distribute.QuickBogoSort; import sorts.distribute.RandomGuessSort; -import sorts.distribute.GuessSort; -import sorts.distribute.BogoBogoSort; +import sorts.distribute.SelectionBogoSort; +import sorts.distribute.SmartBogoBogoSort; +import sorts.distribute.SmartGuessSort; +import sorts.exchange.BubbleBogoSort; +import sorts.exchange.ExchangeBogoSort; +import sorts.exchange.QuadStoogeSort; +import sorts.exchange.SillySort; +import sorts.exchange.SlowSort; +import sorts.exchange.SnuffleSort; +import sorts.exchange.StoogeSort; +import sorts.insert.HanoiSort; +import sorts.select.BadSort; import sorts.templates.Sort; /* @@ -65,17 +57,13 @@ of this software and associated documentation files (the "Software"), to deal final public class RunImpracticalSorts extends MultipleSortThread { private Sort BadSort; private Sort StoogeSort; - private Sort StableStoogeSort; private Sort SillySort; private Sort SlowSort; private Sort SnuffleSort; private Sort HanoiSort; - private Sort StableHanoiSort; - private Sort NapoleonSort; private Sort SelectionBogoSort; private Sort BubbleBogoSort; private Sort CocktailBogoSort; - private Sort MarkovSort; private Sort LessBogoSort; private Sort ExchangeBogoSort; private Sort MedianQuickBogoSort; @@ -85,16 +73,12 @@ final public class RunImpracticalSorts extends MultipleSortThread { private Sort BozoSort; private Sort DeterministicBogoSort; private Sort SmartBogoBogoSort; - private Sort SliceBogoSort; private Sort BogoSort; private Sort OptimizedGuessSort; private Sort RandomGuessSort; private Sort GuessSort; private Sort BogoBogoSort; - private Sort FireSort; private Sort QuadStoogeSort; - private Sort ReflectionSort; - private Sort StupidFireSort; public RunImpracticalSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -103,17 +87,13 @@ public RunImpracticalSorts(ArrayVisualizer arrayVisualizer) { BadSort = new BadSort(this.arrayVisualizer); StoogeSort = new StoogeSort(this.arrayVisualizer); - StableStoogeSort = new StableStoogeSort(this.arrayVisualizer); SillySort = new SillySort(this.arrayVisualizer); SlowSort = new SlowSort(this.arrayVisualizer); SnuffleSort = new SnuffleSort(this.arrayVisualizer); HanoiSort = new HanoiSort(this.arrayVisualizer); - StableHanoiSort = new StableHanoiSort(this.arrayVisualizer); - NapoleonSort = new NapoleonSort(this.arrayVisualizer); SelectionBogoSort = new SelectionBogoSort(this.arrayVisualizer); BubbleBogoSort = new BubbleBogoSort(this.arrayVisualizer); CocktailBogoSort = new CocktailBogoSort(this.arrayVisualizer); - MarkovSort = new MarkovSort(this.arrayVisualizer); LessBogoSort = new LessBogoSort(this.arrayVisualizer); ExchangeBogoSort = new ExchangeBogoSort(this.arrayVisualizer); MedianQuickBogoSort = new MedianQuickBogoSort(this.arrayVisualizer); @@ -123,33 +103,23 @@ public RunImpracticalSorts(ArrayVisualizer arrayVisualizer) { BozoSort = new BozoSort(this.arrayVisualizer); DeterministicBogoSort = new DeterministicBogoSort(this.arrayVisualizer); SmartBogoBogoSort = new SmartBogoBogoSort(this.arrayVisualizer); - SliceBogoSort = new SliceBogoSort(this.arrayVisualizer); BogoSort = new BogoSort(this.arrayVisualizer); OptimizedGuessSort = new OptimizedGuessSort(this.arrayVisualizer); RandomGuessSort = new RandomGuessSort(this.arrayVisualizer); GuessSort = new GuessSort(this.arrayVisualizer); BogoBogoSort = new BogoBogoSort(this.arrayVisualizer); - FireSort = new FireSort(this.arrayVisualizer); QuadStoogeSort = new QuadStoogeSort(this.arrayVisualizer); - ReflectionSort = new ReflectionSort(this.arrayVisualizer); - StupidFireSort = new StupidFireSort(this.arrayVisualizer); } @Override protected synchronized void executeSortList(int[] array) throws Exception { - RunImpracticalSorts.this.runIndividualSort(FireSort, 0, array, 256, 0.25, true); - RunImpracticalSorts.this.runIndividualSort(StupidFireSort, 0, array, 128, 1, true); - RunImpracticalSorts.this.runIndividualSort(ReflectionSort, 0, array, 128, 0.25, true); RunImpracticalSorts.this.runIndividualSort(BadSort, 0, array, 64, 0.0075, true); RunImpracticalSorts.this.runIndividualSort(StoogeSort, 0, array, 64, 0.005, true); - RunImpracticalSorts.this.runIndividualSort(StableStoogeSort, 0, array, 64, 0.005, true); RunImpracticalSorts.this.runIndividualSort(QuadStoogeSort, 0, array, 64, 0.005, true); RunImpracticalSorts.this.runIndividualSort(SillySort, 0, array, 64, 0.5, true); RunImpracticalSorts.this.runIndividualSort(SlowSort, 0, array, 64, 0.5, true); RunImpracticalSorts.this.runIndividualSort(SnuffleSort, 0, array, 64, 0.25, true); RunImpracticalSorts.this.runIndividualSort(HanoiSort, 0, array, 8, 0.025, true); - RunImpracticalSorts.this.runIndividualSort(StableHanoiSort, 0, array, 8, 0.025, true); - RunImpracticalSorts.this.runIndividualSort(NapoleonSort, 0, array, 6, 0.005, true); // Bogosorts Sounds.toggleSofterSounds(true); @@ -157,7 +127,6 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunImpracticalSorts.this.runIndividualSort(SelectionBogoSort, 0, array, 64, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(BubbleBogoSort, 0, array, 40, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(CocktailBogoSort, 0, array, 40, 1e-9, true); - RunImpracticalSorts.this.runIndividualSort(MarkovSort, 0, array, 40, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(LessBogoSort, 0, array, 32, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(ExchangeBogoSort, 0, array, 28, 1e-9, true); // the meh ones @@ -169,7 +138,6 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunImpracticalSorts.this.runIndividualSort(BozoSort, 0, array, 7, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(DeterministicBogoSort, 0, array, 7, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(SmartBogoBogoSort, 0, array, 6, 1e-9, true); - RunImpracticalSorts.this.runIndividualSort(SliceBogoSort, 0, array, 6, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(BogoSort, 0, array, 6, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(OptimizedGuessSort, 0, array, 5, 1e-9, true); RunImpracticalSorts.this.runIndividualSort(RandomGuessSort, 0, array, 5, 1e-9, true); diff --git a/src/threads/RunInsertionSorts.java b/src/threads/RunInsertionSorts.java index 9b5024f2..7511064c 100644 --- a/src/threads/RunInsertionSorts.java +++ b/src/threads/RunInsertionSorts.java @@ -2,7 +2,20 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.insert.*; +import sorts.insert.AATreeSort; +import sorts.insert.AVLTreeSort; +import sorts.insert.BinaryInsertionSort; +import sorts.insert.ClassicTreeSort; +import sorts.insert.DoubleInsertionSort; +import sorts.insert.InsertionSort; +import sorts.insert.LibrarySort; +import sorts.insert.PatienceSort; +import sorts.insert.RecursiveShellSort; +import sorts.insert.ShellSort; +import sorts.insert.ShellSortParallel; +import sorts.insert.SimplifiedLibrarySort; +import sorts.insert.SplaySort; +import sorts.insert.TreeSort; import sorts.templates.Sort; import utils.Shuffles; @@ -36,27 +49,17 @@ final public class RunInsertionSorts extends MultipleSortThread { private Sort InsertionSort; private Sort DoubleInsertionSort; private Sort BinaryInsertionSort; - private Sort TriSearchInsertionSort; - private Sort UnstableInsertionSort; private Sort ShellSort; private Sort RecursiveShellSort; - private Sort RendezvousSort; private Sort SimplifiedLibrarySort; private Sort PatienceSort; private Sort ClassicTreeSort; private Sort AATreeSort; private Sort AVLTreeSort; - private Sort FibonacciInsertionSort; private Sort SplaySort; - private Sort RoomSort; private Sort LibrarySort; - private Sort CocktailShellSort; private Sort ShellSortParallel; - private Sort ShuffledTreeSort; private Sort TreeSort; - private Sort AdaptiveBinaryInsertionSort; - private Sort GambitInsertionSort; - private Sort ReverseInsertionSort; public RunInsertionSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -66,52 +69,32 @@ public RunInsertionSorts(ArrayVisualizer 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); - SimplifiedLibrarySort = new SimplifiedLibrarySort(this.arrayVisualizer); + SimplifiedLibrarySort = new SimplifiedLibrarySort(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); - LibrarySort = new LibrarySort(this.arrayVisualizer); - CocktailShellSort = new CocktailShellSort(this.arrayVisualizer); + LibrarySort = new LibrarySort(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(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(SimplifiedLibrarySort, 0, array, 2048, 1, false); RunInsertionSorts.this.runIndividualSort(LibrarySort, 0, array, 2048, 1, false); RunInsertionSorts.this.runIndividualSort(PatienceSort, 0, array, 2048, 1, false); RunInsertionSorts.this.runIndividualSort(ClassicTreeSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1 : 5, false); RunInsertionSorts.this.runIndividualSort(TreeSort, 0, array, 2048, arrayManager.containsShuffle(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); diff --git a/src/threads/RunMergeSorts.java b/src/threads/RunMergeSorts.java index a65b02ad..df6ff15f 100644 --- a/src/threads/RunMergeSorts.java +++ b/src/threads/RunMergeSorts.java @@ -34,29 +34,20 @@ of this software and associated documentation files (the "Software"), to deal final public class RunMergeSorts extends MultipleSortThread { private Sort MergeSort; private Sort BottomUpMergeSort; - private Sort PartialMergeSort; private Sort TwinSort; - private Sort NaturalMergeSort; private Sort PDMergeSort; private Sort InPlaceMergeSort; private Sort ImprovedInPlaceMergeSort; private Sort LazyStableSort; - private Sort ReverseLazyStableSort; - private Sort ModuloMergeSort; private Sort BlockSwapMergeSort; private Sort RotateMergeSort; private Sort AndreySort; - private Sort SplitSixteenMergeSort; private Sort StrandSort; - private Sort OutOfPlaceWeaveMergeSort; private Sort BufferedStoogeSort; - private Sort IndexMergeSort; private Sort IterativeTopDownMergeSort; private Sort MergeSortParallel; private Sort NewShuffleMergeSort; - private Sort OptimizedPancakeSort; private Sort RotateMergeSortParallel; - private Sort StacklessRotateMergeSort; private Sort WeavedMergeSort; public RunMergeSorts(ArrayVisualizer arrayVisualizer) { @@ -66,29 +57,20 @@ public RunMergeSorts(ArrayVisualizer arrayVisualizer) { MergeSort = new MergeSort(this.arrayVisualizer); BottomUpMergeSort = new BottomUpMergeSort(this.arrayVisualizer); - PartialMergeSort = new PartialMergeSort(this.arrayVisualizer); TwinSort = new TwinSort(this.arrayVisualizer); - NaturalMergeSort = new NaturalMergeSort(this.arrayVisualizer); PDMergeSort = new PDMergeSort(this.arrayVisualizer); InPlaceMergeSort = new InPlaceMergeSort(this.arrayVisualizer); ImprovedInPlaceMergeSort = new ImprovedInPlaceMergeSort(this.arrayVisualizer); - OutOfPlaceWeaveMergeSort = new OutOfPlaceWeaveMergeSort(this.arrayVisualizer); LazyStableSort = new LazyStableSort(this.arrayVisualizer); - ReverseLazyStableSort = new ReverseLazyStableSort(this.arrayVisualizer); - ModuloMergeSort = new ModuloMergeSort(this.arrayVisualizer); BlockSwapMergeSort = new BlockSwapMergeSort(this.arrayVisualizer); RotateMergeSort = new RotateMergeSort(this.arrayVisualizer); AndreySort = new AndreySort(this.arrayVisualizer); - SplitSixteenMergeSort = new Split16Merge(this.arrayVisualizer); StrandSort = new StrandSort(this.arrayVisualizer); BufferedStoogeSort = new BufferedStoogeSort(this.arrayVisualizer); - IndexMergeSort = new IndexMergeSort(this.arrayVisualizer); IterativeTopDownMergeSort = new IterativeTopDownMergeSort(this.arrayVisualizer); MergeSortParallel = new MergeSortParallel(this.arrayVisualizer); NewShuffleMergeSort = new NewShuffleMergeSort(this.arrayVisualizer); - OptimizedPancakeSort = new OptimizedPancakeSort(this.arrayVisualizer); RotateMergeSortParallel = new RotateMergeSortParallel(this.arrayVisualizer); - StacklessRotateMergeSort = new StacklessRotateMergeSort(this.arrayVisualizer); WeavedMergeSort = new WeavedMergeSort(this.arrayVisualizer); } @@ -97,28 +79,20 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunMergeSorts.this.runIndividualSort(MergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(BottomUpMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(MergeSortParallel, 0, array, 2048, 1.5, false); - RunMergeSorts.this.runIndividualSort(PartialMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(IterativeTopDownMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(WeavedMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(TwinSort, 0, array, 2048, 1.5, false); - RunMergeSorts.this.runIndividualSort(NaturalMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(PDMergeSort, 0, array, 2048, 1, false); RunMergeSorts.this.runIndividualSort(InPlaceMergeSort, 0, array, 2048, 1.5, false); RunMergeSorts.this.runIndividualSort(ImprovedInPlaceMergeSort, 0, array, 2048, 1.5, false); - RunMergeSorts.this.runIndividualSort(OutOfPlaceWeaveMergeSort, 0, array, 2048, 1, false); RunMergeSorts.this.runIndividualSort(LazyStableSort, 0, array, 256, 0.2, false); - RunMergeSorts.this.runIndividualSort(ReverseLazyStableSort, 0, array, 256, 0.1, false); - RunMergeSorts.this.runIndividualSort(ModuloMergeSort, 0, array, 2048, 3, false); RunMergeSorts.this.runIndividualSort(BlockSwapMergeSort, 0, array, 256, 0.1, false); RunMergeSorts.this.runIndividualSort(RotateMergeSort, 0, array, 512, 0.2, false); RunMergeSorts.this.runIndividualSort(RotateMergeSortParallel, 0, array, 512, 0.2, false); RunMergeSorts.this.runIndividualSort(AndreySort, 0, array, 2048, 1, false); - RunMergeSorts.this.runIndividualSort(SplitSixteenMergeSort, 0, array, 512, 0.75, false); - RunMergeSorts.this.runIndividualSort(IndexMergeSort, 0, array, 2048, 1, false); RunMergeSorts.this.runIndividualSort(NewShuffleMergeSort, 0, array, 1024, 1, false); RunMergeSorts.this.runIndividualSort(StrandSort, 0, array, 2048, 1, false); RunMergeSorts.this.runIndividualSort(BufferedStoogeSort, 0, array, 256, 0.2, false); - RunMergeSorts.this.runIndividualSort(OptimizedPancakeSort, 0, array, 2048, 1, false); } @Override diff --git a/src/threads/RunQuickSorts.java b/src/threads/RunQuickSorts.java index cefbf41f..ec9c783a 100644 --- a/src/threads/RunQuickSorts.java +++ b/src/threads/RunQuickSorts.java @@ -2,9 +2,6 @@ import main.ArrayVisualizer; import panes.JErrorPane; -import sorts.quick.*; -import sorts.templates.Sort; -import utils.Shuffles; /* * @@ -33,19 +30,15 @@ of this software and associated documentation files (the "Software"), to deal */ final public class RunQuickSorts extends MultipleSortThread { - private Sort CubeRootQuickSort; public RunQuickSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); this.sortCount = 1; this.categoryCount = this.sortCount; - - CubeRootQuickSort = new CubeRootQuickSort(this.arrayVisualizer); } @Override protected synchronized void executeSortList(int[] array) throws Exception { - RunQuickSorts.this.runIndividualSort(CubeRootQuickSort, 0, array, 2048, arrayManager.containsShuffle(Shuffles.RANDOM) ? 1 : 6.5, false); } @Override diff --git a/src/threads/RunSelectionSorts.java b/src/threads/RunSelectionSorts.java index fe95980f..8cf83079 100644 --- a/src/threads/RunSelectionSorts.java +++ b/src/threads/RunSelectionSorts.java @@ -45,14 +45,9 @@ final public class RunSelectionSorts extends MultipleSortThread { private Sort PoplarHeapSort; private Sort TournamentSort; private Sort AsynchronousSort; - private Sort QueueSort; - private Sort DequeueSort; private Sort StableSelectionSort; private Sort BingoSort; private Sort ClassicTournamentSort; - private Sort ReverseSandpaperSort; - private Sort ReverseSelectionSort; - private Sort SandpaperSort; private Sort StableCycleSort; private Sort TriangularHeapSort; @@ -75,13 +70,8 @@ public RunSelectionSorts(ArrayVisualizer arrayVisualizer) { PoplarHeapSort = new PoplarHeapSort(this.arrayVisualizer); TournamentSort = new TournamentSort(this.arrayVisualizer); AsynchronousSort = new AsynchronousSort(this.arrayVisualizer); - QueueSort = new QueueSort(this.arrayVisualizer); - DequeueSort = new DequeueSort(this.arrayVisualizer); BingoSort = new BingoSort(this.arrayVisualizer); ClassicTournamentSort = new ClassicTournamentSort(this.arrayVisualizer); - ReverseSandpaperSort = new ReverseSandpaperSort(this.arrayVisualizer); - ReverseSelectionSort = new ReverseSelectionSort(this.arrayVisualizer); - SandpaperSort = new SandpaperSort(this.arrayVisualizer); StableCycleSort = new StableCycleSort(this.arrayVisualizer); TriangularHeapSort = new TriangularHeapSort(this.arrayVisualizer); } @@ -89,11 +79,8 @@ public RunSelectionSorts(ArrayVisualizer arrayVisualizer) { @Override protected synchronized void executeSortList(int[] array) throws Exception { RunSelectionSorts.this.runIndividualSort(SelectionSort, 0, array, 128, 0.01, false); - RunSelectionSorts.this.runIndividualSort(ReverseSelectionSort, 0, array, 128, 0.01, false); RunSelectionSorts.this.runIndividualSort(DoubleSelectionSort, 0, array, 128, 0.01, false); RunSelectionSorts.this.runIndividualSort(StableSelectionSort, 0, array, 128, 0.5, false); - RunSelectionSorts.this.runIndividualSort(SandpaperSort, 0, array, 128, 0.05, false); - RunSelectionSorts.this.runIndividualSort(ReverseSandpaperSort, 0, array, 128, 0.05, false); RunSelectionSorts.this.runIndividualSort(CycleSort, 0, array, 128, 0.01, false); RunSelectionSorts.this.runIndividualSort(StableCycleSort, 0, array, 128, 0.01, false); RunSelectionSorts.this.runIndividualSort(BingoSort, 0, array, 128, 0.1, false); @@ -109,8 +96,6 @@ protected synchronized void executeSortList(int[] array) throws Exception { RunSelectionSorts.this.runIndividualSort(TournamentSort, 0, array, 2048, 1.5, false); RunSelectionSorts.this.runIndividualSort(ClassicTournamentSort, 0, array, 2048, 1.5, false); RunSelectionSorts.this.runIndividualSort(AsynchronousSort, 0, array, 1024, 1.5, false); - RunSelectionSorts.this.runIndividualSort(QueueSort, 0, array, 2048, 1, false); - RunSelectionSorts.this.runIndividualSort(DequeueSort, 0, array, 2048, 1, false); } @Override