├── .classpath
├── .gitattributes
├── .gitignore
├── .project
├── .settings
├── org.eclipse.jdt.core.prefs
└── org.eclipse.jdt.ui.prefs
├── LICENSE
├── README.md
├── build.xml
├── dist
├── arrayVisualizer.jar
├── build.xml
└── jar-in-jar-loader.zip
├── lib
└── classgraph-4.8.47.jar
└── src
├── SortPrompt.form
├── UtilFrame.form
├── ViewPrompt.form
├── frames
├── ArrayFrame.java
└── UtilFrame.java
├── main
├── ArrayManager.java
├── ArrayVisualizer.java
└── SortAnalyzer.java
├── prompts
├── ShufflePrompt.java
├── SortPrompt.java
└── ViewPrompt.java
├── sorts
├── AmericanFlagSort.java
├── BadSort.java
├── BinaryGnomeSort.java
├── BinaryInsertionSort.java
├── BinaryMergeSort.java
├── BinaryQuickSort.java
├── BitonicSort.java
├── BogoBogoSort.java
├── BogoSort.java
├── BottomUpMergeSort.java
├── BranchedPDQSort.java
├── BranchlessPDQSort.java
├── BubbleBogoSort.java
├── BubbleSort.java
├── CircleSort.java
├── CocktailBogoSort.java
├── CocktailMergeSort.java
├── CocktailShakerSort.java
├── CombSort.java
├── CountingSort.java
├── CycleSort.java
├── DoubleSelectionSort.java
├── DualPivotQuickSort.java
├── ExchangeBogoSort.java
├── FlashSort.java
├── FlippedMinHeapSort.java
├── GnomeSort.java
├── GrailSort.java
├── GravitySort.java
├── HolyGrailSort.java
├── HybridCombSort.java
├── InPlaceLSDRadixSort.java
├── InPlaceMergeSort.java
├── InsertionSort.java
├── IntroCircleSort.java
├── IntroSort.java
├── IterativeBitonicSort.java
├── IterativeOddEvenMergeSort.java
├── IterativePairwiseSort.java
├── LLQuickSort.java
├── LRQuickSort.java
├── LSDRadixSort.java
├── LazyStableSort.java
├── LessBogoSort.java
├── MSDRadixSort.java
├── MaxHeapSort.java
├── MergeSort.java
├── MinHeapSort.java
├── OddEvenMergeSort.java
├── OddEvenSort.java
├── OptimizedDualPivotQuickSort.java
├── PancakeSort.java
├── PatienceSort.java
├── PigeonholeSort.java
├── PoplarHeapSort.java
├── RecursiveBinaryQuickSort.java
├── RecursiveBitonicSort.java
├── RecursiveCombSort.java
├── RecursiveOddEvenMergeSort.java
├── RecursivePairwiseSort.java
├── RotateMergeSort.java
├── SelectionSort.java
├── ShatterSort.java
├── ShellSort.java
├── SillySort.java
├── SimpleShatterSort.java
├── SkaSort_disabled.java
├── SlowSort.java
├── SmartBubbleSort.java
├── SmartCocktailSort.java
├── SmartGnomeSort.java
├── SmoothSort.java
├── SqrtSort.java
├── StableQuickSort.java
├── StoogeSort.java
├── TernaryHeapSort.java
├── TimSort.java
├── TimeSort.java
├── TournamentSort.java
├── TreeSort.java
├── WeakHeapSort.java
├── WeaveMergeSort.java
└── WikiSort.java
├── soundfont
├── SFXFetcher.java
└── sfx.sf2
├── templates
├── BinaryInsertionSorting.java
├── BinaryQuickSorting.java
├── BogoSorting.java
├── CircleSorting.java
├── CombSorting.java
├── Frame.java
├── GrailSorting.java
├── HeapSorting.java
├── HolyGrailSorting.java
├── InsertionSorting.java
├── JEnhancedOptionPane.java
├── JErrorPane.java
├── MergeSorting.java
├── MultipleSortThread.java
├── PDQSorting.java
├── ShatterSorting.java
├── ShellSorting.java
├── Sort.java
├── TimSorting.java
├── Visual.java
└── WikiSorting.java
├── test
└── Tester.java
├── threads
├── RunAllSorts.java
├── RunComparisonSort.java
├── RunConcurrentSorts.java
├── RunDistributionSort.java
├── RunDistributionSorts.java
├── RunExchangeSorts.java
├── RunHybridSorts.java
├── RunImpracticalSorts.java
├── RunInsertionSorts.java
├── RunMergeSorts.java
├── RunMiscellaneousSorts.java
└── RunSelectionSorts.java
├── utils
├── Delays.java
├── Highlights.java
├── Reads.java
├── Renderer.java
├── Shuffles.java
├── Sounds.java
├── Timer.java
└── Writes.java
└── visuals
├── Bars.java
├── Circular.java
├── Hoops.java
├── Mesh.java
├── Pixels.java
└── VisualStyles.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | /bin/
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ArrayVisualizer-master
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.zeroturnaround.eclipse.rebelXmlBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.8
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.release=disabled
13 | org.eclipse.jdt.core.compiler.source=1.8
14 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.ui.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.ui.text.custom_code_templates=
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 w0rthy
4 | Copyright (c) 2020 MusicTheorist
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/dist/arrayVisualizer.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MusicTheorist/ArrayVisualizer/ff9d8ea47b7ea71461e9f2e8bdb142c204ccea14/dist/arrayVisualizer.jar
--------------------------------------------------------------------------------
/dist/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/dist/jar-in-jar-loader.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MusicTheorist/ArrayVisualizer/ff9d8ea47b7ea71461e9f2e8bdb142c204ccea14/dist/jar-in-jar-loader.zip
--------------------------------------------------------------------------------
/lib/classgraph-4.8.47.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MusicTheorist/ArrayVisualizer/ff9d8ea47b7ea71461e9f2e8bdb142c204ccea14/lib/classgraph-4.8.47.jar
--------------------------------------------------------------------------------
/src/sorts/BadSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This example of an O(n^3) sorting algorithm may be found here, written by James Jensen (StriplingWarrayior on StackOverflow):
11 | * https://stackoverflow.com/questions/27389344/is-there-a-sorting-algorithm-with-a-worst-case-time-complexity-of-n3
12 | */
13 |
14 | final public class BadSort extends Sort {
15 | public BadSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Bad");
19 | this.setRunAllID("Bad Sort");
20 | this.setReportSortID("Badsort");
21 | this.setCategory("Selection Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(true);
26 | this.setUnreasonableLimit(4096);
27 | this.isBogoSort(false);
28 | }
29 |
30 | @Override
31 | public void runSort(int[] array, int currentLen, int bucketCount) {
32 | for (int i = 0; i < currentLen; i++) {
33 | int shortest = i;
34 | Delays.sleep(0.05);
35 |
36 | for (int j = i; j < currentLen; j++) {
37 | Highlights.markArray(1, j);
38 | Delays.sleep(0.05);
39 |
40 | boolean isShortest = true;
41 | for (int k = j + 1; k < currentLen; k++) {
42 | Highlights.markArray(2, k);
43 | Delays.sleep(0.05);
44 |
45 | if (Reads.compare(array[j], array[k]) == 1) {
46 | isShortest = false;
47 | break;
48 | }
49 | }
50 | if(isShortest) {
51 | shortest = j;
52 | break;
53 | }
54 | }
55 | Writes.swap(array, i, shortest, 0.05, true, false);
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/src/sorts/BinaryGnomeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class BinaryGnomeSort extends Sort {
10 | public BinaryGnomeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Binary Gnome");
14 | this.setRunAllID("Optimized Gnome Sort + Binary Search");
15 | this.setReportSortID("Optimized Gnomesort + Binary Search");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | @Override
26 | public void runSort(int[] array, int length, int bucketCount) {
27 | for (int i = 1; i < length; i++) {
28 | int num = array[i];
29 |
30 | int lo = 0, hi = i;
31 | while (lo < hi) {
32 | int mid = lo + ((hi - lo) / 2);
33 |
34 | Highlights.markArray(1, lo);
35 | Highlights.markArray(3, mid);
36 | Highlights.markArray(2, hi);
37 |
38 | Delays.sleep(1);
39 |
40 | if (Reads.compare(num, array[mid]) < 0) { // do NOT shift equal elements past each other; this maintains stability!
41 | hi = mid;
42 | }
43 | else {
44 | lo = mid + 1;
45 | }
46 | }
47 |
48 | // item has to go into position lo
49 |
50 | Highlights.clearMark(1);
51 | Highlights.clearMark(2);
52 |
53 | int j = i;
54 | while (j > lo) {
55 | Writes.swap(array, j, j - 1, 0.05, true, false);
56 | j--;
57 | }
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/src/sorts/BinaryInsertionSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BinaryInsertionSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class BinaryInsertionSort extends BinaryInsertionSorting {
36 | public BinaryInsertionSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Binary Insert");
40 | this.setRunAllID("Binary Insertion Sort");
41 | this.setReportSortID("Binary Insertsort");
42 | this.setCategory("Insertion Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | public void customBinaryInsert(int[] array, int start, int end, double sleep) {
52 | this.binaryInsertSort(array, start, end, sleep, sleep);
53 | }
54 |
55 | @Override
56 | public void runSort(int[] array, int currentLength, int bucketCount) {
57 | this.binaryInsertSort(array, 0, currentLength, 1, 0.05);
58 | }
59 | }
--------------------------------------------------------------------------------
/src/sorts/BinaryMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.MergeSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class BinaryMergeSort extends MergeSorting {
36 | public BinaryMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Binary Merge");
40 | this.setRunAllID("Binary Merge Sort");
41 | this.setReportSortID("Binary Mergesort");
42 | this.setCategory("Hybrid Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | this.mergeSort(array, length, true);
54 | }
55 | }
--------------------------------------------------------------------------------
/src/sorts/BinaryQuickSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BinaryQuickSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /**
10 | * Binary MSD Radix Sort / Binary Quicksort.
11 | *
12 | * Implemented as recursive decent, and via task queue, see:
13 | * * binaryQuickSortRecursive, and
14 | * * binaryQuickSort respectively.
15 | *
16 | * Both of which are in-place sorting algorithms, with the recursive utilizing
17 | * the stack for divide-and-conquer, while the non-recursive utilizes a queue.
18 | *
19 | * Can be extended to support unsigned integers, by sorting the first bit rin
20 | * reverse. Can be made stable at the cost of O(n) memory. Can be parallalized
21 | * to O(log2(n)) subtasks / threads.
22 | *
23 | * @author Skeen
24 | */
25 |
26 | final public class BinaryQuickSort extends BinaryQuickSorting {
27 | public BinaryQuickSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
28 | super(delayOps, markOps, readOps, writeOps);
29 |
30 | this.setSortPromptID("Binary Quick");
31 | this.setRunAllID("Binary Quick Sort");
32 | this.setReportSortID("Binary Quicksort");
33 | this.setCategory("Distribution Sorts");
34 | this.isComparisonBased(false);
35 | this.isBucketSort(false);
36 | this.isRadixSort(false);
37 | this.isUnreasonablySlow(false);
38 | this.setUnreasonableLimit(0);
39 | this.isBogoSort(false);
40 | }
41 |
42 | @Override
43 | public void runSort(int[] array, int length, int bucketCount) {
44 | int mostSignificantBit = Reads.analyzeBit(array, length);
45 | this.binaryQuickSort(array, 0, length - 1, mostSignificantBit);
46 | }
47 | }
--------------------------------------------------------------------------------
/src/sorts/BitonicSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Bitonic Sort was taken from here, written by H.W. Lang:
11 | * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
12 | */
13 |
14 | final public class BitonicSort extends Sort {
15 | private boolean direction = true;
16 |
17 | public BitonicSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
18 | super(delayOps, markOps, readOps, writeOps);
19 |
20 | this.setSortPromptID("Bitonic");
21 | this.setRunAllID("Batcher's Bitonic Sort");
22 | this.setReportSortID("Bitonic Sort");
23 | this.setCategory("Concurrent Sorts");
24 | this.isComparisonBased(true);
25 | this.isBucketSort(false);
26 | this.isRadixSort(false);
27 | this.isUnreasonablySlow(false);
28 | this.setUnreasonableLimit(0);
29 | this.isBogoSort(false);
30 | }
31 |
32 | private static int greatestPowerOfTwoLessThan(int n){
33 | int k = 1;
34 | while (k < n) {
35 | k = k << 1;
36 | }
37 | return k >> 1;
38 | }
39 |
40 | private void compare(int[] A, int i, int j, boolean dir)
41 | {
42 | Highlights.markArray(1, i);
43 | Highlights.markArray(2, j);
44 |
45 | Delays.sleep(0.5);
46 |
47 | int cmp = Reads.compare(A[i], A[j]);
48 |
49 | if (dir == (cmp == 1)) Writes.swap(A, i, j, 1, true, false);
50 | }
51 |
52 | private void bitonicMerge(int[] A, int lo, int n, boolean dir)
53 | {
54 | if (n > 1)
55 | {
56 | int m = BitonicSort.greatestPowerOfTwoLessThan(n);
57 |
58 | for (int i = lo; i < lo + n - m; i++) {
59 | this.compare(A, i, i+m, dir);
60 | }
61 |
62 | this.bitonicMerge(A, lo, m, dir);
63 | this.bitonicMerge(A, lo + m, n - m, dir);
64 | }
65 | }
66 |
67 | private void bitonicSort(int[] A, int lo, int n, boolean dir)
68 | {
69 | if (n > 1)
70 | {
71 | int m = n / 2;
72 | this.bitonicSort(A, lo, m, !dir);
73 | this.bitonicSort(A, lo + m, n - m, dir);
74 | this.bitonicMerge(A, lo, n, dir);
75 | }
76 | }
77 |
78 | public void changeDirection(String choice) throws Exception {
79 | if(choice.equals("forward")) this.direction = true;
80 | else if(choice.equals("backward")) this.direction = false;
81 | else throw new Exception("Invalid direction for Bitonic Sort!");
82 | }
83 |
84 | @Override
85 | public void runSort(int[] array, int currentLength, int bucketCount) {
86 | this.bitonicSort(array, 0, currentLength, this.direction);
87 | }
88 | }
--------------------------------------------------------------------------------
/src/sorts/BogoBogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 |
5 | public final class BogoBogoSort extends BogoSorting {
6 | public BogoBogoSort(utils.Delays delayOps, utils.Highlights markOps, utils.Reads readOps, utils.Writes writeOps) {
7 | super(delayOps, markOps, readOps, writeOps);
8 |
9 | this.setSortPromptID("Bogobogo");
10 | this.setRunAllID("Bogobogo Sort");
11 | this.setReportSortID("Bogobogosort");
12 | this.setCategory("Distributive Sorts");
13 | this.isComparisonBased(false); //Comparisons are not used to swap elements
14 | this.isBucketSort(false);
15 | this.isRadixSort(false);
16 | this.isUnreasonablySlow(true);
17 | this.setUnreasonableLimit(8);
18 | this.isBogoSort(true);
19 | }
20 |
21 | @Override
22 | public void runSort(int[] array, int currentLength, int bucketCount) {
23 | int bogoLength = 2;
24 | boolean arrayNotSorted = true;
25 |
26 | while(arrayNotSorted) {
27 | if(bogoIsSorted(array, bogoLength)) {
28 | if(bogoLength == currentLength) {
29 | arrayNotSorted = false;
30 | }
31 | else {
32 | bogoLength++;
33 | }
34 | }
35 | else {
36 | bogoLength = 2;
37 | }
38 | if(arrayNotSorted) bogoSwap(array, bogoLength, 0);
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/src/sorts/BogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class BogoSort extends BogoSorting {
36 | public BogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Bogo");
40 | this.setRunAllID("Bogo Sort");
41 | this.setReportSortID("Bogosort");
42 | this.setCategory("Distributive Sorts");
43 | this.isComparisonBased(false); //Comparisons are not used to swap elements
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(true);
47 | this.setUnreasonableLimit(16);
48 | this.isBogoSort(true);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int currentLen, int bucketCount) {
53 | while(!this.bogoIsSorted(array, currentLen)) {
54 | this.bogoSwap(array, currentLen, 0);
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/src/sorts/BranchedPDQSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.PDQSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | pdqsort.h - Pattern-defeating quicksort.
12 | Copyright (c) 2015 Orson Peters
13 | This software is provided 'as-is', without any express or implied warranty. In no event will the
14 | authors be held liable for any damages arising from the use of this software.
15 | Permission is granted to anyone to use this software for any purpose, including commercial
16 | applications, and to alter it and redistribute it freely, subject to the following restrictions:
17 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
18 | original software. If you use this software in a product, an acknowledgment in the product
19 | documentation would be appreciated but is not required.
20 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
21 | being the original software.
22 | 3. This notice may not be removed or altered from any source distribution.
23 | *
24 | */
25 |
26 | final public class BranchedPDQSort extends PDQSorting {
27 | public BranchedPDQSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
28 | super(delayOps, markOps, readOps, writeOps);
29 |
30 | this.setSortPromptID("Branched PDQ");
31 | this.setRunAllID("Pattern-Defeating Quick Sort");
32 | this.setReportSortID("Pattern-Defeating Quicksort");
33 | this.setCategory("Hybrid Sorts");
34 | this.isComparisonBased(true);
35 | this.isBucketSort(false);
36 | this.isRadixSort(false);
37 | this.isUnreasonablySlow(false);
38 | this.setUnreasonableLimit(0);
39 | this.isBogoSort(false);
40 | }
41 |
42 | public void customSort(int[] array, int low, int high) {
43 | this.newHeapSorter(new MaxHeapSort(this.Delays, this.Highlights, this.Reads, this.Writes));
44 | pdqLoop(array, low, high, false, pdqLog(high - low));
45 | }
46 |
47 | @Override
48 | public void runSort(int[] array, int currentLength, int bucketCount) {
49 | this.newHeapSorter(new MaxHeapSort(this.Delays, this.Highlights, this.Reads, this.Writes));
50 | pdqLoop(array, 0, currentLength, false, pdqLog(currentLength));
51 | }
52 | }
--------------------------------------------------------------------------------
/src/sorts/BranchlessPDQSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.PDQSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | pdqsort.h - Pattern-defeating quicksort.
12 | Copyright (c) 2015 Orson Peters
13 | This software is provided 'as-is', without any express or implied warranty. In no event will the
14 | authors be held liable for any damages arising from the use of this software.
15 | Permission is granted to anyone to use this software for any purpose, including commercial
16 | applications, and to alter it and redistribute it freely, subject to the following restrictions:
17 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
18 | original software. If you use this software in a product, an acknowledgment in the product
19 | documentation would be appreciated but is not required.
20 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
21 | being the original software.
22 | 3. This notice may not be removed or altered from any source distribution.
23 | *
24 | */
25 |
26 | final public class BranchlessPDQSort extends PDQSorting {
27 | public BranchlessPDQSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
28 | super(delayOps, markOps, readOps, writeOps);
29 |
30 | this.setSortPromptID("Branchless PDQ");
31 | this.setRunAllID("Branchless Pattern-Defeating Quick Sort");
32 | this.setReportSortID("Branchless Pattern-Defeating Quicksort");
33 | this.setCategory("Hybrid Sorts");
34 | this.isComparisonBased(true);
35 | this.isBucketSort(false);
36 | this.isRadixSort(false);
37 | this.isUnreasonablySlow(false);
38 | this.setUnreasonableLimit(0);
39 | this.isBogoSort(false);
40 | }
41 |
42 | @Override
43 | public void runSort(int[] array, int currentLength, int bucketCount) {
44 | this.newHeapSorter(new MaxHeapSort(this.Delays, this.Highlights, this.Reads, this.Writes));
45 | pdqLoop(array, 0, currentLength, true, pdqLog(currentLength));
46 | }
47 | }
--------------------------------------------------------------------------------
/src/sorts/BubbleBogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class BubbleBogoSort extends BogoSorting {
36 | public BubbleBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Bubble Bogo");
40 | this.setRunAllID("Bubble Bogo Sort");
41 | this.setReportSortID("Bubble Bogosort");
42 | this.setCategory("Exchange Sorts");
43 | this.isComparisonBased(true); //Comparisons ARE used to swap elements
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(true);
47 | this.setUnreasonableLimit(2048);
48 | this.isBogoSort(true);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int currentLen, int bucketCount) {
53 | while(!this.bogoIsSorted(array, currentLen)){
54 | int index1 = (int) (Math.random() * (currentLen - 1));
55 |
56 | Highlights.markArray(1, index1);
57 |
58 | if(Reads.compare(array[index1], array[index1 + 1]) == 1){
59 | Writes.swap(array, index1, index1 + 1, 1, true, false);
60 | }
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/src/sorts/BubbleSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class BubbleSort extends Sort {
36 | public BubbleSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Bubble");
40 | this.setRunAllID("Bubble Sort");
41 | this.setReportSortID("Bubblesort");
42 | this.setCategory("Exchange Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | boolean sorted = false;
54 |
55 | while(!sorted) {
56 | sorted = true;
57 | for(int i = 0; i < length - 1; i++) {
58 | if(Reads.compare(array[i], array[i + 1]) == 1){
59 | Writes.swap(array, i, i + 1, 0.075, true, false);
60 | sorted = false;
61 | }
62 |
63 | Highlights.markArray(1, i);
64 | Highlights.markArray(2, i + 1);
65 | Delays.sleep(0.05);
66 | }
67 | }
68 | }
69 | }
--------------------------------------------------------------------------------
/src/sorts/CircleSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.CircleSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org.
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.2
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | final public class CircleSort extends CircleSorting {
22 | public CircleSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Circle");
26 | this.setRunAllID("Circle Sort");
27 | this.setReportSortID("Circlesort");
28 | this.setCategory("Exchange Sorts");
29 | this.isComparisonBased(true);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | @Override
38 | public void runSort(int[] array, int length, int bucketCount) {
39 | do {
40 | Delays.sleep(1);
41 | } while (this.circleSortRoutine(array, 0, length - 1, 0) != 0);
42 | }
43 | }
--------------------------------------------------------------------------------
/src/sorts/CocktailBogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class CocktailBogoSort extends BogoSorting {
10 | public CocktailBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Cocktail Bogo");
14 | this.setRunAllID("Cocktail Bogo Sort");
15 | this.setReportSortID("Cocktail Bogosort");
16 | this.setCategory("Distributive Sorts");
17 | this.isComparisonBased(false); //Comparisons are not used to swap elements
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(true);
21 | this.setUnreasonableLimit(512);
22 | this.isBogoSort(true);
23 | }
24 |
25 | @Override
26 | public void runSort(int[] array, int currentLen, int bucketCount) {
27 | int minIterator = 0;
28 | int maxIterator = currentLen - 1;
29 |
30 | while(minIterator < maxIterator) {
31 | boolean maxSorted = this.isMaxSorted(array, minIterator, maxIterator);
32 | boolean minSorted = this.isMinSorted(array, maxIterator + 1, minIterator);
33 |
34 | while(!maxSorted && !minSorted) {
35 | this.bogoSwap(array, maxIterator + 1, minIterator);
36 |
37 | maxSorted = this.isMaxSorted(array, minIterator, maxIterator);
38 | minSorted = this.isMinSorted(array, maxIterator + 1, minIterator);
39 | }
40 |
41 | if(minSorted) {
42 | //Highlights.markArray(1, minIterator);
43 | minIterator++;
44 | minSorted = false;
45 | }
46 | if(maxSorted) {
47 | //Highlights.markArray(2, maxIterator);
48 | maxIterator--;
49 | maxSorted = false;
50 | }
51 | }
52 | }
53 | }
--------------------------------------------------------------------------------
/src/sorts/CocktailMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import templates.TimSorting;
5 | import utils.Delays;
6 | import utils.Highlights;
7 | import utils.Reads;
8 | import utils.Writes;
9 |
10 | // Inspired by Sorting Stuff's "Obscure Sorting Algorithms": https://www.youtube.com/watch?v=fWubJgIWyxQ
11 |
12 | // Basically, "Cocktail Merge Sort" is a hybrid between Cocktail Shaker Sort and TimSort. It starts by building
13 | // runs of TimSort's minimum length using Cocktail Shaker, then merges all these runs using TimSort. This
14 | // effectively replaces Binary Insertion Sort, used for building runs in TimSort. Big-O analysis would still say
15 | // this is constant time, as the minrun value is not dependent on the number of elements we are sorting, but
16 | // Cocktail Shaker has worse constant factors than Insertion Sort. So basically, this is just for fun.
17 | // But hey, why not? ;)
18 |
19 | public class CocktailMergeSort extends Sort {
20 | private TimSorting timSortInstance; // TimSort cannot be simply written off as an abstract class, as it creates an instance of itself
21 | // in order to track its state. Plus, it contains both instance and static methods, requiring even
22 | // more refactoring, which would be just doing unnecessary busy work. Instead of what we've done for
23 | // the rest of the algorithms, we'll favor composition over inheritance here and pass "util" objects
24 | // to it.
25 |
26 | public CocktailMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
27 | super(delayOps, markOps, readOps, writeOps);
28 |
29 | this.setSortPromptID("Cocktail Merge");
30 | this.setRunAllID("Cocktail Merge Sort");
31 | this.setReportSortID("Cocktail Mergesort");
32 | this.setCategory("Hybrid Sorts");
33 | this.isComparisonBased(true);
34 | this.isBucketSort(false);
35 | this.isRadixSort(false);
36 | this.isUnreasonablySlow(false);
37 | this.setUnreasonableLimit(0);
38 | this.isBogoSort(false);
39 | }
40 |
41 | @Override
42 | public void runSort(int[] array, int currentLength, int bucketCount) {
43 | CocktailShakerSort cocktailShaker = new CocktailShakerSort(Delays, Highlights, Reads, Writes);
44 | int minRunLen = TimSorting.minRunLength(currentLength);
45 |
46 | if (currentLength == minRunLen) {
47 | cocktailShaker.runSort(array, currentLength, bucketCount);
48 | }
49 | else {
50 | int i = 0;
51 | for (; i <= (currentLength - minRunLen); i += minRunLen) {
52 | cocktailShaker.customSort(array, i, i + minRunLen);
53 | }
54 | if (i + minRunLen > currentLength) {
55 | cocktailShaker.customSort(array, i, currentLength);
56 | }
57 |
58 | Highlights.clearAllMarks();
59 |
60 | this.timSortInstance = new TimSorting(array, currentLength, this.Delays, this.Highlights, this.Reads, this.Writes);
61 | TimSorting.sort(this.timSortInstance, array, currentLength);
62 | }
63 | }
64 | }
--------------------------------------------------------------------------------
/src/sorts/CocktailShakerSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class CocktailShakerSort extends Sort {
36 | public CocktailShakerSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Cocktail Shaker");
40 | this.setRunAllID("Cocktail Shaker Sort");
41 | this.setReportSortID("Cocktail Shaker Sort");
42 | this.setCategory("Exchange Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | private void cocktailShaker(int[] array, int start, int end, double sleep) {
52 | int i = start;
53 | while(i < ((end / 2) + start)) {
54 | for(int j = i; j < end + start - i - 1; j++) {
55 | if(Reads.compare(array[j], array[j + 1]) == 1) {
56 | Writes.swap(array, j, j + 1, sleep, true, false);
57 | }
58 |
59 | Highlights.markArray(1, j);
60 | Highlights.markArray(2, j + 1);
61 |
62 | Delays.sleep(sleep / 2);
63 | }
64 | for(int j = end + start - i - 1; j > i; j--){
65 | if(Reads.compare(array[j], array[j - 1]) == -1) {
66 | Writes.swap(array, j, j - 1, sleep, true, false);
67 | }
68 |
69 | Highlights.markArray(1, j);
70 | Highlights.markArray(2, j - 1);
71 |
72 | Delays.sleep(sleep / 2);
73 | }
74 |
75 | i++;
76 | }
77 | }
78 |
79 | public void customSort(int[] array, int start, int end) {
80 | this.cocktailShaker(array, start, end, 1);
81 | }
82 |
83 | @Override
84 | public void runSort(int[] array, int length, int bucketCount) {
85 | this.cocktailShaker(array, 0, length, 0.1);
86 | }
87 | }
--------------------------------------------------------------------------------
/src/sorts/CombSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.CombSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | The MIT License (MIT)
12 |
13 | Copyright (c) 2012 Daniel Imms, http://www.growingwiththeweb.com
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy of
16 | this software and associated documentation files (the "Software"), to deal in
17 | the Software without restriction, including without limitation the rights to
18 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19 | the Software, and to permit persons to whom the Software is furnished to do so,
20 | subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 | *
32 | */
33 |
34 | final public class CombSort extends CombSorting {
35 | public CombSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
36 | super(delayOps, markOps, readOps, writeOps);
37 |
38 | this.setSortPromptID("Comb");
39 | this.setRunAllID("Comb Sort");
40 | this.setReportSortID("Combsort");
41 | this.setCategory("Exchange Sorts");
42 | this.isComparisonBased(true);
43 | this.isBucketSort(false);
44 | this.isRadixSort(false);
45 | this.isUnreasonablySlow(false);
46 | this.setUnreasonableLimit(0);
47 | this.isBogoSort(false);
48 |
49 | //Default options
50 | this.setShrinkFactor(4); //Index 4 of Shrink Factors array
51 | }
52 |
53 | @Override
54 | public void runSort(int[] array, int currentLength, int bucketCount) {
55 | this.combSort(this.ArrayVisualizer, array, currentLength, false);
56 | }
57 | }
--------------------------------------------------------------------------------
/src/sorts/CountingSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import java.util.Arrays;
4 |
5 | import templates.Sort;
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Writes;
10 |
11 | /*
12 | *
13 | MIT License
14 |
15 | Copyright (c) 2019 w0rthy
16 |
17 | Permission is hereby granted, free of charge, to any person obtaining a copy
18 | of this software and associated documentation files (the "Software"), to deal
19 | in the Software without restriction, including without limitation the rights
20 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 | copies of the Software, and to permit persons to whom the Software is
22 | furnished to do so, subject to the following conditions:
23 |
24 | The above copyright notice and this permission notice shall be included in all
25 | copies or substantial portions of the Software.
26 |
27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 | SOFTWARE.
34 | *
35 | */
36 |
37 | final public class CountingSort extends Sort {
38 | public CountingSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
39 | super(delayOps, markOps, readOps, writeOps);
40 |
41 | this.setSortPromptID("Counting");
42 | this.setRunAllID("Counting Sort");
43 | this.setReportSortID("Counting Sort");
44 | this.setCategory("Distributive Sorts");
45 | this.isComparisonBased(false);
46 | this.isBucketSort(false);
47 | this.isRadixSort(false);
48 | this.isUnreasonablySlow(false);
49 | this.setUnreasonableLimit(0);
50 | this.isBogoSort(false);
51 | }
52 |
53 | @Override
54 | public void runSort(int[] array, int length, int bucketCount) {
55 | int max = Reads.analyzeMax(array, length, 0, false);
56 |
57 | int[] output = Arrays.copyOf(array, length);
58 | int[] counts = new int[max + 1];
59 |
60 | for (int i = 0; i < length; i++) {
61 | Writes.write(counts, array[i], counts[array[i]] + 1, 1, false, true);
62 | Highlights.markArray(1, i);
63 | }
64 |
65 | for (int i = 1; i < counts.length; i++) {
66 | Writes.write(counts, i, counts[i] + counts[i - 1], 1, true, true);
67 | }
68 |
69 | for (int i = length - 1; i >= 0; i--) {
70 | output[counts[array[i]] - 1] = array[i];
71 | counts[array[i]]--;
72 | }
73 |
74 | // Extra loop to simulate the results from the "output" array being written
75 | // to the visual array.
76 | for (int i = length - 1; i >= 0; i--) {
77 | Writes.write(array, i, output[i], 1, true, false);
78 | Writes.changeTempWrites(1);
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/src/sorts/CycleSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.3
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | final public class CycleSort extends Sort {
22 | public CycleSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Cycle");
26 | this.setRunAllID("Cycle Sort");
27 | this.setReportSortID("Cyclesort");
28 | this.setCategory("Selection Sorts");
29 | this.isComparisonBased(true);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | @Override
38 | public void runSort(int[] array, int length, int bucketCount) {
39 | for (int cycleStart = 0; cycleStart < length - 1; cycleStart++) {
40 | int val = array[cycleStart];
41 |
42 | /*
43 | Count the number of values that are smaller
44 | than val since cycleStart
45 | */
46 |
47 | int pos = cycleStart;
48 | Highlights.markArray(3, pos);
49 |
50 | for (int i = cycleStart + 1; i < length; i++) {
51 | Highlights.markArray(2, i);
52 | Delays.sleep(0.01);
53 |
54 | if (Reads.compare(array[i], val) == -1) {
55 | pos++;
56 | Highlights.markArray(1, pos);
57 | Delays.sleep(0.01);
58 | }
59 |
60 | }
61 |
62 | // there aren't any
63 | if (pos == cycleStart) {
64 | Highlights.markArray(1, pos);
65 | continue;
66 | }
67 |
68 | // Skip duplicates
69 | while (val == array[pos]) {
70 | pos++;
71 | Highlights.markArray(1, pos);
72 | }
73 |
74 | // Put val into final position
75 | int tmp = array[pos];
76 | Writes.write(array, pos, val, 0.02, true, false);
77 | val = tmp;
78 |
79 | /*
80 | Repeat as long as we can find values to swap
81 | otherwise start new cycle
82 | */
83 | while (pos != cycleStart) {
84 | pos = cycleStart;
85 | Highlights.markArray(3, pos);
86 |
87 | for (int i = cycleStart + 1; i < length; i++) {
88 | Highlights.markArray(2, i);
89 | Delays.sleep(0.01);
90 |
91 | if (Reads.compare(array[i], val) == -1) {
92 | pos++;
93 | Highlights.markArray(1, pos);
94 | Delays.sleep(0.01);
95 | }
96 | }
97 |
98 | while (val == array[pos]) {
99 | pos++;
100 | Highlights.markArray(1, pos);
101 | }
102 |
103 | tmp = array[pos];
104 | Writes.write(array, pos, val, 0.02, true, false);
105 | val = tmp;
106 | }
107 | }
108 | }
109 | }
--------------------------------------------------------------------------------
/src/sorts/DoubleSelectionSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class DoubleSelectionSort extends Sort {
36 | public DoubleSelectionSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Double Selection");
40 | this.setRunAllID("Double Selection Sort");
41 | this.setReportSortID("Double Selection Sort");
42 | this.setCategory("Selection Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | int left = 0;
54 | int right = length - 1;
55 | int smallest = 0;
56 | int biggest = 0;
57 |
58 | while(left <= right) {
59 | for(int i = left; i <= right; i++) {
60 | Highlights.markArray(3, i);
61 |
62 | if(Reads.compare(array[i], array[biggest]) == 1) {
63 | biggest = i;
64 | Highlights.markArray(1, biggest);
65 | Delays.sleep(0.01);
66 | }
67 | if(Reads.compare(array[i], array[smallest]) == -1) {
68 | smallest = i;
69 | Highlights.markArray(2, smallest);
70 | Delays.sleep(0.01);
71 | }
72 |
73 | Delays.sleep(0.01);
74 | }
75 | if(biggest == left)
76 | biggest = smallest;
77 |
78 | Writes.swap(array, left, smallest, 0.02, true, false);
79 | Writes.swap(array, right, biggest, 0.02, true, false);
80 |
81 | left++;
82 | right--;
83 |
84 | smallest = left;
85 | biggest = right;
86 | }
87 | }
88 | }
--------------------------------------------------------------------------------
/src/sorts/DualPivotQuickSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class DualPivotQuickSort extends Sort {
10 | public DualPivotQuickSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Dual-Pivot Quick");
14 | this.setRunAllID("Dual-Pivot Quick Sort");
15 | this.setReportSortID("Dual-Pivot Quicksort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | /*
26 | * This example of a basic Dual-Pivot Quicksort may be found here, written by Sebastian Wild (Sebastian on StackOverflow):
27 | * https://cs.stackexchange.com/questions/24092/dual-pivot-quicksort-reference-implementation
28 | */
29 | private void dualPivot(int[] a, int left, int right) {
30 | if (right > left) {
31 | if (Reads.compare(a[left], a[right]) == 1) {
32 | Writes.swap(a, left, right, 1, true, false);
33 | }
34 |
35 | int p = a[left];
36 | int q = a[right];
37 |
38 | int l = left + 1;
39 | int g = right - 1;
40 | int k = l;
41 |
42 | while (k <= g)
43 | {
44 | Delays.sleep(0.1);
45 |
46 | if (Reads.compare(a[k], p) == -1) {
47 | Writes.swap(a, k, l, 1, true, false);
48 | Highlights.clearMark(2);
49 |
50 | l++;
51 | Highlights.markArray(4, l);
52 | }
53 | else if (Reads.compare(a[k], q) >= 0) {
54 | while (Reads.compare(a[g], q) == 1 && k < g) {
55 | g--;
56 | Highlights.markArray(3, g);
57 | Delays.sleep(0.2);
58 | }
59 |
60 | Writes.swap(a, k, g, 1, true, false);
61 | Highlights.clearMark(2);
62 |
63 | g--;
64 | Highlights.markArray(3, g);
65 |
66 | if (Reads.compare(a[k], p) == -1) {
67 | Writes.swap(a, k, l, 0.2, true, false);
68 | Highlights.clearMark(2);
69 |
70 | ++l;
71 | Highlights.markArray(4, l);
72 | }
73 | }
74 | ++k;
75 | Highlights.markArray(1, k);
76 | Delays.sleep(0.2);
77 | }
78 |
79 | --l;
80 | ++g;
81 |
82 | Writes.swap(a, left, l, 1, true, false);
83 |
84 | Highlights.clearMark(2);
85 |
86 | Writes.swap(a, right, g, 1, true, false);
87 |
88 | Highlights.clearMark(2);
89 | Highlights.clearMark(3);
90 | Highlights.clearMark(4);
91 |
92 | this.dualPivot(a, left, l - 1);
93 | this.dualPivot(a, l + 1, g - 1);
94 | this.dualPivot(a, g + 1, right);
95 | }
96 | }
97 |
98 | @Override
99 | public void runSort(int[] array, int currentLength, int bucketCount) {
100 | this.dualPivot(array, 0, currentLength - 1);
101 | }
102 | }
--------------------------------------------------------------------------------
/src/sorts/ExchangeBogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class ExchangeBogoSort extends BogoSorting {
10 | public ExchangeBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Exchange Bogo");
14 | this.setRunAllID("Exchange Bogo Sort");
15 | this.setReportSortID("Exchange Bogosort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true); //Comparisons ARE used to swap elements
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(true);
21 | this.setUnreasonableLimit(1024);
22 | this.isBogoSort(true);
23 | }
24 |
25 | @Override
26 | public void runSort(int[] array, int currentLen, int bucketCount) {
27 | while(!bogoIsSorted(array, currentLen)){
28 | int index1 = (int) (Math.random() * currentLen),
29 | index2 = (int) (Math.random() * currentLen);
30 |
31 | Highlights.markArray(1, index1);
32 | Highlights.markArray(2, index2);
33 |
34 | if(index1 < index2) {
35 | if(Reads.compare(array[index1], array[index2]) == 1){
36 | Writes.swap(array, index1, index2, 1, true, false);
37 | }
38 | }
39 | else {
40 | if(Reads.compare(array[index1], array[index2]) == -1){
41 | Writes.swap(array, index1, index2, 1, true, false);
42 | }
43 | }
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/src/sorts/FlippedMinHeapSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org.
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.2
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | /*
22 | modified by Lucy Phipps from ../templates/HeapSorting.java and MinHeapSort.java
23 | the only real changes are subtracting every array access from (length - 1)
24 | and removing the Writes.reverse() at the end
25 | the rest is just compacting the code a bit
26 | */
27 |
28 | final public class FlippedMinHeapSort extends Sort {
29 | public FlippedMinHeapSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
30 | super(delayOps, markOps, readOps, writeOps);
31 | this.setSortPromptID("Flipped Min Heap");
32 | this.setRunAllID("Flipped Min Heap Sort");
33 | this.setReportSortID("Flipped Reverse Heapsort");
34 | this.setCategory("Selection Sorts");
35 | this.isComparisonBased(true);
36 | this.isBucketSort(false);
37 | this.isRadixSort(false);
38 | this.isUnreasonablySlow(false);
39 | this.setUnreasonableLimit(0);
40 | this.isBogoSort(false);
41 | }
42 | private void siftDown(int[] array, int length, int root, int dist) {
43 | while (root <= dist / 2) {
44 | int leaf = 2 * root;
45 | if (leaf < dist && Reads.compare(array[length - leaf], array[length - leaf - 1]) == 1) {
46 | leaf++;
47 | }
48 | Highlights.markArray(1, length - root);
49 | Highlights.markArray(2, length - leaf);
50 | Delays.sleep(1);
51 | if (Reads.compare(array[length - root], array[length - leaf]) == 1) {
52 | Writes.swap(array, length - root, length - leaf, 0, true, false);
53 | root = leaf;
54 | } else break;
55 | }
56 | }
57 | @Override
58 | public void runSort(int[] array, int length, int bucketCount) {
59 | for (int i = length / 2; i >= 1; i--) {
60 | siftDown(array, length, i, length);
61 | }
62 | for (int i = length; i > 1; i--) {
63 | Writes.swap(array, length - 1, length - i, 1, true, false);
64 | siftDown(array, length, 1, i - 1);
65 | }
66 | }
67 | }
--------------------------------------------------------------------------------
/src/sorts/GnomeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class GnomeSort extends Sort {
10 | public GnomeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Gnome");
14 | this.setRunAllID("Gnome Sort");
15 | this.setReportSortID("Gnomesort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | // Code retrieved from http://www.algostructure.com/sorting/gnomesort.php
26 |
27 | @Override
28 | public void runSort(int[] array, int length, int bucketCount) {
29 | for (int i = 1; i < length;)
30 | {
31 | if (Reads.compare(array[i], array[i-1]) >= 0)
32 | {
33 | i++;
34 | Highlights.markArray(1, i);
35 | Delays.sleep(0.04);
36 | }
37 | else
38 | {
39 | Writes.swap(array, i, i - 1, 0.02, true, false);
40 |
41 | Highlights.clearMark(2);
42 |
43 | if (i > 1) {
44 | i--;
45 | Highlights.markArray(1, i);
46 | Delays.sleep(0.02);
47 | }
48 | }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/src/sorts/GravitySort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class GravitySort extends Sort {
36 | public GravitySort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Gravity");
40 | this.setRunAllID("Gravity Sort");
41 | //this.setRunAllID("Gravity (Bead) Sort");
42 | this.setReportSortID("Beadsort");
43 | this.setCategory("Distributive Sorts");
44 | this.isComparisonBased(false);
45 | this.isBucketSort(false);
46 | this.isRadixSort(false);
47 | this.isUnreasonablySlow(true);
48 | this.setUnreasonableLimit(4096);
49 | this.isBogoSort(false);
50 | }
51 |
52 | @Override
53 | public void runSort(int[] array, int length, int bucketCount) {
54 | int max = Reads.analyzeMax(array, length, 1, true);
55 | int[][] abacus = new int[length][max];
56 |
57 | for(int i = 0; i < length; i++) {
58 | for(int j = 0; j < array[i]; j++) {
59 | Writes.multiDimWrite(abacus, i, abacus[0].length - j - 1, 1, 0, true, true);
60 | }
61 | }
62 |
63 | //apply gravity
64 | for(int i = 0; i < abacus[0].length; i++) {
65 | for(int j = 0; j < abacus.length; j++) {
66 | Highlights.markArray(1, j);
67 | if(abacus[j][i] == 1) {
68 | //Drop it
69 | int dropPos = j;
70 |
71 | Writes.startLap();
72 | while(dropPos + 1 < abacus.length && abacus[dropPos][i] == 1) {
73 | dropPos++;
74 | }
75 | Writes.stopLap();
76 |
77 | if(abacus[dropPos][i] == 0) {
78 | Writes.multiDimWrite(abacus, j, i, 0, 0, true, true);
79 | Writes.multiDimWrite(abacus, dropPos, i, 1, 0, true, true);
80 | }
81 | }
82 | }
83 |
84 | int count = 0;
85 | for(int x = 0; x < abacus.length; x++){
86 | count = 0;
87 |
88 | Writes.startLap();
89 | for(int y = 0; y < abacus[0].length; y++) {
90 | count += abacus[x][y];
91 | }
92 | Writes.stopLap();
93 |
94 | Writes.write(array, x, count, 0.001, true, false);
95 | }
96 | Highlights.markArray(2, length - i - 1);
97 | }
98 | }
99 | }
--------------------------------------------------------------------------------
/src/sorts/HybridCombSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.CombSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | The MIT License (MIT)
12 |
13 | Copyright (c) 2012 Daniel Imms, http://www.growingwiththeweb.com
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy of
16 | this software and associated documentation files (the "Software"), to deal in
17 | the Software without restriction, including without limitation the rights to
18 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19 | the Software, and to permit persons to whom the Software is furnished to do so,
20 | subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 | *
32 | */
33 |
34 | final public class HybridCombSort extends CombSorting {
35 | public HybridCombSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
36 | super(delayOps, markOps, readOps, writeOps);
37 |
38 | this.setSortPromptID("Hybrid Comb");
39 | this.setRunAllID("Hybrid Comb Sort");
40 | this.setReportSortID("Hybrid Combsort");
41 | this.setCategory("Hybrid Sorts");
42 | this.isComparisonBased(true);
43 | this.isBucketSort(false);
44 | this.isRadixSort(false);
45 | this.isUnreasonablySlow(false);
46 | this.setUnreasonableLimit(0);
47 | this.isBogoSort(false);
48 |
49 | //Default options
50 | this.setShrinkFactor(4); //Index 4 of Shrink Factors array
51 | }
52 |
53 | @Override
54 | public void runSort(int[] array, int currentLength, int bucketCount) {
55 | this.combSort(this.ArrayVisualizer, array, currentLength, true);
56 | }
57 | }
--------------------------------------------------------------------------------
/src/sorts/InPlaceLSDRadixSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class InPlaceLSDRadixSort extends Sort {
36 | public InPlaceLSDRadixSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("In-Place LSD Radix");
40 | //this.setRunAllID("In-Place LSD Radix Sort, Base 2");
41 | this.setRunAllID("In-Place LSD Radix Sort, Base 10");
42 | this.setReportSortID("In-Place LSD Radix Sort");
43 | this.setCategory("Distributive Sorts");
44 | this.isComparisonBased(false);
45 | this.isBucketSort(true);
46 | this.isRadixSort(true);
47 | this.isUnreasonablySlow(false);
48 | this.setUnreasonableLimit(0);
49 | this.isBogoSort(false);
50 | }
51 |
52 | @Override
53 | public void runSort(int[] array, int length, int bucketCount) {
54 | this.setRunAllID("In-Place LSD Radix Sort, Base " + bucketCount);
55 |
56 | int pos = 0;
57 | int[] vregs = new int[bucketCount-1];
58 |
59 | int maxpower = Reads.analyzeMaxLog(array, length, bucketCount, 0.5, true);
60 |
61 | for(int p = 0; p <= maxpower; p++){
62 | for(int i = 0; i < vregs.length; i++) {
63 | Writes.write(vregs, i, length - 1, 0, false, true);
64 | }
65 |
66 | pos = 0;
67 |
68 | for(int i = 0; i < length; i++){
69 | int digit = Reads.getDigit(array[pos], p, bucketCount);
70 |
71 | if(digit == 0) {
72 | pos++;
73 | Highlights.markArray(0, pos);
74 | }
75 | else {
76 | for(int j = 0; j < vregs.length;j++)
77 | Highlights.markArray(j + 1, vregs[j]);
78 |
79 | Writes.multiSwap(array, pos, vregs[digit - 1], bucketCount / 10000d, false, false);
80 |
81 | for(int j = digit - 1; j > 0; j--) {
82 | Writes.write(vregs, j - 1, vregs[j - 1] - 1, 0, false, true);
83 | }
84 | }
85 | }
86 | }
87 | }
88 | }
--------------------------------------------------------------------------------
/src/sorts/InPlaceMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class InPlaceMergeSort extends Sort {
36 | public InPlaceMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("In-Place Merge");
40 | this.setRunAllID("In-Place Merge Sort");
41 | this.setReportSortID("In-Place Mergesort");
42 | this.setCategory("Merge Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | private void push(int[] array, int low, int high) {
52 | for(int i = low; i < high; i++) {
53 | if(Reads.compare(array[i], array[i + 1]) == 1) {
54 | Writes.swap(array, i, i + 1, 0.035, true, false);
55 | }
56 | }
57 | }
58 |
59 | private void merge(int[] array, int min, int max, int mid) {
60 | int i = min;
61 | while(i <= mid) {
62 | if(Reads.compare(array[i], array[mid + 1]) == 1){
63 | Writes.swap(array, i, mid + 1, 0.035, true, false);
64 | push(array, mid + 1, max);
65 | }
66 | i++;
67 | Delays.sleep(0.035);
68 | }
69 | }
70 |
71 | private void mergeSort(int[] array, int min,int max) {
72 | if(max - min == 0) { //only one element.
73 | Delays.sleep(1); //no swap
74 | }
75 | else if(max - min == 1) { //only two elements and swaps them
76 | if(Reads.compare(array[min], array[max]) == 1) {
77 | Writes.swap(array, min, max, 0.035, true, false);
78 | }
79 | }
80 | else {
81 | int mid = ((int) Math.floor((min + max) / 2)); //The midpoint
82 |
83 | mergeSort(array, min, mid); //sort the left side
84 | mergeSort(array, mid + 1, max); //sort the right side
85 | merge(array, min, max, mid); //combines them
86 | }
87 | }
88 |
89 | @Override
90 | public void runSort(int[] array, int currentLength, int bucketCount) {
91 | this.mergeSort(array, 0, currentLength - 1);
92 | }
93 | }
--------------------------------------------------------------------------------
/src/sorts/InsertionSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.InsertionSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class InsertionSort extends InsertionSorting {
36 | public InsertionSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Insertion");
40 | this.setRunAllID("Insertion Sort");
41 | this.setReportSortID("Insertsort");
42 | this.setCategory("Insertion Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | public void customInsertSort(int[] array, int start, int end, double sleep, boolean auxwrite) {
52 | this.insertionSort(array, start, end, sleep, auxwrite);
53 | }
54 |
55 | @Override
56 | public void runSort(int[] array, int currentLength, int bucketCount) {
57 | this.insertionSort(array, 0, currentLength, 0.015, false);
58 | }
59 | }
--------------------------------------------------------------------------------
/src/sorts/IntroCircleSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.CircleSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org.
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.2
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | final public class IntroCircleSort extends CircleSorting {
22 | public IntroCircleSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Intro Circle");
26 | this.setRunAllID("Introspective Circle Sort");
27 | this.setReportSortID("Introspective Circlesort");
28 | this.setCategory("Hybrid Sorts");
29 | this.isComparisonBased(true);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | @Override
38 | public void runSort(int[] array, int length, int bucketCount) {
39 | int iterations = 0;
40 | int threshold = (int) (Math.log(length) / Math.log(2)) / 2;
41 |
42 | do {
43 | iterations++;
44 |
45 | if(iterations >= threshold) {
46 | BinaryInsertionSort binaryInserter = new BinaryInsertionSort(this.Delays, this.Highlights, this.Reads, this.Writes);
47 | binaryInserter.customBinaryInsert(array, 0, length, 0.1);
48 | break;
49 | }
50 | } while (this.circleSortRoutine(array, 0, length - 1, 0) != 0);
51 | }
52 | }
--------------------------------------------------------------------------------
/src/sorts/IterativeBitonicSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Bitonic Sort was taken from here, written by Nikos Pitsianis:
11 | * https://www2.cs.duke.edu/courses/fall08/cps196.1/Pthreads/bitonic.c
12 | */
13 |
14 | final public class IterativeBitonicSort extends Sort {
15 | public IterativeBitonicSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Iterative Bitonic");
19 | this.setRunAllID("Iterative Bitonic Sort");
20 | this.setReportSortID("Iterative Bitonic Sort");
21 | this.setCategory("Concurrent Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(false);
26 | this.setUnreasonableLimit(0);
27 | this.isBogoSort(false);
28 | }
29 |
30 | @Override
31 | public void runSort(int[] array, int currentLength, int bucketCount) {
32 | int i, j, k;
33 |
34 | for(k = 2; k <= currentLength; k = 2 * k) {
35 | for(j = k >> 1; j > 0; j = j >> 1) {
36 | for(i = 0; i < currentLength; i++) {
37 | int ij = i ^ j;
38 |
39 | if((ij) > i) {
40 | if((i & k) == 0 && Reads.compare(array[i], array[ij]) == 1)
41 | Writes.swap(array, i, ij, 1, true, false);
42 | if((i & k) != 0 && Reads.compare(array[i], array[ij]) == -1)
43 | Writes.swap(array, i, ij, 1, true, false);
44 | }
45 | }
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/src/sorts/IterativeOddEvenMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Odd-Even Merge Sort was taken from here, written by wkpark on StackOverflow:
11 | * https://stackoverflow.com/questions/34426337/how-to-fix-this-non-recursive-odd-even-merge-sort-algorithm
12 | */
13 |
14 | final public class IterativeOddEvenMergeSort extends Sort {
15 | public IterativeOddEvenMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Iter. Odd-Even Merge");
19 | this.setRunAllID("Iterative Odd-Even Merge Sort");
20 | this.setReportSortID("Iterative Odd-Even Mergesort");
21 | this.setCategory("Concurrent Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(false);
26 | this.setUnreasonableLimit(0);
27 | this.isBogoSort(false);
28 | }
29 |
30 | @Override
31 | public void runSort(int[] array, int currentLength, int bucketCount) {
32 | for (int p = 1; p < currentLength; p += p)
33 | for (int k = p; k > 0; k /= 2)
34 | for (int j = k % p; j + k < currentLength; j += k + k)
35 | for (int i = 0; i < k; i++)
36 | if ((i + j)/(p + p) == (i + j + k)/(p + p)) {
37 | Highlights.markArray(1, i + j);
38 | Highlights.markArray(2, i + j + k);
39 | Delays.sleep(1);
40 | if(Reads.compare(array[i + j], array[i + j + k]) > 0)
41 | Writes.swap(array, i + j, i + j + k, 1, true, false);
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/src/sorts/LLQuickSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class LLQuickSort extends Sort {
10 | public LLQuickSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("LL Quick");
14 | this.setRunAllID("Quick Sort, Left/Left Pointers");
15 | this.setReportSortID("Left/Left Quicksort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | private int partition(int[] array, int lo, int hi) {
26 | int pivot = array[hi];
27 | int i = lo;
28 |
29 | for(int j = lo; j < hi; j++) {
30 | Highlights.markArray(1, j);
31 | if(Reads.compare(array[j], pivot) < 0) {
32 | Writes.swap(array, i, j, 1, true, false);
33 | i++;
34 | }
35 | Delays.sleep(1);
36 | }
37 | Writes.swap(array, i, hi, 1, true, false);
38 | return i;
39 | }
40 |
41 | private void quickSort(int[] array, int lo, int hi) {
42 | if(lo < hi) {
43 | int p = this.partition(array, lo, hi);
44 | this.quickSort(array, lo, p - 1);
45 | this.quickSort(array, p + 1, hi);
46 | }
47 | }
48 |
49 | @Override
50 | public void runSort(int[] array, int currentLength, int bucketCount) {
51 | this.quickSort(array, 0, currentLength - 1);
52 | }
53 | }
--------------------------------------------------------------------------------
/src/sorts/LRQuickSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class LRQuickSort extends Sort {
10 | public LRQuickSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("LR Quick");
14 | this.setRunAllID("Quick Sort, Left/Right Pointers");
15 | this.setReportSortID("Left/Right Quicksort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | // Thanks to Timo Bingmann for providing a good reference for Quick Sort w/ LR pointers.
26 | private void quickSort(int[] a, int p, int r) {
27 | int pivot = p + (r - p) / 2;
28 | int x = a[pivot];
29 |
30 | int i = p;
31 | int j = r;
32 |
33 | Highlights.markArray(3, pivot);
34 |
35 | while (i <= j) {
36 | while (Reads.compare(a[i], x) == -1){
37 | i++;
38 | Highlights.markArray(1, i);
39 | Delays.sleep(0.5);
40 | }
41 | while (Reads.compare(a[j], x) == 1){
42 | j--;
43 | Highlights.markArray(2, j);
44 | Delays.sleep(0.5);
45 | }
46 |
47 | if (i <= j) {
48 | // Follow the pivot and highlight it.
49 | if(i == pivot) {
50 | Highlights.markArray(3, j);
51 | }
52 | if(j == pivot) {
53 | Highlights.markArray(3, i);
54 | }
55 |
56 | Writes.swap(a, i, j, 1, true, false);
57 |
58 | i++;
59 | j--;
60 | }
61 | }
62 |
63 | if(p < j) {
64 | this.quickSort(a, p, j);
65 | }
66 | if(i < r) {
67 | this.quickSort(a, i, r);
68 | }
69 | }
70 |
71 | @Override
72 | public void runSort(int[] array, int currentLength, int bucketCount) {
73 | this.quickSort(array, 0, currentLength - 1);
74 | }
75 | }
--------------------------------------------------------------------------------
/src/sorts/LSDRadixSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import java.util.ArrayList;
4 |
5 | import templates.Sort;
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Writes;
10 |
11 | /*
12 | *
13 | MIT License
14 |
15 | Copyright (c) 2019 w0rthy
16 |
17 | Permission is hereby granted, free of charge, to any person obtaining a copy
18 | of this software and associated documentation files (the "Software"), to deal
19 | in the Software without restriction, including without limitation the rights
20 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 | copies of the Software, and to permit persons to whom the Software is
22 | furnished to do so, subject to the following conditions:
23 |
24 | The above copyright notice and this permission notice shall be included in all
25 | copies or substantial portions of the Software.
26 |
27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 | SOFTWARE.
34 | *
35 | */
36 |
37 | final public class LSDRadixSort extends Sort {
38 | public LSDRadixSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
39 | super(delayOps, markOps, readOps, writeOps);
40 |
41 | this.setSortPromptID("LSD Radix");
42 | this.setRunAllID("Least Significant Digit Radix Sort, Base 4");
43 | this.setReportSortID("Least Significant Digit Radixsort");
44 | this.setCategory("Distributive Sorts");
45 | this.isComparisonBased(false);
46 | this.isBucketSort(true);
47 | this.isRadixSort(true);
48 | this.isUnreasonablySlow(false);
49 | this.setUnreasonableLimit(0);
50 | this.isBogoSort(false);
51 | }
52 |
53 | @Override
54 | public void runSort(int[] array, int length, int bucketCount) {
55 | this.setRunAllID("Least Significant Digit Radix Sort, Base " + bucketCount);
56 |
57 | int highestpower = Reads.analyzeMaxLog(array, length, bucketCount, 0.5, true);
58 |
59 | @SuppressWarnings("unchecked")
60 | ArrayList[] registers = new ArrayList[bucketCount];
61 |
62 | for(int i = 0; i < bucketCount; i++)
63 | registers[i] = new ArrayList<>();
64 |
65 | for(int p = 0; p <= highestpower; p++){
66 | for(int i = 0; i < length; i++){
67 | Highlights.markArray(1, i);
68 |
69 | int digit = Reads.getDigit(array[i], p, bucketCount);
70 | registers[digit].add(array[i]);
71 |
72 | Writes.mockWrite(length, digit, array[i], 1);
73 | }
74 |
75 | Writes.fancyTranscribe(array, length, registers, bucketCount * 0.8);
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/src/sorts/LazyStableSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.GrailSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | The MIT License (MIT)
12 |
13 | Copyright (c) 2013 Andrey Astrelin
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy of
16 | this software and associated documentation files (the "Software"), to deal in
17 | the Software without restriction, including without limitation the rights to
18 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19 | the Software, and to permit persons to whom the Software is furnished to do so,
20 | subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 | *
32 | */
33 |
34 | /********* Lazy stable sorting ***********/
35 | /* */
36 | /* (c) 2013 by Andrey Astrelin */
37 | /* Refactored by MusicTheorist */
38 | /* */
39 | /* Simple in-place stable sorting, */
40 | /* methods copied from Grail Sort */
41 | /* */
42 | /*****************************************/
43 |
44 | final public class LazyStableSort extends GrailSorting {
45 | public LazyStableSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
46 | super(delayOps, markOps, readOps, writeOps);
47 |
48 | this.setSortPromptID("Lazy Stable");
49 | this.setRunAllID("Lazy Stable Sort");
50 | this.setReportSortID("Lazy Stable Sort");
51 | this.setCategory("Merge Sorts");
52 | this.isComparisonBased(true);
53 | this.isBucketSort(false);
54 | this.isRadixSort(false);
55 | this.isUnreasonablySlow(false);
56 | this.setUnreasonableLimit(0);
57 | this.isBogoSort(false);
58 | }
59 |
60 | @Override
61 | public void runSort(int[] array, int length, int bucketCount) {
62 | this.grailLazyStableSort(array, 0, length);
63 | }
64 | }
--------------------------------------------------------------------------------
/src/sorts/LessBogoSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BogoSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class LessBogoSort extends BogoSorting {
10 | public LessBogoSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Less Bogo");
14 | this.setRunAllID("Less Bogo Sort");
15 | this.setReportSortID("Less Bogosort");
16 | this.setCategory("Distributive Sorts");
17 | this.isComparisonBased(false); //Comparisons are not used to swap elements
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(true);
21 | this.setUnreasonableLimit(512);
22 | this.isBogoSort(true);
23 | }
24 |
25 | @Override
26 | public void runSort(int[] array, int currentLen, int bucketCount) {
27 | int iterator = 0;
28 |
29 | while(iterator != currentLen) {
30 | while(!this.isMinSorted(array, currentLen, iterator)) {
31 | this.bogoSwap(array, currentLen, iterator);
32 | }
33 | //Highlights.markArray(1, iterator);
34 | iterator++;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/sorts/MSDRadixSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import java.util.ArrayList;
4 |
5 | import templates.Sort;
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Writes;
10 |
11 | /*
12 | *
13 | MIT License
14 |
15 | Copyright (c) 2019 w0rthy
16 |
17 | Permission is hereby granted, free of charge, to any person obtaining a copy
18 | of this software and associated documentation files (the "Software"), to deal
19 | in the Software without restriction, including without limitation the rights
20 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 | copies of the Software, and to permit persons to whom the Software is
22 | furnished to do so, subject to the following conditions:
23 |
24 | The above copyright notice and this permission notice shall be included in all
25 | copies or substantial portions of the Software.
26 |
27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 | SOFTWARE.
34 | *
35 | */
36 |
37 | final public class MSDRadixSort extends Sort {
38 | public MSDRadixSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
39 | super(delayOps, markOps, readOps, writeOps);
40 |
41 | this.setSortPromptID("MSD Radix");
42 | //this.setRunAllID("Most Significant Digit Radix Sort");
43 | this.setRunAllID("Most Significant Digit Radix Sort, Base 4");
44 | this.setReportSortID("Most Significant Digit Radixsort");
45 | this.setCategory("Distributive Sorts");
46 | this.isComparisonBased(false);
47 | this.isBucketSort(true);
48 | this.isRadixSort(true);
49 | this.isUnreasonablySlow(false);
50 | this.setUnreasonableLimit(0);
51 | this.isBogoSort(false);
52 | }
53 |
54 | private void radixMSD(int[] array, int length, int min, int max, int radix, int pow) {
55 | if(min >= max || pow < 0)
56 | return;
57 |
58 | Highlights.markArray(2, max - 1);
59 | Highlights.markArray(3, min);
60 |
61 | @SuppressWarnings("unchecked")
62 | ArrayList[] registers = new ArrayList[radix];
63 |
64 | for(int i = 0; i < radix; i++)
65 | registers[i] = new ArrayList<>();
66 |
67 | for(int i = min; i < max; i++) {
68 | Highlights.markArray(1, i);
69 |
70 | int digit = Reads.getDigit(array[i], pow, radix);
71 | registers[digit].add(array[i]);
72 |
73 | Writes.mockWrite(length, digit, array[i], 1);
74 | }
75 |
76 | Highlights.clearMark(2);
77 | Highlights.clearMark(3);
78 |
79 | Writes.transcribeMSD(array, registers, 0, min, 0.8, true, false);
80 |
81 | int sum = 0;
82 | for(int i = 0; i < registers.length; i++) {
83 | this.radixMSD(array, length, sum + min, sum + min + registers[i].size(), radix, pow-1);
84 |
85 | sum += registers[i].size();
86 | registers[i].clear();
87 | Writes.changeTempWrites(registers[i].size());
88 | }
89 | }
90 |
91 | @Override
92 | public void runSort(int[] array, int length, int bucketCount) {
93 | int highestpower = Reads.analyzeMaxLog(array, length, bucketCount, 0.5, true);
94 |
95 | radixMSD(array, length, 0, length, bucketCount, highestpower);
96 | }
97 | }
--------------------------------------------------------------------------------
/src/sorts/MaxHeapSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.HeapSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org.
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.2
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | final public class MaxHeapSort extends HeapSorting {
22 | public MaxHeapSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Max Heap");
26 | this.setRunAllID("Max Heap Sort");
27 | this.setReportSortID("Heapsort");
28 | this.setCategory("Selection Sorts");
29 | this.isComparisonBased(true);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | public void customHeapSort(int[] array, int start, int length, double sleep) {
38 | this.heapSort(array, start, length, sleep, true);
39 | }
40 |
41 | @Override
42 | public void runSort(int[] array, int length, int bucketCount) {
43 | this.heapSort(array, 0, length, 1, true);
44 | }
45 | }
--------------------------------------------------------------------------------
/src/sorts/MergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.MergeSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class MergeSort extends MergeSorting {
36 | public MergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Merge");
40 | this.setRunAllID("Merge Sort");
41 | this.setReportSortID("Mergesort");
42 | this.setCategory("Merge Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | this.mergeSort(array, length, false);
54 | }
55 | }
--------------------------------------------------------------------------------
/src/sorts/MinHeapSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.HeapSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | Copyright (c) rosettacode.org.
12 | Permission is granted to copy, distribute and/or modify this document
13 | under the terms of the GNU Free Documentation License, Version 1.2
14 | or any later version published by the Free Software Foundation;
15 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
16 | Texts. A copy of the license is included in the section entitled "GNU
17 | Free Documentation License".
18 | *
19 | */
20 |
21 | final public class MinHeapSort extends HeapSorting {
22 | public MinHeapSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Min Heap");
26 | this.setRunAllID("Min Heap Sort");
27 | this.setReportSortID("Reverse Heapsort");
28 | this.setCategory("Selection Sorts");
29 | this.isComparisonBased(true);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | @Override
38 | public void runSort(int[] array, int length, int bucketCount) {
39 | this.heapSort(array, 0, length, 1, false);
40 | }
41 | }
--------------------------------------------------------------------------------
/src/sorts/OddEvenMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Odd-Even Merge Sort was taken from here, written by H.W. Lang:
11 | * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/networks/oemen.htm
12 | */
13 |
14 | final public class OddEvenMergeSort extends Sort {
15 | public OddEvenMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Odd-Even Merge");
19 | this.setRunAllID("Batcher's Odd-Even Merge Sort");
20 | this.setReportSortID("Odd-Even Mergesort");
21 | this.setCategory("Concurrent Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(false);
26 | this.setUnreasonableLimit(0);
27 | this.isBogoSort(false);
28 | }
29 |
30 | private void oddEvenMergeCompare(int[] array, int i, int j) {
31 | if (Reads.compare(array[i], array[j]) > 0)
32 | Writes.swap(array, i, j, 1, true, false);
33 | }
34 |
35 | /** lo is the starting position and
36 | * n is the length of the piece to be merged,
37 | * r is the distance of the elements to be compared
38 | */
39 | private void oddEvenMerge(int[] array, int lo, int n, int r) {
40 | int m = r * 2;
41 | if (m < n) {
42 | this.oddEvenMerge(array, lo, n, m); // even subsequence
43 | this.oddEvenMerge(array, lo+r, n, m); // odd subsequence
44 |
45 | for (int i = lo + r; i + r < lo + n; i += m) {
46 | Highlights.markArray(1, i);
47 | Highlights.markArray(2, i + r);
48 | this.oddEvenMergeCompare(array, i, i + r);
49 | }
50 | }
51 | else {
52 | Highlights.markArray(1, lo + r);
53 | Highlights.markArray(2, lo);
54 | this.oddEvenMergeCompare(array, lo, lo+r);
55 | }
56 | }
57 |
58 | private void oddEvenMergeSort(int[] array, int lo, int n) {
59 | if (n > 1) {
60 | int m = n / 2;
61 | this.oddEvenMergeSort(array, lo, m);
62 | this.oddEvenMergeSort(array, lo + m, m);
63 | this.oddEvenMerge(array, lo, n, 1);
64 | }
65 | }
66 |
67 | @Override
68 | public void runSort(int[] array, int currentLength, int bucketCount) {
69 | this.oddEvenMergeSort(array, 0, currentLength);
70 | }
71 | }
--------------------------------------------------------------------------------
/src/sorts/OddEvenSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Odd-Even Sort was taken from here, written by Rachit Belwariar:
11 | * https://www.geeksforgeeks.org/odd-even-sort-brick-sort/
12 | */
13 |
14 | final public class OddEvenSort extends Sort {
15 | public OddEvenSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Odd-Even");
19 | this.setRunAllID("Odd-Even Sort");
20 | this.setReportSortID("Odd-Even Sort");
21 | this.setCategory("Exchange Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(false);
26 | this.setUnreasonableLimit(0);
27 | this.isBogoSort(false);
28 | }
29 |
30 | @Override
31 | public void runSort(int[] array, int length, int bucketCount) {
32 | boolean sorted = false;
33 |
34 | while (!sorted) {
35 | sorted = true;
36 |
37 | for (int i = 1; i < length - 1; i += 2) {
38 | if(Reads.compare(array[i], array[i + 1]) == 1) {
39 | Writes.swap(array, i, i + 1, 0.075, true, false);
40 | sorted = false;
41 | }
42 |
43 | Highlights.markArray(1, i);
44 | Delays.sleep(0.025);
45 | }
46 |
47 | for (int i = 0; i < length - 1; i += 2) {
48 | if(Reads.compare(array[i], array[i + 1]) == 1) {
49 | Writes.swap(array, i, i + 1, 0.075, true, false);
50 | sorted = false;
51 | }
52 |
53 | Highlights.markArray(2, i);
54 | Delays.sleep(0.025);
55 | }
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/src/sorts/PancakeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * IDeserve
11 | * https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve
12 | * Given an array, sort the array using Pancake sort.
13 | *
14 | * @author Saurabh
15 | * https://www.ideserve.co.in/learn/pancake-sorting
16 | */
17 |
18 | final public class PancakeSort extends Sort {
19 | public PancakeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
20 | super(delayOps, markOps, readOps, writeOps);
21 |
22 | this.setSortPromptID("Pancake");
23 | this.setRunAllID("Pancake Sorting");
24 | this.setReportSortID("Pancake Sort");
25 | this.setCategory("Miscellaneous Sorts");
26 | this.isComparisonBased(true);
27 | this.isBucketSort(false);
28 | this.isRadixSort(false);
29 | this.isUnreasonablySlow(false);
30 | this.setUnreasonableLimit(0);
31 | this.isBogoSort(false);
32 | }
33 |
34 | private boolean sorted(int[] array, int length) {
35 | for(int i = 0; i < length; i++) {
36 | Highlights.markArray(1, i);
37 | Delays.sleep(0.025);
38 |
39 | if(Reads.compare(array[i], array[i + 1]) > 0) return false;
40 | }
41 | return true;
42 | }
43 |
44 | private int findMax(int[] arr, int end) {
45 | int index = 0, max = Integer.MIN_VALUE;
46 | for (int i = 0; i <= end; i++) {
47 | Highlights.markArray(1, i);
48 |
49 | if (Reads.compare(arr[i], max) == 1) {
50 | max = arr[i];
51 | index = i;
52 | Highlights.markArray(2, i);
53 | }
54 |
55 | Delays.sleep(0.025);
56 | Highlights.clearMark(1);
57 | }
58 | return index;
59 | }
60 |
61 | @Override
62 | public void runSort(int[] array, int length, int bucketCount) {
63 | for (int i = length - 1; i >= 0; i--) {
64 | if(!this.sorted(array, i)) {
65 | int index = this.findMax(array, i);
66 |
67 | if(index == 0) {
68 | Writes.reversal(array, 0, i, 0.05, true, false);
69 | }
70 | else if(index != i) {
71 | Writes.reversal(array, 0, index, 0.05, true, false);
72 | Writes.reversal(array, 0, i, 0.05, true, false);
73 | }
74 | }
75 | else break;
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/src/sorts/PigeonholeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE").
11 | * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS
12 | * LICENSE OR COPYRIGHT LAW IS PROHIBITED.
13 | *
14 | * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE.
15 | * TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN
16 | * CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
17 | */
18 |
19 | // Code refactored from the Python implementation found here: https://en.wikipedia.org/wiki/Pigeonhole_sort
20 |
21 | final public class PigeonholeSort extends Sort {
22 | public PigeonholeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
23 | super(delayOps, markOps, readOps, writeOps);
24 |
25 | this.setSortPromptID("Pigeonhole");
26 | this.setRunAllID("Pigeonhole Sort");
27 | this.setReportSortID("Pigeonhole Sort");
28 | this.setCategory("Distributive Sorts");
29 | this.isComparisonBased(false);
30 | this.isBucketSort(false);
31 | this.isRadixSort(false);
32 | this.isUnreasonablySlow(false);
33 | this.setUnreasonableLimit(0);
34 | this.isBogoSort(false);
35 | }
36 |
37 | @Override
38 | public void runSort(int[] array, int length, int bucketCount) {
39 | int min = Integer.MAX_VALUE;
40 | int max = Integer.MIN_VALUE;
41 |
42 | for(int i = 0; i < length; i++) {
43 | if(array[i] < min) {
44 | min = array[i];
45 | }
46 | if(array[i] > max) {
47 | max = array[i];
48 | }
49 | }
50 |
51 | int mi = min;
52 | int size = max - mi + 1;
53 | int[] holes = new int[size];
54 |
55 | for(int x = 0; x < length; x++) {
56 | Writes.write(holes, array[x] - mi, holes[array[x] - mi] + 1, 1, false, true);
57 | Highlights.markArray(1, x);
58 | }
59 |
60 | int j = 0;
61 |
62 | for(int count = 0; count < size; count++) {
63 | while(holes[count] > 0) {
64 | Writes.write(holes, count, holes[count] - 1, 0, false, true);
65 | Writes.write(array, j, count + mi, 1, false, false);
66 |
67 | Highlights.markArray(1, j);
68 | j++;
69 | }
70 | }
71 | }
72 | }
--------------------------------------------------------------------------------
/src/sorts/RecursiveBinaryQuickSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.BinaryQuickSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /**
10 | * Binary MSD Radix Sort / Binary Quicksort.
11 | *
12 | * Implemented as recursive decent, and via task queue, see:
13 | * * binaryQuickSortRecursive, and
14 | * * binaryQuickSort respectively.
15 | *
16 | * Both of which are in-place sorting algorithms, with the recursive utilizing
17 | * the stack for divide-and-conquer, while the non-recursive utilizes a queue.
18 | *
19 | * Can be extended to support unsigned integers, by sorting the first bit rin
20 | * reverse. Can be made stable at the cost of O(n) memory. Can be parallalized
21 | * to O(log2(n)) subtasks / threads.
22 | *
23 | * @author Skeen
24 | */
25 |
26 | final public class RecursiveBinaryQuickSort extends BinaryQuickSorting {
27 | public RecursiveBinaryQuickSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
28 | super(delayOps, markOps, readOps, writeOps);
29 |
30 | this.setSortPromptID("Recurs. Binary Quick");
31 | this.setRunAllID("Recursive Binary Quick Sort");
32 | this.setReportSortID("Recursive Binary Quicksort");
33 | this.setCategory("Distribution Sorts");
34 | this.isComparisonBased(false);
35 | this.isBucketSort(false);
36 | this.isRadixSort(false);
37 | this.isUnreasonablySlow(false);
38 | this.setUnreasonableLimit(0);
39 | this.isBogoSort(false);
40 | }
41 |
42 | @Override
43 | public void runSort(int[] array, int length, int bucketCount) {
44 | int mostSignificantBit = Reads.analyzeBit(array, length);
45 | this.binaryQuickSortRecursive(array, 0, length - 1, mostSignificantBit);
46 | }
47 | }
--------------------------------------------------------------------------------
/src/sorts/RecursiveBitonicSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Bitonic Sort was taken from here, written by H.W. Lang:
11 | * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
12 | */
13 |
14 | final public class RecursiveBitonicSort extends Sort {
15 | private boolean direction = true;
16 |
17 | public RecursiveBitonicSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
18 | super(delayOps, markOps, readOps, writeOps);
19 |
20 | this.setSortPromptID("Recursive Bitonic");
21 | this.setRunAllID("Batcher's Bitonic Sort");
22 | this.setReportSortID("Recursive Bitonic Sort");
23 | this.setCategory("Concurrent Sorts");
24 | this.isComparisonBased(true);
25 | this.isBucketSort(false);
26 | this.isRadixSort(false);
27 | this.isUnreasonablySlow(false);
28 | this.setUnreasonableLimit(0);
29 | this.isBogoSort(false);
30 | }
31 |
32 | private static int greatestPowerOfTwoLessThan(int n){
33 | int k = 1;
34 | while (k < n) {
35 | k = k << 1;
36 | }
37 | return k >> 1;
38 | }
39 |
40 | private void compare(int[] A, int i, int j, boolean dir)
41 | {
42 | Highlights.markArray(1, i);
43 | Highlights.markArray(2, j);
44 |
45 | Delays.sleep(0.5);
46 |
47 | int cmp = Reads.compare(A[i], A[j]);
48 |
49 | if (dir == (cmp == 1)) Writes.swap(A, i, j, 1, true, false);
50 | }
51 |
52 | private void bitonicMerge(int[] A, int lo, int n, boolean dir)
53 | {
54 | if (n > 1)
55 | {
56 | int m = RecursiveBitonicSort.greatestPowerOfTwoLessThan(n);
57 |
58 | for (int i = lo; i < lo + n - m; i++) {
59 | this.compare(A, i, i+m, dir);
60 | }
61 |
62 | this.bitonicMerge(A, lo, m, dir);
63 | this.bitonicMerge(A, lo + m, n - m, dir);
64 | }
65 | }
66 |
67 | private void bitonicSort(int[] A, int lo, int n, boolean dir)
68 | {
69 | if (n > 1)
70 | {
71 | int m = n / 2;
72 | this.bitonicSort(A, lo, m, !dir);
73 | this.bitonicSort(A, lo + m, n - m, dir);
74 | this.bitonicMerge(A, lo, n, dir);
75 | }
76 | }
77 |
78 | public void changeDirection(String choice) throws Exception {
79 | if(choice.equals("forward")) this.direction = true;
80 | else if(choice.equals("backward")) this.direction = false;
81 | else throw new Exception("Invalid direction for Bitonic Sort!");
82 | }
83 |
84 | @Override
85 | public void runSort(int[] array, int currentLength, int bucketCount) {
86 | this.bitonicSort(array, 0, currentLength, this.direction);
87 | }
88 | }
--------------------------------------------------------------------------------
/src/sorts/RecursiveOddEvenMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * This version of Odd-Even Merge Sort was taken from here, written by H.W. Lang:
11 | * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/networks/oemen.htm
12 | */
13 |
14 | final public class RecursiveOddEvenMergeSort extends Sort {
15 | public RecursiveOddEvenMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
16 | super(delayOps, markOps, readOps, writeOps);
17 |
18 | this.setSortPromptID("Rec. Odd-Even Merge");
19 | this.setRunAllID("Batcher's Odd-Even Merge Sort");
20 | this.setReportSortID("Recursive Odd-Even Mergesort");
21 | this.setCategory("Concurrent Sorts");
22 | this.isComparisonBased(true);
23 | this.isBucketSort(false);
24 | this.isRadixSort(false);
25 | this.isUnreasonablySlow(false);
26 | this.setUnreasonableLimit(0);
27 | this.isBogoSort(false);
28 | }
29 |
30 | private void oddEvenMergeCompare(int[] array, int i, int j) {
31 | Highlights.markArray(1, i);
32 | Highlights.markArray(2, j);
33 | Delays.sleep(1);
34 | if (Reads.compare(array[i], array[j]) > 0)
35 | Writes.swap(array, i, j, 1, true, false);
36 | }
37 |
38 | /** lo is the starting position and
39 | * n is the length of the piece to be merged,
40 | * r is the distance of the elements to be compared
41 | */
42 | private void oddEvenMerge(int[] array, int lo, int n, int r) {
43 | int m = r * 2;
44 | if (m < n) {
45 | this.oddEvenMerge(array, lo, n, m); // even subsequence
46 | this.oddEvenMerge(array, lo+r, n, m); // odd subsequence
47 |
48 | for (int i = lo + r; i + r < lo + n; i += m) {
49 | Highlights.markArray(1, i);
50 | Highlights.markArray(2, i + r);
51 | this.oddEvenMergeCompare(array, i, i + r);
52 | }
53 | }
54 | else {
55 | Highlights.markArray(1, lo + r);
56 | Highlights.markArray(2, lo);
57 | this.oddEvenMergeCompare(array, lo, lo+r);
58 | }
59 | }
60 |
61 | private void oddEvenMergeSort(int[] array, int lo, int n) {
62 | if (n > 1) {
63 | int m = n / 2;
64 | this.oddEvenMergeSort(array, lo, m);
65 | this.oddEvenMergeSort(array, lo + m, m);
66 | this.oddEvenMerge(array, lo, n, 1);
67 | }
68 | }
69 |
70 | @Override
71 | public void runSort(int[] array, int currentLength, int bucketCount) {
72 | this.oddEvenMergeSort(array, 0, currentLength);
73 | }
74 | }
--------------------------------------------------------------------------------
/src/sorts/RecursivePairwiseSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 | Copyright (c) 2019 PiotrGrochowski
13 | Permission is hereby granted, free of charge, to any person obtaining a copy
14 | of this software and associated documentation files (the "Software"), to deal
15 | in the Software without restriction, including without limitation the rights
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 | copies of the Software, and to permit persons to whom the Software is
18 | furnished to do so, subject to the following conditions:
19 | The above copyright notice and this permission notice shall be included in all
20 | copies or substantial portions of the Software.
21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 | SOFTWARE.
28 | *
29 | */
30 |
31 | final public class RecursivePairwiseSort extends Sort {
32 | public RecursivePairwiseSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
33 | super(delayOps, markOps, readOps, writeOps);
34 |
35 | this.setSortPromptID("Recursive Pairwise");
36 | this.setRunAllID("Recursive Pairwise Sorting Network");
37 | this.setReportSortID("Recursive Pairwise Sort");
38 | this.setCategory("Concurrent Sorts");
39 | this.isComparisonBased(true);
40 | this.isBucketSort(false);
41 | this.isRadixSort(false);
42 | this.isUnreasonablySlow(false);
43 | this.setUnreasonableLimit(0);
44 | this.isBogoSort(false);
45 | }
46 |
47 | private void pairwiserecursive(int[] array, int start, int end, int gap, double sleep) {
48 | if (start == end - gap){
49 | return;
50 | }
51 | int b = start + gap;
52 | while (b < end){
53 | Delays.sleep(sleep);
54 | Highlights.markArray(1, b - gap);
55 | Highlights.markArray(2, b);
56 | if(Reads.compare(array[b - gap], array[b]) == 1) {
57 | Writes.swap(array, b - gap, b, sleep, true, false);
58 | }
59 | b += (2 * gap);
60 | }
61 | if (((end - start) / gap)%2 == 0){
62 | this.pairwiserecursive(array, start, end, gap * 2, 1);
63 | this.pairwiserecursive(array, start + gap, end + gap, gap * 2, 1);
64 | }
65 | else{
66 | this.pairwiserecursive(array, start, end + gap, gap * 2, 1);
67 | this.pairwiserecursive(array, start + gap, end, gap * 2, 1);
68 | }
69 | int a = 1;
70 | while (a < ((end - start) / gap)){
71 | a = (a * 2) + 1;
72 | }
73 | b = start + gap;
74 | while (b + gap < end){
75 | int c = a;
76 | while (c > 1){
77 | c /= 2;
78 | if (b + (c * gap) < end){
79 | Delays.sleep(sleep);
80 | Highlights.markArray(1, b);
81 | Highlights.markArray(2, b + (c * gap));
82 | if(Reads.compare(array[b], array[b + (c * gap)]) == 1) {
83 | Writes.swap(array, b, b + (c * gap), sleep, true, false);
84 | }
85 | }
86 | }
87 | b += (2 * gap);
88 | }
89 | }
90 |
91 | @Override
92 | public void runSort(int[] array, int length, int bucketCount) {
93 | this.pairwiserecursive(array, 0, length, 1, 1);
94 | }
95 | }
--------------------------------------------------------------------------------
/src/sorts/RotateMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.GrailSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | The MIT License (MIT)
12 |
13 | Copyright (c) 2013 Andrey Astrelin
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy of
16 | this software and associated documentation files (the "Software"), to deal in
17 | the Software without restriction, including without limitation the rights to
18 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19 | the Software, and to permit persons to whom the Software is furnished to do so,
20 | subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 | *
32 | */
33 |
34 | /********* Classic in-place merge ********/
35 | /* */
36 | /* (c) 2013 by Andrey Astrelin */
37 | /* Refactored by MusicTheorist */
38 | /* */
39 | /* Classic implementation of in-place */
40 | /* merge sort; uses binary search and */
41 | /* rotations */
42 | /* */
43 | /* Copied from GrailSort.h */
44 | /* */
45 | /*****************************************/
46 |
47 | final public class RotateMergeSort extends GrailSorting {
48 | public RotateMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
49 | super(delayOps, markOps, readOps, writeOps);
50 |
51 | this.setSortPromptID("Rotate Merge");
52 | this.setRunAllID("Rotate Merge Sort");
53 | //this.setRunAllID("In-Place Merge Sort with Rotations");
54 | this.setReportSortID("In-Place Rotate Mergesort");
55 | this.setCategory("Merge Sorts");
56 | this.isComparisonBased(true);
57 | this.isBucketSort(false);
58 | this.isRadixSort(false);
59 | this.isUnreasonablySlow(false);
60 | this.setUnreasonableLimit(0);
61 | this.isBogoSort(false);
62 | }
63 |
64 | public void customRotateMerge(int[] array, int start, int end) {
65 | this.grailInPlaceMergeSort(array, start, end);
66 | }
67 |
68 | @Override
69 | public void runSort(int[] array, int length, int bucketCount) {
70 | this.grailInPlaceMergeSort(array, 0, length);
71 | }
72 | }
--------------------------------------------------------------------------------
/src/sorts/SelectionSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class SelectionSort extends Sort {
36 | public SelectionSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Selection");
40 | this.setRunAllID("Selection Sort");
41 | this.setReportSortID("Selection Sort");
42 | this.setCategory("Selection Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | for (int i = 0; i < length - 1; i++) {
54 | int lowestindex = i;
55 |
56 | for (int j = i + 1; j < length; j++) {
57 | Highlights.markArray(2, j);
58 | Delays.sleep(0.01);
59 |
60 | if (Reads.compare(array[j], array[lowestindex]) == -1){
61 | lowestindex = j;
62 | Highlights.markArray(1, lowestindex);
63 | Delays.sleep(0.01);
64 | }
65 | }
66 | Writes.swap(array, i, lowestindex, 0.02, true, false);
67 | }
68 | }
69 | }
--------------------------------------------------------------------------------
/src/sorts/ShatterSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.ShatterSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class ShatterSort extends ShatterSorting {
36 | public ShatterSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Shatter");
40 | this.setRunAllID("Shatter Sort");
41 | this.setReportSortID("Shatter Sort");
42 | this.setCategory("Distributive Sorts");
43 | this.isComparisonBased(false);
44 | this.isBucketSort(true);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | this.shatterSort(array, length, bucketCount);
54 | }
55 | }
--------------------------------------------------------------------------------
/src/sorts/ShellSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.ShellSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | // Shell sort variant retrieved from:
10 | // https://www.cs.princeton.edu/~rs/talks/shellsort.ps
11 |
12 | final public class ShellSort extends ShellSorting {
13 | public ShellSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
14 | super(delayOps, markOps, readOps, writeOps);
15 |
16 | this.setSortPromptID("Shell");
17 | this.setRunAllID("Shell Sort");
18 | this.setReportSortID("Shellsort");
19 | this.setCategory("Insertion Sorts");
20 | this.isComparisonBased(true);
21 | this.isBucketSort(false);
22 | this.isRadixSort(false);
23 | this.isUnreasonablySlow(false);
24 | this.setUnreasonableLimit(0);
25 | this.isBogoSort(false);
26 | }
27 |
28 | public void finishQuickShell(int[] array, int currentLen) {
29 | this.quickShellSort(array, 0, currentLen);
30 | }
31 |
32 | @Override
33 | public void runSort(int[] array, int currentLength, int bucketCount) {
34 | this.shellSort(this.ArrayVisualizer, array, currentLength);
35 | }
36 | }
--------------------------------------------------------------------------------
/src/sorts/SillySort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | // Written by Tom Duff, and found here: http://home.tiac.net/~cri_d/cri/2001/badsort.html
10 | // from https://stackoverflow.com/questions/2609857/are-there-any-worse-sorting-algorithms-than-bogosort-a-k-a-monkey-sort/
11 |
12 | final public class SillySort extends Sort {
13 | public SillySort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
14 | super(delayOps, markOps, readOps, writeOps);
15 |
16 | this.setSortPromptID("Silly");
17 | this.setRunAllID("Silly Sort");
18 | this.setReportSortID("Sillysort");
19 | this.setCategory("Exchange Sorts");
20 | this.isComparisonBased(true);
21 | this.isBucketSort(false);
22 | this.isRadixSort(false);
23 | this.isUnreasonablySlow(true);
24 | this.setUnreasonableLimit(512);
25 | this.isBogoSort(false);
26 | }
27 |
28 | private void sillySort(int[] array, int i, int j) {
29 | int m;
30 |
31 | Delays.sleep(1);
32 |
33 | if (i < j) {
34 | /* find the middle of the array */
35 | m = i + ((j - i) / 2);
36 |
37 | /*
38 | * use this function (recursively) to find put the minimum elements of
39 | * each half into the first elements of each half
40 | */
41 | this.sillySort(array, i, m);
42 | this.sillySort(array, m + 1, j);
43 |
44 | /*
45 | * Choose the smallest element of the two halves, and put that element in
46 | * the first position
47 | */
48 | if (Reads.compare(array[i], array[m + 1]) >= 0) {
49 | Writes.swap(array, i, m + 1, 1, true, false);
50 | }
51 |
52 | Highlights.markArray(1, i);
53 | Highlights.markArray(2, m + 1);
54 |
55 | this.sillySort(array, i + 1, j);
56 | }
57 | }
58 |
59 | @Override
60 | public void runSort(int[] array, int currentLength, int bucketCount) {
61 | this.sillySort(array, 0, currentLength - 1);
62 | }
63 | }
--------------------------------------------------------------------------------
/src/sorts/SimpleShatterSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.ShatterSorting;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class SimpleShatterSort extends ShatterSorting {
36 | public SimpleShatterSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Simple Shatter");
40 | this.setRunAllID("Simple Shatter Sort");
41 | this.setReportSortID("Simple Shatter Sort");
42 | this.setCategory("Distributive Sorts");
43 | this.isComparisonBased(false);
44 | this.isBucketSort(true);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | this.simpleShatterSort(array, length, bucketCount, (int) (Math.log(length) / Math.log(2)) / 2);
54 | }
55 | }
--------------------------------------------------------------------------------
/src/sorts/SlowSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | // Code refactored from Python: http://wiki.c2.com/?SlowSort
10 |
11 | final public class SlowSort extends Sort {
12 | public SlowSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
13 | super(delayOps, markOps, readOps, writeOps);
14 |
15 | this.setSortPromptID("Slow");
16 | this.setRunAllID("Slow Sort");
17 | this.setReportSortID("Slowsort");
18 | this.setCategory("Exchange Sorts");
19 | this.isComparisonBased(true);
20 | this.isBucketSort(false);
21 | this.isRadixSort(false);
22 | this.isUnreasonablySlow(true);
23 | this.setUnreasonableLimit(512);
24 | this.isBogoSort(false);
25 | }
26 |
27 | private void slowSort(int[] A, int i, int j) {
28 | Delays.sleep(1);
29 |
30 | if (i >= j) {
31 | return;
32 | }
33 |
34 | int m = i + ((j - i) / 2);
35 |
36 | this.slowSort(A, i, m);
37 | this.slowSort(A, m + 1, j);
38 |
39 | if (Reads.compare(A[m], A[j]) == 1) {
40 | Writes.swap(A, m, j, 1, true, false);
41 | }
42 |
43 | Highlights.markArray(1, j);
44 | Highlights.markArray(2, m);
45 |
46 | this.slowSort(A, i, j - 1);
47 | }
48 |
49 | @Override
50 | public void runSort(int[] array, int currentLength, int bucketCount) {
51 | this.slowSort(array, 0, currentLength - 1);
52 | }
53 | }
--------------------------------------------------------------------------------
/src/sorts/SmartBubbleSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class SmartBubbleSort extends Sort {
36 | public SmartBubbleSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Smart Bubble");
40 | this.setRunAllID("Optimized Bubble Sort");
41 | this.setReportSortID("Optimized Bubblesort");
42 | this.setCategory("Exchange Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | @Override
52 | public void runSort(int[] array, int length, int bucketCount) {
53 | for(int i = length - 1; i > 0; i--) {
54 | boolean sorted = true;
55 | for(int j = 0; j < i; j++) {
56 | if(Reads.compare(array[j], array[j + 1]) == 1){
57 | Writes.swap(array, j, j + 1, 0.075, true, false);
58 | sorted = false;
59 | }
60 |
61 | Highlights.markArray(1, j);
62 | Highlights.markArray(2, j + 1);
63 | Delays.sleep(0.025);
64 | }
65 | if(sorted) break;
66 | }
67 | }
68 | }
--------------------------------------------------------------------------------
/src/sorts/SmartCocktailSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class SmartCocktailSort extends Sort {
36 | public SmartCocktailSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Smart Cocktail");
40 | this.setRunAllID("Optimized Cocktail Shaker Sort");
41 | this.setReportSortID("Optimized Cocktail Shaker Sort");
42 | this.setCategory("Exchange Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | private void smartCocktailShaker(int[] array, int start, int end, double sleep) {
52 | int i = start;
53 | while(i < ((end / 2) + start)) {
54 | boolean sorted = true;
55 | for(int j = i; j < end + start - i - 1; j++) {
56 | if(Reads.compare(array[j], array[j + 1]) == 1) {
57 | Writes.swap(array, j, j + 1, sleep, true, false);
58 | sorted = false;
59 | }
60 |
61 | Highlights.markArray(1, j);
62 | Highlights.markArray(2, j + 1);
63 |
64 | Delays.sleep(sleep / 2);
65 | }
66 | for(int j = end + start - i - 1; j > i; j--){
67 | if(Reads.compare(array[j], array[j - 1]) == -1) {
68 | Writes.swap(array, j, j - 1, sleep, true, false);
69 | sorted = false;
70 | }
71 |
72 | Highlights.markArray(1, j);
73 | Highlights.markArray(2, j - 1);
74 |
75 | Delays.sleep(sleep / 2);
76 | }
77 | if(sorted) break;
78 | else i++;
79 | }
80 | }
81 |
82 | public void customSort(int[] array, int start, int end) {
83 | this.smartCocktailShaker(array, start, end, 1);
84 | }
85 |
86 | @Override
87 | public void runSort(int[] array, int length, int bucketCount) {
88 | this.smartCocktailShaker(array, 0, length, 0.1);
89 | }
90 | }
--------------------------------------------------------------------------------
/src/sorts/SmartGnomeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class SmartGnomeSort extends Sort {
10 | public SmartGnomeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Smart Gnome");
14 | this.setRunAllID("Optimized Gnome Sort");
15 | this.setReportSortID("Optimized Gnomesort");
16 | this.setCategory("Exchange Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | // Taken from https://en.wikipedia.org/wiki/Gnome_sort
26 | private void smartGnomeSort(int[] array, int upperBound, double sleep) {
27 | int pos = upperBound;
28 |
29 | while(pos > 0 && Reads.compare(array[pos - 1], array[pos]) == 1) {
30 | Writes.swap(array, pos - 1, pos, sleep, true, false);
31 | pos--;
32 | }
33 | }
34 |
35 | public void customSort(int[] array, int low, int high, double sleep) {
36 | for(int i = low + 1; i < high; i++) {
37 | smartGnomeSort(array, i, sleep);
38 | }
39 | }
40 |
41 | @Override
42 | public void runSort(int[] array, int length, int bucketCount) {
43 | for(int i = 1; i < length; i++) {
44 | smartGnomeSort(array, i, 0.05);
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/sorts/StoogeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE").
11 | * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS
12 | * LICENSE OR COPYRIGHT LAW IS PROHIBITED.
13 | *
14 | * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE.
15 | * TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN
16 | * CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
17 | */
18 |
19 | // Code refactored from: https://en.wikipedia.org/wiki/Stooge_sort
20 | final public class StoogeSort extends Sort {
21 | public StoogeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
22 | super(delayOps, markOps, readOps, writeOps);
23 |
24 | this.setSortPromptID("Stooge");
25 | this.setRunAllID("Stooge Sort");
26 | this.setReportSortID("Stoogesort");
27 | this.setCategory("Exchange Sorts");
28 | this.isComparisonBased(true);
29 | this.isBucketSort(false);
30 | this.isRadixSort(false);
31 | this.isUnreasonablySlow(true);
32 | this.setUnreasonableLimit(2048);
33 | this.isBogoSort(false);
34 | }
35 |
36 | private void stoogeSort(int[] A, int i, int j) {
37 | if (Reads.compare(A[i], A[j]) == 1) {
38 | Writes.swap(A, i, j, 0.005, true, false);
39 | }
40 |
41 | Delays.sleep(0.0025);
42 |
43 | Highlights.markArray(1, i);
44 | Highlights.markArray(2, j);
45 |
46 | if (j - i + 1 >= 3) {
47 | int t = (j - i + 1) / 3;
48 |
49 | Highlights.markArray(3, j - t);
50 | Highlights.markArray(4, i + t);
51 |
52 | this.stoogeSort(A, i, j-t);
53 | this.stoogeSort(A, i+t, j);
54 | this.stoogeSort(A, i, j-t);
55 | }
56 | }
57 |
58 | @Override
59 | public void runSort(int[] array, int currentLength, int bucketCount) {
60 | this.stoogeSort(array, 0, currentLength - 1);
61 | }
62 | }
--------------------------------------------------------------------------------
/src/sorts/TernaryHeapSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class TernaryHeapSort extends Sort {
10 | public TernaryHeapSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Ternary Heap");
14 | this.setRunAllID("Ternary Heap Sort");
15 | this.setReportSortID("Ternary Heapsort");
16 | this.setCategory("Selection Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | // TERNARY HEAP SORT - written by qbit
26 | // https://codereview.stackexchange.com/questions/63384/binary-heapsort-and-ternary-heapsort-implementation
27 |
28 | private int heapSize;
29 |
30 | private static int leftBranch(int i) {
31 | return 3 * i + 1;
32 | }
33 |
34 | private static int middleBranch(int i) {
35 | return 3 * i + 2;
36 | }
37 |
38 | private static int rightBranch(int i) {
39 | return 3 * i + 3;
40 | }
41 |
42 | private void maxHeapify(int[] array, int i) {
43 |
44 | int leftChild = TernaryHeapSort.leftBranch(i);
45 | int rightChild = TernaryHeapSort.rightBranch(i);
46 | int middleChild = TernaryHeapSort.middleBranch(i);
47 | int largest;
48 |
49 | largest = leftChild <= heapSize && Reads.compare(array[leftChild], array[i]) > 0 ? leftChild : i;
50 |
51 | if(rightChild <= heapSize && Reads.compare(array[rightChild], array[largest]) > 0) {
52 | largest = rightChild;
53 | }
54 |
55 | if(middleChild <= heapSize && Reads.compare(array[middleChild], array[largest]) > 0) {
56 | largest = middleChild;
57 | }
58 |
59 |
60 | if(largest != i) {
61 | Writes.swap(array, i, largest, 1, true, false);
62 | this.maxHeapify(array, largest);
63 | }
64 | }
65 |
66 | private void buildMaxTernaryHeap(int[] array, int length) {
67 | heapSize = length - 1;
68 | for(int i = length - 1 / 3; i >= 0; i--)
69 | this.maxHeapify(array, i);
70 | }
71 |
72 | @Override
73 | public void runSort(int[] array, int length, int bucketCount) {
74 | this.buildMaxTernaryHeap(array, length);
75 |
76 | for(int i = length - 1; i >= 0; i--){
77 | Writes.swap(array, 0, i, 1, true, false); //add last element on array, i.e heap root
78 |
79 | heapSize = heapSize - 1; //shrink heap by 1
80 | this.maxHeapify(array, 0);
81 | }
82 | }
83 | }
--------------------------------------------------------------------------------
/src/sorts/TimSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import templates.TimSorting;
5 | import utils.Delays;
6 | import utils.Highlights;
7 | import utils.Reads;
8 | import utils.Writes;
9 |
10 | /*
11 | * Copyright (C) 2008 The Android Open Source Project
12 | *
13 | * Licensed under the Apache License, Version 2.0 (the "License");
14 | * you may not use this file except in compliance with the License.
15 | * You may obtain a copy of the License at
16 | *
17 | * http://www.apache.org/licenses/LICENSE-2.0
18 | *
19 | * Unless required by applicable law or agreed to in writing, software
20 | * distributed under the License is distributed on an "AS IS" BASIS,
21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 | * See the License for the specific language governing permissions and
23 | * limitations under the License.
24 | */
25 |
26 | public class TimSort extends Sort {
27 | private TimSorting timSortInstance; // TimSort cannot be simply written off as an abstract class, as it creates an instance of itself
28 | // in order to track its state. Plus, it contains both instance and static methods, requiring even
29 | // more refactoring, which would be just doing unnecessary busy work. Instead of what we've done for
30 | // the rest of the algorithms, we'll favor composition over inheritance here and pass "util" objects
31 | // to it.
32 |
33 | public TimSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
34 | super(delayOps, markOps, readOps, writeOps);
35 |
36 | this.setSortPromptID("Tim");
37 | this.setRunAllID("Tim Sort");
38 | this.setReportSortID("Timsort");
39 | this.setCategory("Hybrid Sorts");
40 | this.isComparisonBased(true);
41 | this.isBucketSort(false);
42 | this.isRadixSort(false);
43 | this.isUnreasonablySlow(false);
44 | this.setUnreasonableLimit(0);
45 | this.isBogoSort(false);
46 | }
47 |
48 | @Override
49 | public void runSort(int[] array, int currentLength, int bucketCount) {
50 | this.timSortInstance = new TimSorting(array, currentLength, this.Delays, this.Highlights, this.Reads, this.Writes);
51 |
52 | TimSorting.sort(this.timSortInstance, array, currentLength);
53 | }
54 | }
--------------------------------------------------------------------------------
/src/sorts/TreeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | final public class TreeSort extends Sort {
10 | public TreeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
11 | super(delayOps, markOps, readOps, writeOps);
12 |
13 | this.setSortPromptID("Tree");
14 | this.setRunAllID("Tree Sort (Unbalanced)");
15 | this.setReportSortID("Treesort");
16 | this.setCategory("Insertion Sorts");
17 | this.isComparisonBased(true);
18 | this.isBucketSort(false);
19 | this.isRadixSort(false);
20 | this.isUnreasonablySlow(false);
21 | this.setUnreasonableLimit(0);
22 | this.isBogoSort(false);
23 | }
24 |
25 | // Code retrieved from https://www.geeksforgeeks.org/tree-sort/
26 |
27 | final private class Node {
28 | int key;
29 | Node left, right;
30 |
31 | public Node(int item) {
32 | key = item;
33 | left = right = null;
34 | }
35 | }
36 |
37 | private Node root;
38 | private int index, length;
39 |
40 | private Node treeWrite(Node element, int at) {
41 | Node node = new Node(0);
42 |
43 | if(at > 0 && at < this.length) Highlights.markArray(1, at - 1);
44 | Writes.changeTempWrites(1);
45 | Writes.startLap();
46 | node = element;
47 | Writes.stopLap();
48 |
49 | Delays.sleep(0.25);
50 |
51 | return node;
52 | }
53 |
54 | private void insert(int key) {
55 | this.root = this.treeWrite(insertRec(this.root, key, 1), 1);
56 | }
57 |
58 | private Node insertRec(Node root, int key, int depth) {
59 | if (root == null) {
60 | root = treeWrite(new Node(key), 1);
61 | return root;
62 | }
63 |
64 | if (Reads.compare(key, root.key) == -1)
65 | root.left = treeWrite(insertRec(root.left, key, depth * 2), depth * 2);
66 | else if (Reads.compare(key, root.key) == 1)
67 | root.right = treeWrite(insertRec(root.right, key, (depth * 2) + 1), (depth * 2) + 1);
68 |
69 | return root;
70 | }
71 |
72 | private void traverseRec(Node root, int[] array) {
73 | if (root != null) {
74 | this.traverseRec(root.left, array);
75 | Writes.write(array, this.index++, root.key, 1, true, false);
76 | this.traverseRec(root.right, array);
77 | }
78 | }
79 |
80 | private void treeIns(int arr[]) {
81 | for(int i = 0; i < this.length; i++) {
82 | Highlights.markArray(2, i);
83 | this.insert(arr[i]);
84 | }
85 | Highlights.clearMark(2);
86 | }
87 |
88 | @Override
89 | public void runSort(int[] array, int currentLength, int bucketCount) {
90 | this.length = currentLength;
91 | this.treeIns(array);
92 | this.index = 0;
93 | this.traverseRec(this.root, array);
94 | }
95 | }
--------------------------------------------------------------------------------
/src/sorts/WeakHeapSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | // Refactored from C++ code written by Manish Bhojasia, found here:
10 | // https://www.sanfoundry.com/cpp-program-implement-weak-heap/
11 | final public class WeakHeapSort extends Sort {
12 | public WeakHeapSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
13 | super(delayOps, markOps, readOps, writeOps);
14 |
15 | this.setSortPromptID("Weak Heap");
16 | this.setRunAllID("Weak Heap Sort");
17 | this.setReportSortID("Weak Heapsort");
18 | this.setCategory("Selection Sorts");
19 | this.isComparisonBased(true);
20 | this.isBucketSort(false);
21 | this.isRadixSort(false);
22 | this.isUnreasonablySlow(false);
23 | this.setUnreasonableLimit(0);
24 | this.isBogoSort(false);
25 | }
26 |
27 | private static int getBitwiseFlag(int[] bits, int x) {
28 | return ((bits[(x) >> 3] >> ((x) & 7)) & 1);
29 | }
30 |
31 | private void toggleBitwiseFlag(int[] bits, int x) {
32 | int flag = bits[(x) >> 3];
33 | flag ^= 1 << ((x) & 7);
34 |
35 | Writes.write(bits, (x) >> 3, flag, 0, true, true);
36 | }
37 |
38 | /*
39 | * Merge Weak Heap
40 | */
41 | private void weakHeapMerge(int[] array, int[] bits, int i, int j) {
42 | if (Reads.compare(array[i], array[j]) == -1)
43 | {
44 | this.toggleBitwiseFlag(bits, j);
45 | Writes.swap(array, i, j, 1, true, false);
46 | }
47 | }
48 |
49 | @Override
50 | public void runSort(int[] array, int length, int bucketCount) {
51 | int n = length;
52 | int i, j, x, y, Gparent;
53 |
54 | int bitsLength = (n + 7) / 8;
55 | int[] bits = new int[bitsLength];
56 |
57 | for (i = 0; i < n / 8; ++i) {
58 | Writes.write(bits, i, 0, 0, false, true);
59 | }
60 |
61 | for (i = n - 1; i > 0; --i) {
62 | j = i;
63 |
64 | while ((j & 1) == WeakHeapSort.getBitwiseFlag(bits, j >> 1))
65 | j >>= 1;
66 | Gparent = j >> 1;
67 |
68 | this.weakHeapMerge(array, bits, Gparent, i);
69 | }
70 |
71 | for (i = n - 1; i >= 2; --i) {
72 | Writes.swap(array, 0, i, 1, true, false);
73 |
74 | x = 1;
75 |
76 | while ((y = 2 * x + WeakHeapSort.getBitwiseFlag(bits, x)) < i)
77 | x = y;
78 |
79 | while (x > 0) {
80 | this.weakHeapMerge(array, bits, 0, x);
81 | x >>= 1;
82 | }
83 | }
84 | Writes.swap(array, 0, 1, 1, true, false);
85 | }
86 | }
--------------------------------------------------------------------------------
/src/sorts/WeaveMergeSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import utils.Delays;
5 | import utils.Highlights;
6 | import utils.Reads;
7 | import utils.Writes;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class WeaveMergeSort extends Sort {
36 | public WeaveMergeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
37 | super(delayOps, markOps, readOps, writeOps);
38 |
39 | this.setSortPromptID("Weave Merge");
40 | this.setRunAllID("Weave Merge Sort");
41 | this.setReportSortID("Weave Mergesort");
42 | this.setCategory("Hybrid Sorts");
43 | this.isComparisonBased(true);
44 | this.isBucketSort(false);
45 | this.isRadixSort(false);
46 | this.isUnreasonablySlow(false);
47 | this.setUnreasonableLimit(0);
48 | this.isBogoSort(false);
49 | }
50 |
51 | private void weaveInsert(int[] arr, int start, int end) {
52 | int pos;
53 |
54 | for(int j = start; j < end; j++){
55 | pos = j;
56 |
57 | Highlights.markArray(1, j);
58 | Highlights.clearMark(2);
59 |
60 | while(pos > start && Reads.compare(arr[pos], arr[pos - 1]) < 1) {
61 | Writes.swap(arr, pos, pos - 1, 0.2, true, false);
62 | pos--;
63 | }
64 | }
65 | }
66 |
67 | private void weaveMerge(int[] arr, int min, int max, int mid) {
68 | int i = 1;
69 | int target = (mid - min);
70 |
71 | while(i <= target) {
72 | Writes.multiSwap(arr, mid + i, min + (i * 2) - 1, 0.05, true, false);
73 | i++;
74 | }
75 |
76 | this.weaveInsert(arr, min, max + 1);
77 | }
78 |
79 | private void weaveMergeSort(int[] array, int min, int max) {
80 | if(max - min == 0) { //only one element.
81 | Delays.sleep(1); //no swap
82 | }
83 | else if(max - min == 1) { //only two elements and swaps them
84 | if(Reads.compare(array[min], array[max]) == 1) {
85 | Writes.swap(array, min, max, 0.01, true, false);
86 | }
87 | }
88 | else {
89 | int mid = (int) Math.floor((min + max) / 2); //The midpoint
90 |
91 | this.weaveMergeSort(array, min, mid); //sort the left side
92 | this.weaveMergeSort(array, mid + 1, max); //sort the right side
93 | this.weaveMerge(array, min, max, mid); //combines them
94 | }
95 | }
96 |
97 | @Override
98 | public void runSort(int[] array, int currentLength, int bucketCount) {
99 | this.weaveMergeSort(array, 0, currentLength - 1);
100 | }
101 | }
--------------------------------------------------------------------------------
/src/sorts/WikiSort.java:
--------------------------------------------------------------------------------
1 | package sorts;
2 |
3 | import templates.Sort;
4 | import templates.WikiSorting;
5 |
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Writes;
10 |
11 | /*
12 | *
13 | This is free and unencumbered software released into the public domain.
14 |
15 | Anyone is free to copy, modify, publish, use, compile, sell, or
16 | distribute this software, either in source code form or as a compiled
17 | binary, for any purpose, commercial or non-commercial, and by any
18 | means.
19 |
20 | In jurisdictions that recognize copyright laws, the author or authors
21 | of this software dedicate any and all copyright interest in the
22 | software to the public domain. We make this dedication for the benefit
23 | of the public at large and to the detriment of our heirs and
24 | successors. We intend this dedication to be an overt act of
25 | relinquishment in perpetuity of all present and future rights to this
26 | software under copyright law.
27 |
28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 | OTHER DEALINGS IN THE SOFTWARE.
35 |
36 | For more information, please refer to
37 | *
38 | */
39 |
40 | public class WikiSort extends Sort {
41 | private WikiSorting wikiSortInstance; // Just like TimSort, WikiSort cannot be simply written off as an abstract class, as it creates an
42 | // instance of itself in order to track its state. Plus, it contains both instance and static methods,
43 | // requiring even more refactoring, which would be just doing unnecessary busy work. Instead of what
44 | // we've done for the rest of the algorithms, we'll favor composition over inheritance here and pass
45 | // "util" objects to it.
46 |
47 | private InsertionSort insertionSort;
48 |
49 | // Cache sizes for WikiSort
50 |
51 | // final private static int halfSize = (currentLen + 1) / 2;
52 | // final private static int squareRoot = (int) (Math.sqrt((currentLen + 1) / 2) + 1);
53 | // final private static int staticBuffer = 32;
54 | // final private static int noBuffer = 0;
55 |
56 | private int cache;
57 |
58 | public WikiSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
59 | super(delayOps, markOps, readOps, writeOps);
60 |
61 | this.setSortPromptID("Wiki");
62 | //this.setRunAllID("Wiki Sort (Block Merge Sort)");
63 | this.setRunAllID("Wiki Sort [Block Merge Sort]");
64 | this.setReportSortID("Wikisort");
65 | this.setCategory("Hybrid Sorts");
66 | this.isComparisonBased(true);
67 | this.isBucketSort(false);
68 | this.isRadixSort(false);
69 | this.isUnreasonablySlow(false);
70 | this.setUnreasonableLimit(0);
71 | this.isBogoSort(false);
72 | }
73 |
74 | @Override
75 | public void runSort(int[] array, int currentLength, int bucketCount) {
76 | this.cache = 0;
77 |
78 | this.insertionSort = new InsertionSort(this.Delays, this.Highlights, this.Reads, this.Writes);
79 | this.wikiSortInstance = new WikiSorting(this.insertionSort, this.Delays, this.Highlights, this.Reads, this.Writes, this.cache);
80 |
81 | WikiSorting.sort(this.wikiSortInstance, array, currentLength);
82 | }
83 | }
--------------------------------------------------------------------------------
/src/soundfont/SFXFetcher.java:
--------------------------------------------------------------------------------
1 | package soundfont;
2 |
3 | import java.io.InputStream;
4 |
5 | final public class SFXFetcher {
6 | private InputStream sfxFile;
7 |
8 | public SFXFetcher() {
9 | this.sfxFile = this.getClass().getResourceAsStream("sfx.sf2");
10 | }
11 |
12 | public InputStream getSFXFile() {
13 | return this.sfxFile;
14 | }
15 | }
--------------------------------------------------------------------------------
/src/soundfont/sfx.sf2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MusicTheorist/ArrayVisualizer/ff9d8ea47b7ea71461e9f2e8bdb142c204ccea14/src/soundfont/sfx.sf2
--------------------------------------------------------------------------------
/src/templates/BinaryInsertionSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | /*
9 | *
10 | MIT License
11 |
12 | Copyright (c) 2019 w0rthy
13 |
14 | Permission is hereby granted, free of charge, to any person obtaining a copy
15 | of this software and associated documentation files (the "Software"), to deal
16 | in the Software without restriction, including without limitation the rights
17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 | copies of the Software, and to permit persons to whom the Software is
19 | furnished to do so, subject to the following conditions:
20 |
21 | The above copyright notice and this permission notice shall be included in all
22 | copies or substantial portions of the Software.
23 |
24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 | SOFTWARE.
31 | *
32 | */
33 |
34 | public abstract class BinaryInsertionSorting extends Sort {
35 | protected BinaryInsertionSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
36 | super(delayOps, markOps, readOps, writeOps);
37 | }
38 |
39 | protected void binaryInsertSort(int[] array, int start, int end, double compSleep, double writeSleep) {
40 | for (int i = start; i < end; i++) {
41 | int num = array[i];
42 | int lo = start, hi = i;
43 |
44 | while (lo < hi) {
45 | int mid = lo + ((hi - lo) / 2); // avoid int overflow!
46 | Highlights.markArray(1, lo);
47 | Highlights.markArray(2, mid);
48 | Highlights.markArray(3, hi);
49 |
50 | Delays.sleep(compSleep);
51 |
52 | if (Reads.compare(num, array[mid]) < 0) { // do NOT move equal elements to right of inserted element; this maintains stability!
53 | hi = mid;
54 | }
55 | else {
56 | lo = mid + 1;
57 | }
58 | }
59 |
60 | Highlights.clearMark(3);
61 |
62 | // item has to go into position lo
63 |
64 | int j = i - 1;
65 |
66 | while (j >= lo)
67 | {
68 | Writes.write(array, j + 1, array[j], writeSleep, true, false);
69 | j--;
70 | }
71 | Writes.write(array, lo, num, writeSleep, true, false);
72 |
73 | Highlights.clearAllMarks();
74 | }
75 | }
76 | }
--------------------------------------------------------------------------------
/src/templates/BinaryQuickSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 |
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Writes;
10 |
11 | /**
12 | * Binary MSD Radix Sort / Binary Quicksort.
13 | *
14 | * Implemented as recursive decent, and via task queue, see:
15 | * * binaryQuickSortRecursive, and
16 | * * binaryQuickSort respectively.
17 | *
18 | * Both of which are in-place sorting algorithms, with the recursive utilizing
19 | * the stack for divide-and-conquer, while the non-recursive utilizes a queue.
20 | *
21 | * Can be extended to support unsigned integers, by sorting the first bit rin
22 | * reverse. Can be made stable at the cost of O(n) memory. Can be parallalized
23 | * to O(log2(n)) subtasks / threads.
24 | *
25 | * @author Skeen
26 | */
27 |
28 | public abstract class BinaryQuickSorting extends Sort {
29 | protected BinaryQuickSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
30 | super(delayOps, markOps, readOps, writeOps);
31 | }
32 |
33 | public static class Task {
34 | public int p;
35 | public int r;
36 | public int bit;
37 |
38 | public Task(int p, int r, int bit)
39 | {
40 | this.p = p;
41 | this.r = r;
42 | this.bit = bit;
43 | }
44 | }
45 |
46 | public void binaryQuickSortRecursive(int[] array, int p, int r, int bit)
47 | {
48 | if (p < r && bit >= 0)
49 | {
50 | int q = partition(array, p, r, bit);
51 | Delays.sleep(1);
52 | binaryQuickSortRecursive(array, p, q, bit-1);
53 | binaryQuickSortRecursive(array, q+1, r, bit-1);
54 | }
55 | }
56 |
57 | public void binaryQuickSort(int[] array, int p, int r, int bit)
58 | {
59 | Queue tasks = new LinkedList<>();
60 | tasks.add(new Task(p, r, bit));
61 |
62 | while (tasks.isEmpty() == false)
63 | {
64 | Task task = tasks.remove();
65 | if (task.p < task.r && task.bit >= 0)
66 | {
67 | int q = partition(array, task.p, task.r, task.bit);
68 | Delays.sleep(1);
69 | tasks.add(new Task(task.p, q, task.bit-1));
70 | tasks.add(new Task(q+1, task.r, task.bit-1));
71 | }
72 | }
73 | }
74 |
75 | public int partition(int[] array, int p, int r, int bit)
76 | {
77 | int i = p - 1;
78 | int j = r + 1;
79 |
80 | while (true) {
81 | // Left is not set
82 | i++;
83 | while(i < r && !Reads.getBit(array[i], bit)) {
84 | i++;
85 | Highlights.markArray(1, i);
86 | Delays.sleep(0.45);
87 | }
88 | // Right is set
89 | j--;
90 | while(j > p && Reads.getBit(array[j], bit)) {
91 | j--;
92 | Highlights.markArray(2, j);
93 | Delays.sleep(0.45);
94 | }
95 | // If i is less than j, we swap, otherwise we are done
96 | if (i < j)
97 | Writes.swap(array, i, j, 1, true, false);
98 | else
99 | return j;
100 | }
101 | }
102 | }
--------------------------------------------------------------------------------
/src/templates/BogoSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | /*
9 | *
10 | MIT License
11 |
12 | Copyright (c) 2019 w0rthy
13 |
14 | Permission is hereby granted, free of charge, to any person obtaining a copy
15 | of this software and associated documentation files (the "Software"), to deal
16 | in the Software without restriction, including without limitation the rights
17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 | copies of the Software, and to permit persons to whom the Software is
19 | furnished to do so, subject to the following conditions:
20 |
21 | The above copyright notice and this permission notice shall be included in all
22 | copies or substantial portions of the Software.
23 |
24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 | SOFTWARE.
31 | *
32 | */
33 |
34 | public abstract class BogoSorting extends Sort {
35 | protected BogoSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
36 | super(delayOps, markOps, readOps, writeOps);
37 | }
38 |
39 | private static int randomPosition(int length, int offset) {
40 | return (int) (Math.random() * (length - offset));
41 | }
42 |
43 | protected void bogoSwap(int[] array, int length, int offset){
44 | for(int i = offset; i < length; i++) {
45 | Writes.swap(array, i, BogoSorting.randomPosition(length, i) + i, 0, true, false);
46 | }
47 | }
48 |
49 | protected boolean bogoIsSorted(int[] array, int length){
50 | for(int i = 0; i < length - 1; i++) {
51 | if(Reads.compare(array[i], array[i + 1]) == 1) {
52 | Highlights.markArray(1, i);
53 | return false;
54 | }
55 | }
56 | Highlights.clearMark(1);
57 | return true;
58 | }
59 |
60 | protected boolean isMinSorted(int[] array, int length, int offset) {
61 | Highlights.clearAllMarks();
62 |
63 | //Highlights.markArray(2, offset);
64 | //Highlights.markArray(3, length);
65 |
66 | for(int i = offset + 1; i < length; i++) {
67 | Highlights.markArray(1, i);
68 | Delays.sleep(0.075);
69 |
70 | if(Reads.compare(array[offset], array[i]) == 1) {
71 | return false;
72 | }
73 | }
74 | return true;
75 | }
76 |
77 | protected boolean isMaxSorted(int[] array, int minIterator, int maxIterator) {
78 | Highlights.clearAllMarks();
79 |
80 | //Highlights.markArray(2, minIterator);
81 | //Highlights.markArray(3, maxIterator);
82 |
83 | for(int i = maxIterator; i >= minIterator; i--) {
84 | Highlights.markArray(1, i);
85 | Delays.sleep(0.075);
86 |
87 | if(Reads.compare(array[maxIterator], array[i]) == -1) {
88 | return false;
89 | }
90 | }
91 | return true;
92 | }
93 | }
--------------------------------------------------------------------------------
/src/templates/CircleSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | /*
9 | *
10 | Copyright (c) rosettacode.org.
11 | Permission is granted to copy, distribute and/or modify this document
12 | under the terms of the GNU Free Documentation License, Version 1.2
13 | or any later version published by the Free Software Foundation;
14 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
15 | Texts. A copy of the license is included in the section entitled "GNU
16 | Free Documentation License".
17 | *
18 | */
19 |
20 | public abstract class CircleSorting extends Sort {
21 | protected CircleSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
22 | super(delayOps, markOps, readOps, writeOps);
23 | }
24 |
25 | protected int circleSortRoutine(int[] array, int lo, int hi, int swapCount) {
26 | if (lo == hi)
27 | return swapCount;
28 |
29 | int high = hi;
30 | int low = lo;
31 | int mid = (hi - lo) / 2;
32 |
33 | while (lo < hi) {
34 | if (Reads.compare(array[lo], array[hi]) > 0) {
35 | Writes.swap(array, lo, hi, 1, true, false);
36 | swapCount++;
37 | }
38 | lo++;
39 | hi--;
40 |
41 | Highlights.markArray(1, lo);
42 | Highlights.markArray(2, hi);
43 | Delays.sleep(0.5);
44 | }
45 |
46 | if (lo == hi && Reads.compare(array[lo], array[hi + 1]) > 0) {
47 | Writes.swap(array, lo, hi + 1, 1, true, false);
48 | swapCount++;
49 | }
50 |
51 | swapCount = this.circleSortRoutine(array, low, low + mid, swapCount);
52 | swapCount = this.circleSortRoutine(array, low + mid + 1, high, swapCount);
53 |
54 | return swapCount;
55 | }
56 | }
--------------------------------------------------------------------------------
/src/templates/Frame.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | /*
4 | *
5 | MIT License
6 |
7 | Copyright (c) 2019 w0rthy
8 |
9 | Permission is hereby granted, free of charge, to any person obtaining a copy
10 | of this software and associated documentation files (the "Software"), to deal
11 | in the Software without restriction, including without limitation the rights
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | copies of the Software, and to permit persons to whom the Software is
14 | furnished to do so, subject to the following conditions:
15 |
16 | The above copyright notice and this permission notice shall be included in all
17 | copies or substantial portions of the Software.
18 |
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | SOFTWARE.
26 | *
27 | */
28 |
29 | /**
30 | *
31 | * @author S630690
32 | */
33 | public interface Frame {
34 | abstract void reposition();
35 | abstract boolean isVisible();
36 | abstract void dispose();
37 | }
--------------------------------------------------------------------------------
/src/templates/HeapSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | /*
9 | *
10 | Copyright (c) rosettacode.org.
11 | Permission is granted to copy, distribute and/or modify this document
12 | under the terms of the GNU Free Documentation License, Version 1.2
13 | or any later version published by the Free Software Foundation;
14 | with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
15 | Texts. A copy of the license is included in the section entitled "GNU
16 | Free Documentation License".
17 | *
18 | */
19 |
20 | public abstract class HeapSorting extends Sort {
21 | protected HeapSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
22 | super(delayOps, markOps, readOps, writeOps);
23 | }
24 |
25 | private void siftDown(int[] array, int root, int dist, int start, double sleep, boolean isMax) {
26 | int compareVal = 0;
27 |
28 | if(isMax) compareVal = -1;
29 | else compareVal = 1;
30 |
31 | while (root <= dist / 2) {
32 | int leaf = 2 * root;
33 | if (leaf < dist && Reads.compare(array[start + leaf - 1], array[start + leaf]) == compareVal) {
34 | leaf++;
35 | }
36 | Highlights.markArray(1, start + root - 1);
37 | Highlights.markArray(2, start + leaf - 1);
38 | Delays.sleep(sleep);
39 | if (Reads.compare(array[start + root - 1], array[start + leaf - 1]) == compareVal) {
40 | Writes.swap(array, start + root - 1, start + leaf - 1, 0, true, false);
41 | root = leaf;
42 | }
43 | else break;
44 | }
45 | }
46 |
47 | private void heapify(int[] arr, int low, int high, double sleep, boolean isMax) {
48 | int length = high - low;
49 | for (int i = length / 2; i >= 1; i--) {
50 | siftDown(arr, i, length, low, sleep, isMax);
51 | }
52 | }
53 |
54 | // This version of heap sort works for max and min variants, alongside sorting
55 | // partial ranges of an array.
56 | protected void heapSort(int[] arr, int start, int length, double sleep, boolean isMax) {
57 | heapify(arr, start, length, sleep, isMax);
58 |
59 | for (int i = length - start; i > 1; i--) {
60 | Writes.swap(arr, start, start + i - 1, sleep, true, false);
61 | siftDown(arr, 1, i - 1, start, sleep, isMax);
62 | }
63 |
64 | if(!isMax) {
65 | Writes.reversal(arr, start, start + length - 1, 1, true, false);
66 | }
67 | }
68 | }
--------------------------------------------------------------------------------
/src/templates/InsertionSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | /*
9 | *
10 | MIT License
11 |
12 | Copyright (c) 2019 w0rthy
13 |
14 | Permission is hereby granted, free of charge, to any person obtaining a copy
15 | of this software and associated documentation files (the "Software"), to deal
16 | in the Software without restriction, including without limitation the rights
17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 | copies of the Software, and to permit persons to whom the Software is
19 | furnished to do so, subject to the following conditions:
20 |
21 | The above copyright notice and this permission notice shall be included in all
22 | copies or substantial portions of the Software.
23 |
24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 | SOFTWARE.
31 | *
32 | */
33 |
34 | public abstract class InsertionSorting extends Sort {
35 | protected InsertionSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
36 | super(delayOps, markOps, readOps, writeOps);
37 | }
38 |
39 | protected void insertionSort(int[] array, int start, int end, double sleep, boolean auxwrite) {
40 | int pos;
41 | int current;
42 |
43 | for(int i = start; i < end; i++) {
44 | current = array[i];
45 | pos = i - 1;
46 |
47 | while(pos >= start && Reads.compare(array[pos], current) > 0){
48 | Writes.write(array, pos + 1, array[pos], sleep, true, auxwrite);
49 | pos--;
50 | }
51 | Writes.write(array, pos + 1, current, sleep, true, auxwrite);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/templates/JEnhancedOptionPane.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import java.awt.HeadlessException;
4 |
5 | import javax.swing.JDialog;
6 | import javax.swing.JOptionPane;
7 | import javax.swing.UIManager;
8 |
9 | //Many thanks to Freek de Bruijn on StackOverflow for providing a custom JOptionPane.
10 | //https://stackoverflow.com/questions/14407804/how-to-change-the-default-text-of-buttons-in-joptionpane-showinputdialog?noredirect=1&lq=1
11 | final public class JEnhancedOptionPane extends JOptionPane {
12 | /**
13 | *
14 | */
15 | private static final long serialVersionUID = 1L;
16 |
17 | public static String showInputDialog(final Object message, final Object[] options) throws HeadlessException {
18 | final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE,
19 | OK_CANCEL_OPTION, null,
20 | options, null);
21 | pane.setWantsInput(true);
22 | pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
23 | pane.setMessageType(QUESTION_MESSAGE);
24 | pane.selectInitialValue();
25 | final String title = UIManager.getString("OptionPane.inputDialogTitle", null);
26 | final JDialog dialog = pane.createDialog(null, title);
27 | dialog.setVisible(true);
28 | dialog.dispose();
29 | final Object value = pane.getInputValue();
30 | return (value == UNINITIALIZED_VALUE) ? null : (String) value;
31 | }
32 | }
--------------------------------------------------------------------------------
/src/templates/JErrorPane.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import java.io.PrintWriter;
4 | import java.io.StringWriter;
5 |
6 | import javax.swing.JOptionPane;
7 | import javax.swing.JTextArea;
8 |
9 | final public class JErrorPane extends JOptionPane {
10 | /**
11 | *
12 | */
13 | private static final long serialVersionUID = 1L;
14 |
15 | public volatile static boolean errorMessageActive = false;
16 |
17 | public static void invokeErrorMessage(Exception e) {
18 | errorMessageActive = true;
19 |
20 | StringWriter exceptionString = new StringWriter();
21 | e.printStackTrace(new PrintWriter(exceptionString));
22 | String printException = exceptionString.toString();
23 |
24 | JTextArea error = new JTextArea();
25 | error.setText(printException);
26 | error.setCaretPosition(0);
27 | error.setEditable(false);
28 |
29 | JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
30 | errorMessageActive = false;
31 | }
32 | }
--------------------------------------------------------------------------------
/src/templates/MultipleSortThread.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import main.ArrayManager;
4 | import main.ArrayVisualizer;
5 | import utils.Delays;
6 | import utils.Highlights;
7 | import utils.Reads;
8 | import utils.Sounds;
9 | import utils.Timer;
10 | import utils.Writes;
11 |
12 | public abstract class MultipleSortThread {
13 | protected ArrayManager ArrayManager;
14 | protected ArrayVisualizer ArrayVisualizer;
15 | protected Delays Delays;
16 | protected Highlights Highlights;
17 | protected Reads Reads;
18 | protected Writes Writes;
19 | protected Sounds Sounds;
20 | protected Timer Timer;
21 |
22 | protected volatile int sortCount;
23 | protected volatile int sortNumber;
24 |
25 | protected volatile int categoryCount;
26 |
27 | public MultipleSortThread(ArrayVisualizer ArrayVisualizer) {
28 | this.ArrayVisualizer = ArrayVisualizer;
29 | this.ArrayManager = ArrayVisualizer.getArrayManager();
30 | this.Delays = ArrayVisualizer.getDelays();
31 | this.Highlights = ArrayVisualizer.getHighlights();
32 | this.Reads = ArrayVisualizer.getReads();
33 | this.Writes = ArrayVisualizer.getWrites();
34 | this.Sounds = ArrayVisualizer.getSounds();
35 | this.Timer = ArrayVisualizer.getTimer();
36 | }
37 |
38 | protected synchronized void runIndividualSort(Sort sort, int bucketCount, int[] array, int length, double speed) throws InterruptedException {
39 | Delays.setSleepRatio(2.5);
40 |
41 | if(length != ArrayVisualizer.getCurrentLength()) {
42 | ArrayVisualizer.setCurrentLength(length);
43 | }
44 |
45 | ArrayManager.refreshArray(array, ArrayVisualizer.getCurrentLength(), this.ArrayVisualizer);
46 |
47 | ArrayVisualizer.setHeading(sort.getRunAllID() + " (Sort " + this.sortNumber + " of " + this.sortCount + ")");
48 | Delays.setSleepRatio(speed);
49 |
50 | Timer.enableRealTimer();
51 |
52 | sort.runSort(array, ArrayVisualizer.getCurrentLength(), bucketCount);
53 |
54 | ArrayVisualizer.endSort();
55 | Thread.sleep(1000);
56 |
57 | this.sortNumber++;
58 | }
59 |
60 | protected abstract void executeSortList(int[] array) throws Exception;
61 | protected abstract void runThread(int[] array, int current, int total, boolean runAllActive) throws Exception;
62 |
63 | public synchronized void reportCategorySorts(int[] array) throws Exception {
64 | this.runThread(array, 0, 0, false);
65 | }
66 |
67 | public synchronized void reportAllSorts(int[] array, int current, int total) throws Exception {
68 | this.runThread(array, current, total, true);
69 | }
70 |
71 | public int getSortCount() {
72 | return this.sortCount;
73 | }
74 |
75 | public int getCategoryCount() {
76 | return this.categoryCount;
77 | }
78 | }
--------------------------------------------------------------------------------
/src/templates/ShatterSorting.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import java.util.ArrayList;
4 |
5 | import utils.Delays;
6 | import utils.Highlights;
7 | import utils.Reads;
8 | import utils.Writes;
9 |
10 | /*
11 | *
12 | MIT License
13 |
14 | Copyright (c) 2019 w0rthy
15 |
16 | Permission is hereby granted, free of charge, to any person obtaining a copy
17 | of this software and associated documentation files (the "Software"), to deal
18 | in the Software without restriction, including without limitation the rights
19 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 | copies of the Software, and to permit persons to whom the Software is
21 | furnished to do so, subject to the following conditions:
22 |
23 | The above copyright notice and this permission notice shall be included in all
24 | copies or substantial portions of the Software.
25 |
26 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 | SOFTWARE.
33 | *
34 | */
35 |
36 | public abstract class ShatterSorting extends Sort {
37 | protected ShatterSorting(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
38 | super(delayOps, markOps, readOps, writeOps);
39 | }
40 |
41 | private void shatterPartition(int[] array, int length, int num) {
42 | int shatters = (int) Math.ceil(length / (double) num);
43 |
44 | @SuppressWarnings("unchecked")
45 | ArrayList[] registers = new ArrayList[shatters];
46 |
47 | for(int i = 0; i < shatters; i++) {
48 | registers[i] = new ArrayList<>();
49 | }
50 |
51 | for(int i = 0; i < length; i++){
52 | registers[array[i] / num].add(array[i]);
53 | Highlights.markArray(1, i);
54 |
55 | Writes.mockWrite(length, array[i] / num, array[i], 0.5);
56 | }
57 |
58 | Writes.transcribe(array, registers, 0, true, false);
59 | }
60 |
61 | protected void shatterSort(int[] array, int length, int num) {
62 | int shatters = (int) Math.ceil(length / (double) num);
63 |
64 | shatterPartition(array, length, num);
65 |
66 | int[] tmp = new int[num];
67 | for(int i = 0; i < shatters; i++) {
68 | for(int j = 0; j < num; j++) {
69 | if(i * num + j >= length)
70 | Writes.write(tmp, j, -1, 0.5, false, true);
71 | else
72 | Writes.write(tmp, j, array[i * num + j], 0.5, false, true);
73 |
74 | Highlights.markArray(2, i * num + j);
75 | }
76 |
77 | Highlights.clearMark(2);
78 |
79 | for(int j = 0; j < tmp.length; j++) {
80 | int tmpj = tmp[j];
81 |
82 | if(i * num + (tmpj % num) >= length || tmpj == -1) {
83 | break;
84 | }
85 |
86 | Writes.write(array, i * num + (tmpj % num), tmpj, 1, false, false);
87 | Highlights.markArray(1, i * num + (tmpj % num));
88 | }
89 |
90 | Highlights.clearMark(1);
91 | }
92 | }
93 |
94 | protected void simpleShatterSort(int[] array, int length, int num, int rate) {
95 | for(int i = num; i > 1; i = i / rate) {
96 | shatterPartition(array, length, i);
97 | }
98 | shatterPartition(array, length, 1);
99 | }
100 | }
--------------------------------------------------------------------------------
/src/templates/Sort.java:
--------------------------------------------------------------------------------
1 | package templates;
2 |
3 | import utils.Delays;
4 | import utils.Highlights;
5 | import utils.Reads;
6 | import utils.Writes;
7 |
8 | public abstract class Sort {
9 | private String sortPromptID;
10 | private String runAllID;
11 | private String reportSortID;
12 |
13 | private String category;
14 |
15 | private boolean comparisonBased;
16 | private boolean bucketSort;
17 | private boolean radixSort;
18 | private boolean unreasonablySlow;
19 | private boolean bogoSort;
20 | private int unreasonableLimit;
21 |
22 | protected Delays Delays;
23 | protected Highlights Highlights;
24 | protected Reads Reads;
25 | protected Writes Writes;
26 |
27 | protected Sort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
28 | this.setSortPromptID("");
29 | this.setRunAllID("");
30 | this.setReportSortID("");
31 | this.setCategory("");
32 | this.isComparisonBased(true);
33 | this.isBucketSort(false);
34 | this.isRadixSort(false); //Used to slightly change base dialogue and to verify that a radix sort is also labeled a bucket sort.
35 | this.isUnreasonablySlow(false); //This boolean is true when a sort is incredibly slow even when the user clicks the "Skip Sort" button.
36 | this.setUnreasonableLimit(0); //The length of the array such that the user will be warned if this sort is picked and it is marked as unreasonably slow.
37 | this.isBogoSort(false); //Used to slightly change warning dialogue and to verify that a Bogo-based sort is marked as unreasonably slow.
38 |
39 | this.Delays = delayOps;
40 | this.Highlights = markOps;
41 | this.Reads = readOps;
42 | this.Writes = writeOps;
43 | }
44 |
45 | public String getSortPromptID() {
46 | return this.sortPromptID;
47 | }
48 | public String getRunAllID() {
49 | return this.runAllID;
50 | }
51 | public String getReportSortID() {
52 | return this.reportSortID;
53 | }
54 | public String getCategory() {
55 | return this.category;
56 | }
57 | public boolean comparisonBased() {
58 | return this.comparisonBased;
59 | }
60 | public boolean usesBuckets() {
61 | return this.bucketSort;
62 | }
63 | public boolean radixSort() {
64 | return this.radixSort;
65 | }
66 | public boolean getUnreasonablySlow() {
67 | return this.unreasonablySlow;
68 | }
69 | public int getUnreasonableLimit() {
70 | return this.unreasonableLimit;
71 | }
72 | public boolean bogoSort() {
73 | return this.bogoSort;
74 | }
75 |
76 | protected void setSortPromptID(String ID) {
77 | this.sortPromptID = ID;
78 | }
79 | protected void setRunAllID(String ID) {
80 | this.runAllID = ID;
81 | }
82 | protected void setReportSortID(String ID) {
83 | this.reportSortID = ID;
84 | }
85 | protected void setCategory(String ID) {
86 | this.category = ID;
87 | }
88 | protected void isComparisonBased(boolean Bool) {
89 | this.comparisonBased = Bool;
90 | }
91 | public void isBucketSort(boolean Bool) {
92 | this.bucketSort = Bool;
93 | }
94 | protected void isRadixSort(boolean Bool) {
95 | this.radixSort = Bool;
96 | }
97 | protected void isUnreasonablySlow(boolean Bool) {
98 | this.unreasonablySlow = Bool;
99 | }
100 | public void setUnreasonableLimit(int number) {
101 | this.unreasonableLimit = number;
102 | }
103 | protected void isBogoSort(boolean Bool) {
104 | this.bogoSort = Bool;
105 | }
106 |
107 | public abstract void runSort(int[] array, int currentLength, int bucketCount); //bucketCount will be zero for comparison-based sorts
108 | }
--------------------------------------------------------------------------------
/src/test/Tester.java:
--------------------------------------------------------------------------------
1 | package test;
2 |
3 | import java.util.Arrays;
4 |
5 | import main.ArrayVisualizer;
6 | import utils.Delays;
7 | import utils.Highlights;
8 | import utils.Reads;
9 | import utils.Timer;
10 | import utils.Writes;
11 |
12 | public class Tester {
13 | private static Delays Delays;
14 | private static Highlights Highlights;
15 | private static Reads Reads;
16 | private static Timer RealTimer;
17 | private static Writes Writes;
18 |
19 | // Attempts to use insertion sort on [begin, end). Will return false if more than
20 | // partial_insertion_sort_limit elements were moved, and abort sorting. Otherwise it will
21 | // successfully sort and return true.
22 | private static boolean pdqPartialInsertSort(int[] array, int begin, int end) {
23 | if (begin == end) return true;
24 |
25 | int limit = 0;
26 | for (int cur = begin + 1; cur != end; ++cur) {
27 | if (limit > 8) return false;
28 |
29 | int sift = cur;
30 | int siftMinusOne = cur - 1;
31 |
32 | // Compare first so we can avoid 2 moves for an element already positioned correctly.
33 | if (Reads.compare(array[sift], array[siftMinusOne]) < 0) {
34 | int tmp = array[sift];
35 |
36 | do {
37 | Writes.write(array, sift--, array[siftMinusOne], 0, false, false);
38 | } while (sift != begin && Reads.compare(tmp, array[--siftMinusOne]) < 0);
39 |
40 | Writes.write(array, sift, tmp, 0, false, false);
41 | limit += cur - sift;
42 | }
43 | }
44 | return true;
45 | }
46 |
47 | public static void main(String[] args) throws Exception {
48 | int[] testArr = new int[32];
49 | for(int i = 0; i < testArr.length; i++) {
50 | testArr[i] = i;
51 | }
52 |
53 | ArrayVisualizer av = new ArrayVisualizer();
54 |
55 | Delays = new Delays(av);
56 | Delays.setSleepRatio(Double.MAX_VALUE);
57 |
58 | Highlights = new Highlights(av, testArr.length);
59 | Highlights.toggleFancyFinishes(false);
60 |
61 | RealTimer = new Timer();
62 | RealTimer.toggleRealTimer(false);
63 |
64 | Reads = new Reads(av);
65 | Writes = new Writes(av);
66 |
67 | for(int i = 0; i < testArr.length; i++){
68 | Writes.swap(testArr, i, (int)(Math.random()*testArr.length), 0, false, false);
69 | }
70 |
71 | System.out.println(Arrays.toString(testArr));
72 |
73 | RealTimer.startLap();
74 |
75 | pdqPartialInsertSort(testArr, 0, testArr.length);
76 |
77 | RealTimer.stopLap();
78 |
79 | RealTimer.toggleRealTimer(true);
80 |
81 | System.out.println(Arrays.toString(testArr));
82 | System.out.println(RealTimer.getRealTime());
83 | }
84 | }
--------------------------------------------------------------------------------
/src/threads/RunAllSorts.java:
--------------------------------------------------------------------------------
1 | package threads;
2 |
3 | import java.util.ArrayList;
4 |
5 | import main.ArrayVisualizer;
6 | import templates.JErrorPane;
7 | import templates.MultipleSortThread;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class RunAllSorts {
36 | private ArrayVisualizer ArrayVisualizer;
37 | private ArrayList allSortThreads;
38 |
39 | public RunAllSorts(ArrayVisualizer ArrayVisualizer) {
40 | this.ArrayVisualizer = ArrayVisualizer;
41 | this.allSortThreads = new ArrayList<>();
42 | this.allSortThreads.add(new RunExchangeSorts(ArrayVisualizer));
43 | this.allSortThreads.add(new RunSelectionSorts(ArrayVisualizer));
44 | this.allSortThreads.add(new RunInsertionSorts(ArrayVisualizer));
45 | this.allSortThreads.add(new RunMergeSorts(ArrayVisualizer));
46 | this.allSortThreads.add(new RunDistributionSorts(ArrayVisualizer));
47 | this.allSortThreads.add(new RunConcurrentSorts(ArrayVisualizer));
48 | this.allSortThreads.add(new RunHybridSorts(ArrayVisualizer));
49 | this.allSortThreads.add(new RunMiscellaneousSorts(ArrayVisualizer));
50 | this.allSortThreads.add(new RunImpracticalSorts(ArrayVisualizer));
51 | }
52 |
53 | public void reportAllSorts(int[] array) throws Exception {
54 | int totalSortCount = 0;
55 | for(MultipleSortThread category : this.allSortThreads) {
56 | totalSortCount += category.getSortCount();
57 | }
58 |
59 | try {
60 | int currentSort = 1;
61 | for(MultipleSortThread thread : this.allSortThreads) {
62 | thread.reportAllSorts(array, currentSort, totalSortCount);
63 | this.ArrayVisualizer.getSortingThread().join();
64 | currentSort += thread.getCategoryCount();
65 | }
66 | } catch (Exception e) {
67 | JErrorPane.invokeErrorMessage(e);
68 | }
69 |
70 | this.ArrayVisualizer.setCategory("Run All Sorts");
71 | this.ArrayVisualizer.setHeading("Finished!!");
72 | }
73 | }
--------------------------------------------------------------------------------
/src/threads/RunMiscellaneousSorts.java:
--------------------------------------------------------------------------------
1 | package threads;
2 |
3 | import main.ArrayVisualizer;
4 | import sorts.PancakeSort;
5 | import templates.JErrorPane;
6 | import templates.MultipleSortThread;
7 | import templates.Sort;
8 |
9 | /*
10 | *
11 | MIT License
12 |
13 | Copyright (c) 2019 w0rthy
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining a copy
16 | of this software and associated documentation files (the "Software"), to deal
17 | in the Software without restriction, including without limitation the rights
18 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 | copies of the Software, and to permit persons to whom the Software is
20 | furnished to do so, subject to the following conditions:
21 |
22 | The above copyright notice and this permission notice shall be included in all
23 | copies or substantial portions of the Software.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | *
33 | */
34 |
35 | final public class RunMiscellaneousSorts extends MultipleSortThread {
36 | private Sort PancakeSort;
37 |
38 | public RunMiscellaneousSorts(ArrayVisualizer ArrayVisualizer) {
39 | super(ArrayVisualizer);
40 | this.sortCount = 1;
41 | this.categoryCount = this.sortCount;
42 |
43 | PancakeSort = new PancakeSort(Delays, Highlights, Reads, Writes);
44 | }
45 |
46 | @Override
47 | protected synchronized void executeSortList(int[] array) throws Exception {
48 | RunMiscellaneousSorts.this.runIndividualSort(PancakeSort, 0, array, 128, 0.015);
49 | }
50 |
51 | @Override
52 | protected synchronized void runThread(int[] array, int current, int total, boolean runAllActive) throws Exception {
53 | if(ArrayVisualizer.getSortingThread() != null && ArrayVisualizer.getSortingThread().isAlive())
54 | return;
55 |
56 | Sounds.toggleSound(true);
57 | ArrayVisualizer.setSortingThread(new Thread() {
58 | @Override
59 | public void run() {
60 | try{
61 | if(runAllActive) {
62 | RunMiscellaneousSorts.this.sortNumber = current;
63 | RunMiscellaneousSorts.this.sortCount = total;
64 | }
65 | else {
66 | RunMiscellaneousSorts.this.sortNumber = 1;
67 | }
68 |
69 | ArrayManager.toggleMutableLength(false);
70 |
71 | ArrayVisualizer.setCategory("Miscellaneous Sorts");
72 |
73 | RunMiscellaneousSorts.this.executeSortList(array);
74 |
75 | if(!runAllActive) {
76 | ArrayVisualizer.setCategory("Run Miscellaneous Sorts");
77 | ArrayVisualizer.setHeading("Done");
78 | }
79 |
80 | ArrayManager.toggleMutableLength(true);
81 | }
82 | catch (Exception e) {
83 | JErrorPane.invokeErrorMessage(e);
84 | }
85 | Sounds.toggleSound(false);
86 | ArrayVisualizer.setSortingThread(null);
87 | }
88 | });
89 | ArrayVisualizer.runSortingThread();
90 | }
91 | }
--------------------------------------------------------------------------------
/src/visuals/VisualStyles.java:
--------------------------------------------------------------------------------
1 | package visuals;
2 |
3 | import main.ArrayVisualizer;
4 | import utils.Highlights;
5 | import utils.Renderer;
6 |
7 | /*
8 | *
9 | MIT License
10 |
11 | Copyright (c) 2019 w0rthy
12 |
13 | Permission is hereby granted, free of charge, to any person obtaining a copy
14 | of this software and associated documentation files (the "Software"), to deal
15 | in the Software without restriction, including without limitation the rights
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 | copies of the Software, and to permit persons to whom the Software is
18 | furnished to do so, subject to the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be included in all
21 | copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 | SOFTWARE.
30 | *
31 | */
32 |
33 | public enum VisualStyles {
34 | BARS {
35 | @Override
36 | public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) {
37 | ArrayVisualizer.getVisuals()[0].drawVisual(array, ArrayVisualizer, Renderer, Highlights);
38 | }
39 | },
40 | CIRCULAR {
41 | @Override
42 | public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) {
43 | ArrayVisualizer.getVisuals()[1].drawVisual(array, ArrayVisualizer, Renderer, Highlights);
44 | }
45 | },
46 | HOOPS {
47 | @Override
48 | public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) {
49 | ArrayVisualizer.getVisuals()[2].drawVisual(array, ArrayVisualizer, Renderer, Highlights);
50 | }
51 | },
52 | MESH {
53 | @Override
54 | public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) {
55 | ArrayVisualizer.getVisuals()[3].drawVisual(array, ArrayVisualizer, Renderer, Highlights);
56 | }
57 | },
58 | PIXELS {
59 | @Override
60 | public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) {
61 | ArrayVisualizer.getVisuals()[4].drawVisual(array, ArrayVisualizer, Renderer, Highlights);
62 | }
63 | };
64 |
65 | public VisualStyles getCurrentVisual() {
66 | return this;
67 | }
68 |
69 | public abstract void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights);
70 | }
--------------------------------------------------------------------------------