diff --git a/add_trailing_newlines.py b/add_trailing_newlines.py new file mode 100644 index 00000000..23f78b1e --- /dev/null +++ b/add_trailing_newlines.py @@ -0,0 +1,53 @@ +import os + +# linesep = os.linesep.encode('ascii') +linesep = b'\n' + + +def dir_parts(dir: str) -> tuple[str]: + return tuple(os.path.normpath(dir).split(os.sep)) + + +def is_in_dir(dir: str, in_: tuple[str]) -> bool: + return tuple(dir.split(os.sep, len(in_)))[:len(in_)] == in_ + + +def is_in_dirs(dir: str, in_: tuple[tuple[str]]) -> bool: + return any(is_in_dir(dir, i) for i in in_) + + +def add_trailing_newlines(path: str) -> bool: + with open(path, 'r+b') as fp: + fp.seek(-len(linesep), 2) + if fp.read(len(linesep)) != linesep: + fp.write(linesep) + return True + return False + + +skip_parts = ( + '.git', + 'bin', + 'dist', + 'cache', + '__pycache__', + 'lib', + 'sorting_networks', + 'src/main/resources', + 'wrapper' +) +skip_parts = tuple(dir_parts(d) for d in skip_parts) + +for (dir, dirs, files) in os.walk('.'): + if dir == '.': + dir = '' + else: + dir = os.path.normpath(dir) + if is_in_dirs(dir, skip_parts): + continue + for file in files: + file = os.path.join(dir, file) + if add_trailing_newlines(file): + print('Wrote ', file) + else: + print('Skipped', file) diff --git a/src/main/java/io/github/arrayv/dialogs/ImportShuffleDialog.java b/src/main/java/io/github/arrayv/dialogs/ImportShuffleDialog.java index 6ebe9f7f..656e3762 100644 --- a/src/main/java/io/github/arrayv/dialogs/ImportShuffleDialog.java +++ b/src/main/java/io/github/arrayv/dialogs/ImportShuffleDialog.java @@ -1,6 +1,6 @@ package io.github.arrayv.dialogs; -final public class ImportShuffleDialog extends FileDialog { +public final class ImportShuffleDialog extends FileDialog { public ImportShuffleDialog() { super(); @@ -9,4 +9,4 @@ public ImportShuffleDialog() { fileDialog.showOpenDialog(null); this.file = fileDialog.getSelectedFile(); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/frames/ArrayFrame.java b/src/main/java/io/github/arrayv/frames/ArrayFrame.java index 4609be46..b7f5df42 100644 --- a/src/main/java/io/github/arrayv/frames/ArrayFrame.java +++ b/src/main/java/io/github/arrayv/frames/ArrayFrame.java @@ -320,4 +320,4 @@ public void mouseExited(MouseEvent e) { private javax.swing.JSlider jSlider1; private javax.swing.JSlider jSlider2; // End of variables declaration//GEN-END:variables -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/frames/ImageFrame.java b/src/main/java/io/github/arrayv/frames/ImageFrame.java index 744bb01f..63aa6099 100644 --- a/src/main/java/io/github/arrayv/frames/ImageFrame.java +++ b/src/main/java/io/github/arrayv/frames/ImageFrame.java @@ -138,4 +138,4 @@ public void updatePreview(CustomImage visual) { this.lblNewLabel_2.setIcon(imageIcon); this.lblNewLabel_2.setText(""); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/frames/SoundFrame.java b/src/main/java/io/github/arrayv/frames/SoundFrame.java index ea8e6d6d..f345aac0 100644 --- a/src/main/java/io/github/arrayv/frames/SoundFrame.java +++ b/src/main/java/io/github/arrayv/frames/SoundFrame.java @@ -164,4 +164,4 @@ public void actionPerformed(ActionEvent e) { gbc_btnNewButton_2.gridy = 6; contentPane.add(btnNewButton_2, gbc_btnNewButton_2); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/frames/UtilFrame.java b/src/main/java/io/github/arrayv/frames/UtilFrame.java index 39cb3316..1ee20f90 100644 --- a/src/main/java/io/github/arrayv/frames/UtilFrame.java +++ b/src/main/java/io/github/arrayv/frames/UtilFrame.java @@ -586,4 +586,4 @@ private void jComboBox1ActionPerformed() {//GEN-FIRST:event_jButton4ActionPerfor private javax.swing.JCheckBox jCheckBox9; @SuppressWarnings("rawtypes") private javax.swing.JComboBox jComboBox1; -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/prompts/SortPrompt.java b/src/main/java/io/github/arrayv/prompts/SortPrompt.java index 77173e6c..5a01624b 100644 --- a/src/main/java/io/github/arrayv/prompts/SortPrompt.java +++ b/src/main/java/io/github/arrayv/prompts/SortPrompt.java @@ -69,7 +69,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SortPrompt extends javax.swing.JFrame implements AppFrame { +public final class SortPrompt extends javax.swing.JFrame implements AppFrame { public class PlaceholderTextField extends JTextField { /** * Shamelessly copied from https://stackoverflow.com/a/16229082/8840278 diff --git a/src/main/java/io/github/arrayv/prompts/ViewPrompt.java b/src/main/java/io/github/arrayv/prompts/ViewPrompt.java index 066335d5..4feed941 100644 --- a/src/main/java/io/github/arrayv/prompts/ViewPrompt.java +++ b/src/main/java/io/github/arrayv/prompts/ViewPrompt.java @@ -394,4 +394,4 @@ private void disparityChordsActionPerformed(java.awt.event.ActionEvent evt) { private javax.swing.JButton disparityBarGraph; private javax.swing.JButton disparityChords; private javax.swing.JLabel jLabel1; -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/sortdata/SortComparator.java b/src/main/java/io/github/arrayv/sortdata/SortComparator.java index 8d8bf218..4927a857 100644 --- a/src/main/java/io/github/arrayv/sortdata/SortComparator.java +++ b/src/main/java/io/github/arrayv/sortdata/SortComparator.java @@ -2,7 +2,7 @@ import java.util.Comparator; -final public class SortComparator implements Comparator { +public final class SortComparator implements Comparator { public SortComparator() {} @Override diff --git a/src/main/java/io/github/arrayv/visuals/Visual.java b/src/main/java/io/github/arrayv/visuals/Visual.java index 17f7a936..5d79afdc 100644 --- a/src/main/java/io/github/arrayv/visuals/Visual.java +++ b/src/main/java/io/github/arrayv/visuals/Visual.java @@ -125,4 +125,4 @@ public static void drawFancyFinishLine(int logOfLen, int index, int position, Gr } public abstract void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights); -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/VisualStyles.java b/src/main/java/io/github/arrayv/visuals/VisualStyles.java index 8d69a1fe..273a01de 100644 --- a/src/main/java/io/github/arrayv/visuals/VisualStyles.java +++ b/src/main/java/io/github/arrayv/visuals/VisualStyles.java @@ -127,4 +127,4 @@ public VisualStyles getCurrentVisual() { } public abstract void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights); -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/bars/BarGraph.java b/src/main/java/io/github/arrayv/visuals/bars/BarGraph.java index 251a4da4..f1e76e5e 100644 --- a/src/main/java/io/github/arrayv/visuals/bars/BarGraph.java +++ b/src/main/java/io/github/arrayv/visuals/bars/BarGraph.java @@ -7,7 +7,7 @@ import utils.Highlights; import utils.Renderer; -final public class BarGraph extends Visual { +public final class BarGraph extends Visual { public BarGraph(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -57,4 +57,4 @@ else if (ArrayVisualizer.colorEnabled()) { this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/bars/DisparityBarGraph.java b/src/main/java/io/github/arrayv/visuals/bars/DisparityBarGraph.java index 36f5cfa1..70e8df11 100644 --- a/src/main/java/io/github/arrayv/visuals/bars/DisparityBarGraph.java +++ b/src/main/java/io/github/arrayv/visuals/bars/DisparityBarGraph.java @@ -7,7 +7,7 @@ import utils.Highlights; import utils.Renderer; -final public class DisparityBarGraph extends Visual { +public final class DisparityBarGraph extends Visual { public DisparityBarGraph(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -51,4 +51,4 @@ else if (ArrayVisualizer.colorEnabled()) this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/bars/Rainbow.java b/src/main/java/io/github/arrayv/visuals/bars/Rainbow.java index 185ffa6b..d8d28248 100644 --- a/src/main/java/io/github/arrayv/visuals/bars/Rainbow.java +++ b/src/main/java/io/github/arrayv/visuals/bars/Rainbow.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Rainbow extends Visual { +public final class Rainbow extends Visual { public Rainbow(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); } @@ -69,4 +69,4 @@ public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Re this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/bars/SineWave.java b/src/main/java/io/github/arrayv/visuals/bars/SineWave.java index d728d92a..69cf9f9a 100644 --- a/src/main/java/io/github/arrayv/visuals/bars/SineWave.java +++ b/src/main/java/io/github/arrayv/visuals/bars/SineWave.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SineWave extends Visual { +public final class SineWave extends Visual { public SineWave(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -74,4 +74,4 @@ else if (ArrayVisualizer.colorEnabled()) this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/circles/ColorCircle.java b/src/main/java/io/github/arrayv/visuals/circles/ColorCircle.java index ed327d44..b487b53b 100644 --- a/src/main/java/io/github/arrayv/visuals/circles/ColorCircle.java +++ b/src/main/java/io/github/arrayv/visuals/circles/ColorCircle.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ColorCircle extends Visual { +public final class ColorCircle extends Visual { public ColorCircle(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -99,4 +99,4 @@ public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Re if (x[1] != x[2] || y[1] != y[2]) this.mainRender.fillPolygon(x, y, 3); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/circles/DisparityChords.java b/src/main/java/io/github/arrayv/visuals/circles/DisparityChords.java index b27412f9..c0f261e3 100644 --- a/src/main/java/io/github/arrayv/visuals/circles/DisparityChords.java +++ b/src/main/java/io/github/arrayv/visuals/circles/DisparityChords.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DisparityChords extends Visual { +public final class DisparityChords extends Visual { public DisparityChords(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -87,4 +87,4 @@ public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Re } } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/circles/DisparityCircle.java b/src/main/java/io/github/arrayv/visuals/circles/DisparityCircle.java index ba448b6b..07080267 100644 --- a/src/main/java/io/github/arrayv/visuals/circles/DisparityCircle.java +++ b/src/main/java/io/github/arrayv/visuals/circles/DisparityCircle.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DisparityCircle extends Visual { +public final class DisparityCircle extends Visual { public DisparityCircle(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -82,4 +82,4 @@ else if (ArrayVisualizer.colorEnabled()) } this.extraRender.setStroke(ArrayVisualizer.getDefaultStroke()); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/circles/Spiral.java b/src/main/java/io/github/arrayv/visuals/circles/Spiral.java index 101ed04a..7a3d7a7e 100644 --- a/src/main/java/io/github/arrayv/visuals/circles/Spiral.java +++ b/src/main/java/io/github/arrayv/visuals/circles/Spiral.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Spiral extends Visual { +public final class Spiral extends Visual { public Spiral(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -88,4 +88,4 @@ else if (ArrayVisualizer.colorEnabled()) } this.extraRender.setStroke(ArrayVisualizer.getDefaultStroke()); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/dots/DisparityDots.java b/src/main/java/io/github/arrayv/visuals/dots/DisparityDots.java index 72663901..9731974a 100644 --- a/src/main/java/io/github/arrayv/visuals/dots/DisparityDots.java +++ b/src/main/java/io/github/arrayv/visuals/dots/DisparityDots.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DisparityDots extends Visual { +public final class DisparityDots extends Visual { public DisparityDots(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -111,4 +111,4 @@ else if (ArrayVisualizer.colorEnabled()) } } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/dots/ScatterPlot.java b/src/main/java/io/github/arrayv/visuals/dots/ScatterPlot.java index 6d54d0e1..bba8cd75 100644 --- a/src/main/java/io/github/arrayv/visuals/dots/ScatterPlot.java +++ b/src/main/java/io/github/arrayv/visuals/dots/ScatterPlot.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ScatterPlot extends Visual { +public final class ScatterPlot extends Visual { public ScatterPlot(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); } @@ -112,4 +112,4 @@ else if (ArrayVisualizer.colorEnabled()) { this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/dots/SpiralDots.java b/src/main/java/io/github/arrayv/visuals/dots/SpiralDots.java index 1d026a2b..4d4a2842 100644 --- a/src/main/java/io/github/arrayv/visuals/dots/SpiralDots.java +++ b/src/main/java/io/github/arrayv/visuals/dots/SpiralDots.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SpiralDots extends Visual { +public final class SpiralDots extends Visual { public SpiralDots(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); @@ -111,4 +111,4 @@ else if (ArrayVisualizer.colorEnabled()) } } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/dots/WaveDots.java b/src/main/java/io/github/arrayv/visuals/dots/WaveDots.java index 270381d4..2bc4294b 100644 --- a/src/main/java/io/github/arrayv/visuals/dots/WaveDots.java +++ b/src/main/java/io/github/arrayv/visuals/dots/WaveDots.java @@ -35,7 +35,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class WaveDots extends Visual { +public final class WaveDots extends Visual { public WaveDots(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); } @@ -109,4 +109,4 @@ else if (ArrayVisualizer.colorEnabled()) this.mainRender.fillRect(0, Renderer.getYOffset() + Renderer.getViewSize() - 20, ArrayVisualizer.currentWidth(), 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/misc/HoopStack.java b/src/main/java/io/github/arrayv/visuals/misc/HoopStack.java index 2abe369a..38044f9d 100644 --- a/src/main/java/io/github/arrayv/visuals/misc/HoopStack.java +++ b/src/main/java/io/github/arrayv/visuals/misc/HoopStack.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class HoopStack extends Visual { +public final class HoopStack extends Visual { public HoopStack(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); } @@ -76,4 +76,4 @@ else if (Highlights.containsPosition(i)) { } this.mainRender.setStroke(ArrayVisualizer.getDefaultStroke()); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/arrayv/visuals/misc/PixelMesh.java b/src/main/java/io/github/arrayv/visuals/misc/PixelMesh.java index 84a381fa..639d638e 100644 --- a/src/main/java/io/github/arrayv/visuals/misc/PixelMesh.java +++ b/src/main/java/io/github/arrayv/visuals/misc/PixelMesh.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class PixelMesh extends Visual { +public final class PixelMesh extends Visual { public PixelMesh(ArrayVisualizer ArrayVisualizer) { super(ArrayVisualizer); } @@ -76,4 +76,4 @@ else if (Highlights.containsPosition(idx)) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/main/ArrayManager.java b/src/main/java/main/ArrayManager.java index edd78123..157fa745 100644 --- a/src/main/java/main/ArrayManager.java +++ b/src/main/java/main/ArrayManager.java @@ -266,4 +266,4 @@ public void refreshArray(int[] array, int currentLen, ArrayVisualizer ArrayVisua ArrayVisualizer.resetAllStatistics(); } -} \ No newline at end of file +} diff --git a/src/main/java/panes/JEnhancedOptionPane.java b/src/main/java/panes/JEnhancedOptionPane.java index dabf0321..7a1a4f51 100644 --- a/src/main/java/panes/JEnhancedOptionPane.java +++ b/src/main/java/panes/JEnhancedOptionPane.java @@ -7,7 +7,7 @@ // Many thanks to Freek de Bruijn on StackOverflow for providing a custom JOptionPane. // https://stackoverflow.com/questions/14407804/how-to-change-the-default-text-of-buttons-in-joptionpane-showinputdialog?noredirect=1&lq=1 -final public class JEnhancedOptionPane extends JOptionPane { +public final class JEnhancedOptionPane extends JOptionPane { /** * */ @@ -27,4 +27,4 @@ public static String showInputDialog(final String title, final Object message, f final Object value = pane.getInputValue(); return (value == UNINITIALIZED_VALUE) ? null : (String) value; } -} \ No newline at end of file +} diff --git a/src/main/java/panes/JErrorPane.java b/src/main/java/panes/JErrorPane.java index 564c3d5d..a9826249 100644 --- a/src/main/java/panes/JErrorPane.java +++ b/src/main/java/panes/JErrorPane.java @@ -6,7 +6,7 @@ import javax.swing.JOptionPane; import javax.swing.JTextArea; -final public class JErrorPane extends JOptionPane { +public final class JErrorPane extends JOptionPane { /** * */ @@ -45,4 +45,4 @@ public static void invokeCustomErrorMessage(String errorMsg) { JOptionPane.showMessageDialog(null, errorMsg, "Error", JOptionPane.ERROR_MESSAGE); errorMessageActive = false; } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BitonicSortIterative.java b/src/main/java/sorts/concurrent/BitonicSortIterative.java index 10389101..3020b34c 100644 --- a/src/main/java/sorts/concurrent/BitonicSortIterative.java +++ b/src/main/java/sorts/concurrent/BitonicSortIterative.java @@ -6,15 +6,15 @@ /* * This version of Bitonic Sort was taken from here, written by Nikos Pitsianis: * https://www2.cs.duke.edu/courses/fall08/cps196.1/Pthreads/bitonic.c - * + * * Thanks to Piotr Grochowski for rewriting code to allow this implementation to * work for array lengths other than powers of two! */ -final public class BitonicSortIterative extends Sort { +public final class BitonicSortIterative extends Sort { public BitonicSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bitonic (Iterative)"); this.setRunAllSortsName("Iterative Bitonic Sort"); this.setRunSortName("Iterative Bitonic Sort"); @@ -26,14 +26,14 @@ public BitonicSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int i, j, k; for(k = 2; k < sortLength*2; k = 2 * k) { boolean m = (((sortLength + (k - 1)) / k) % 2) != 0; - + for(j = k >> 1; j > 0; j = j >> 1) { for(i = 0; i < sortLength; i++) { int ij = i ^ j; @@ -48,4 +48,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BitonicSortParallel.java b/src/main/java/sorts/concurrent/BitonicSortParallel.java index 9a549d47..f6a7ccf9 100644 --- a/src/main/java/sorts/concurrent/BitonicSortParallel.java +++ b/src/main/java/sorts/concurrent/BitonicSortParallel.java @@ -90,4 +90,4 @@ public void runSort(int[] arr, int length, int buckets) { this.arr = arr; this.bitonicSort(0, length, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BitonicSortRecursive.java b/src/main/java/sorts/concurrent/BitonicSortRecursive.java index 2b287753..efc7231a 100644 --- a/src/main/java/sorts/concurrent/BitonicSortRecursive.java +++ b/src/main/java/sorts/concurrent/BitonicSortRecursive.java @@ -8,12 +8,12 @@ * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm */ -final public class BitonicSortRecursive extends Sort { +public final class BitonicSortRecursive extends Sort { private boolean direction = true; - + public BitonicSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bitonic (Recursive)"); this.setRunAllSortsName("Batcher's Bitonic Sort"); this.setRunSortName("Recursive Bitonic Sort"); @@ -25,19 +25,19 @@ public BitonicSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private static int greatestPowerOfTwoLessThan(int n){ int k = 1; while (k < n) { k = k << 1; } - return k >> 1; + return k >> 1; } - + private void compare(int[] A, int i, int j, boolean dir) { int cmp = Reads.compareIndices(A, i, j, 0.5, true); - + if (dir == (cmp == 1)) Writes.swap(A, i, j, 0.5, true, false); } @@ -72,9 +72,9 @@ public void changeDirection(String choice) throws Exception { else if(choice.equals("backward")) this.direction = false; else throw new Exception("Invalid direction for Bitonic Sort!"); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.bitonicSort(array, 0, sortLength, this.direction); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BoseNelsonSortIterative.java b/src/main/java/sorts/concurrent/BoseNelsonSortIterative.java index 19b6b6a9..ce26a76e 100644 --- a/src/main/java/sorts/concurrent/BoseNelsonSortIterative.java +++ b/src/main/java/sorts/concurrent/BoseNelsonSortIterative.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BoseNelsonSortIterative extends Sort { +public final class BoseNelsonSortIterative extends Sort { public BoseNelsonSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bose-Nelson (Iterative)"); this.setRunAllSortsName("Iterative Bose-Nelson Sorting Network"); this.setRunSortName("Iterative Bose-Nelson Sort"); @@ -18,20 +18,20 @@ public BoseNelsonSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int end; - + private void compSwap(int[] array, int a, int b) { if(b >= this.end) return; - + if(Reads.compareIndices(array, a, b, 0.25, true) == 1) Writes.swap(array, a, b, 0.5, false, false); } - + private void rangeComp(int[] array, int a, int b, int offset) { - int half = (b-a)/2, m = a+half; + int half = (b-a)/2, m = a+half; a += offset; - + for(int i = 0; i < half - offset; i++) if((i & ~offset) == i) this.compSwap(array, a+i, m+i); } @@ -40,10 +40,10 @@ private void rangeComp(int[] array, int a, int b, int offset) { public void runSort(int[] array, int currentLength, int bucketCount) { this.end = currentLength; currentLength = 1 << (int)(Math.ceil(Math.log(currentLength)/Math.log(2))); - + for(int k = 2; k <= currentLength; k*=2) for(int j = 0; j < k/2; j++) for(int i = 0; i+j < this.end; i+=k) this.rangeComp(array, i, i+k, j); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BoseNelsonSortParallel.java b/src/main/java/sorts/concurrent/BoseNelsonSortParallel.java index a04f8af7..90bc52cb 100644 --- a/src/main/java/sorts/concurrent/BoseNelsonSortParallel.java +++ b/src/main/java/sorts/concurrent/BoseNelsonSortParallel.java @@ -3,11 +3,11 @@ import sorts.templates.Sort; import main.ArrayVisualizer; -final public class BoseNelsonSortParallel extends Sort { - +public final class BoseNelsonSortParallel extends Sort { + public BoseNelsonSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bose-Nelson (Parallel)"); this.setRunAllSortsName("Parallel Bose-Nelson Sorting Network"); this.setRunSortName("Parallel Bose-Nelson Sort"); @@ -19,9 +19,9 @@ public BoseNelsonSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(4096); this.setBogoSort(false); } - + private int[] array; - + private class BoseNelson extends Thread { private int start, length; private double sleep; @@ -34,7 +34,7 @@ public void run() { BoseNelsonSortParallel.this.boseNelson(start, length, sleep); } } - + private class BoseNelsonMerge extends Thread { private int start1, len1, start2, len2; private double sleep; @@ -49,7 +49,7 @@ public void run() { BoseNelsonSortParallel.this.boseNelsonMerge(start1, len1, start2, len2, sleep); } } - + private void compareSwap(int start, int end, double sleep) { if (Reads.compareIndices(array, start, end, sleep, true) == 1) { Writes.swap(array, start, end, 2*sleep, true, false); @@ -63,7 +63,7 @@ private void boseNelson(int start, int length, double sleep) { BoseNelson right = new BoseNelson(start + mid, length - mid, sleep); left.start(); right.start(); - + try { left.join(); right.join(); @@ -73,7 +73,7 @@ private void boseNelson(int start, int length, double sleep) { boseNelsonMerge(start, mid, start + mid, length - mid, sleep); } } - + private void boseNelsonMerge(int start1, int len1, int start2, int len2, double sleep) { if (len1 == 1 && len2 == 1) { compareSwap(start1, start2, sleep); @@ -86,12 +86,12 @@ private void boseNelsonMerge(int start1, int len1, int start2, int len2, double } else { int mid1 = len1 / 2; int mid2 = len1 % 2 == 1 ? len2 / 2 : (len2 + 1) / 2; - + BoseNelsonMerge left = new BoseNelsonMerge(start1, mid1, start2, mid2, sleep); BoseNelsonMerge right = new BoseNelsonMerge(start1 + mid1, len1 - mid1, start2 + mid2, len2 - mid2, sleep); left.start(); right.start(); - + try { left.join(); right.join(); @@ -101,10 +101,10 @@ private void boseNelsonMerge(int start1, int len1, int start2, int len2, double boseNelsonMerge(start1 + mid1, len1 - mid1, start2, mid2, sleep); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.boseNelson(0, length, 0.25); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/BoseNelsonSortRecursive.java b/src/main/java/sorts/concurrent/BoseNelsonSortRecursive.java index 817bea88..e1622be5 100644 --- a/src/main/java/sorts/concurrent/BoseNelsonSortRecursive.java +++ b/src/main/java/sorts/concurrent/BoseNelsonSortRecursive.java @@ -3,11 +3,11 @@ import sorts.templates.Sort; import main.ArrayVisualizer; -final public class BoseNelsonSortRecursive extends Sort { - +public final class BoseNelsonSortRecursive extends Sort { + public BoseNelsonSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bose-Nelson (Recursive)"); this.setRunAllSortsName("Recursive Bose-Nelson Sorting Network"); this.setRunSortName("Recursive Bose-Nelson Sort"); @@ -19,7 +19,7 @@ public BoseNelsonSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void compareSwap(int[] array, int start, int end, double sleep) { if (Reads.compareIndices(array, start, end, sleep, true) == 1) { Writes.swap(array, start, end, 2*sleep, true, false); @@ -34,7 +34,7 @@ private void boseNelson(int[] array, int start, int length, double sleep) { boseNelsonMerge(array, start, mid, start + mid, length - mid, sleep); } } - + private void boseNelsonMerge(int[] array, int start1, int len1, int start2, int len2, double sleep) { if (len1 == 1 && len2 == 1) { compareSwap(array, start1, start2, sleep); @@ -52,9 +52,9 @@ private void boseNelsonMerge(int[] array, int start1, int len1, int start2, int boseNelsonMerge(array, start1 + mid1, len1 - mid1, start2, mid2, sleep); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.boseNelson(array, 0, length, 0.25); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/CreaseSort.java b/src/main/java/sorts/concurrent/CreaseSort.java index 08a7e953..1711cbe5 100644 --- a/src/main/java/sorts/concurrent/CreaseSort.java +++ b/src/main/java/sorts/concurrent/CreaseSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CreaseSort extends Sort { +public final class CreaseSort extends Sort { public CreaseSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Crease"); this.setRunAllSortsName("Crease Sorting Network"); this.setRunSortName("Crease Sort"); @@ -44,7 +44,7 @@ public CreaseSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void compSwap(int[] array, int a, int b) { if(Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); @@ -54,17 +54,17 @@ private void compSwap(int[] array, int a, int b) { public void runSort(int[] array, int length, int bucketCount) { int max = 1; for(; max*2 < length; max*=2); - + int next = max; while(next > 0) { for(int i = 0; i+1 < length; i+=2) this.compSwap(array, i, i+1); - + for(int j = max; j >= next && j > 1; j/=2) for(int i = 1; i+j-1 < length; i+=2) this.compSwap(array, i, i+j-1); - + next /= 2; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/DiamondSortIterative.java b/src/main/java/sorts/concurrent/DiamondSortIterative.java index 01ef9847..57655bf8 100644 --- a/src/main/java/sorts/concurrent/DiamondSortIterative.java +++ b/src/main/java/sorts/concurrent/DiamondSortIterative.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 _fluffyy, yuji implemented by aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DiamondSortIterative extends Sort { +public final class DiamondSortIterative extends Sort { public DiamondSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Diamond (Iterative)"); this.setRunAllSortsName("Iterative Diamond Sort"); this.setRunSortName("Iterative Diamondsort"); @@ -44,7 +44,7 @@ public DiamondSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void compSwap(int[] array, int a, int b) { if(Reads.compareIndices(array, a, b, 0.05, true) == 1) Writes.swap(array, a, b, 0.05, true, false); @@ -54,7 +54,7 @@ private void compSwap(int[] array, int a, int b) { public void runSort(int[] array, int length, int bucketCount) throws Exception { int n = 1; for(; n < length; n *= 2); - + int m = 4; for(; m <= n; m *= 2) { for(int k = 0; k < m/2; k++) { @@ -70,4 +70,4 @@ public void runSort(int[] array, int length, int bucketCount) throws Exception { for(int i = k; i+1 < Math.min(length, m-k); i += 2) this.compSwap(array, i, i+1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/DiamondSortRecursive.java b/src/main/java/sorts/concurrent/DiamondSortRecursive.java index 1939633e..e0c8bc86 100644 --- a/src/main/java/sorts/concurrent/DiamondSortRecursive.java +++ b/src/main/java/sorts/concurrent/DiamondSortRecursive.java @@ -41,4 +41,4 @@ private void sort(int[] arr, int start, int stop, boolean merge) { public void runSort(int[] arr, int length, int buckets) { this.sort(arr, 0, length, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/FoldSort.java b/src/main/java/sorts/concurrent/FoldSort.java index aa1a2029..836407e9 100644 --- a/src/main/java/sorts/concurrent/FoldSort.java +++ b/src/main/java/sorts/concurrent/FoldSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 Marcel Pi Nacy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class FoldSort extends Sort { +public final class FoldSort extends Sort { public FoldSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Fold"); this.setRunAllSortsName("Fold Sorting Network"); this.setRunSortName("Fold Sort"); @@ -44,14 +44,14 @@ public FoldSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + int end; - + void compSwap(int[] array, int a, int b) { if(b < end && Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); } - + void halver(int[] array, int low, int high) { while (low < high) @@ -64,10 +64,10 @@ void halver(int[] array, int low, int high) public void runSort(int[] array, int size, int bucketCount) { int ceilLog = 1; for (; (1 << ceilLog) < size; ceilLog++); - + end = size; size = 1 << ceilLog; - + for (int k = size >> 1; k > 0; k >>= 1) //log(N) { for (int i = size; i >= k; i >>= 1) //log(N) @@ -79,4 +79,4 @@ public void runSort(int[] array, int size, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/MatrixSort.java b/src/main/java/sorts/concurrent/MatrixSort.java index fa7b80e1..a5e188f2 100644 --- a/src/main/java/sorts/concurrent/MatrixSort.java +++ b/src/main/java/sorts/concurrent/MatrixSort.java @@ -7,7 +7,7 @@ Idea made by Control#2866 in The Studio Discord Server (https://discord.com/invite/2xGkKC2) */ -final public class MatrixSort extends Sort { +public final class MatrixSort extends Sort { private class MatrixShape { int width; boolean unbalanced, insertLast; @@ -123,4 +123,4 @@ else if (length <= 16) { public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.matrixSort(array, 0, sortLength, 1, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/MergeExchangeSortIterative.java b/src/main/java/sorts/concurrent/MergeExchangeSortIterative.java index 15ff2eed..818860ce 100644 --- a/src/main/java/sorts/concurrent/MergeExchangeSortIterative.java +++ b/src/main/java/sorts/concurrent/MergeExchangeSortIterative.java @@ -3,11 +3,11 @@ import sorts.templates.Sort; import main.ArrayVisualizer; -final public class MergeExchangeSortIterative extends Sort { - +public final class MergeExchangeSortIterative extends Sort { + public MergeExchangeSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Merge-Exchange (Iterative)"); this.setRunAllSortsName("Batcher's Merge-Exchange Sort"); this.setRunSortName("Iterative Merge-Exchange Sort"); @@ -19,7 +19,7 @@ public MergeExchangeSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int t = (int)(Math.log(length-1) / Math.log(2))+1; @@ -41,4 +41,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/OddEvenMergeSortIterative.java b/src/main/java/sorts/concurrent/OddEvenMergeSortIterative.java index d754fa13..db7f3df5 100644 --- a/src/main/java/sorts/concurrent/OddEvenMergeSortIterative.java +++ b/src/main/java/sorts/concurrent/OddEvenMergeSortIterative.java @@ -6,15 +6,15 @@ /* * This version of Odd-Even Merge Sort was taken from here, written by wkpark on StackOverflow: * https://stackoverflow.com/questions/34426337/how-to-fix-this-non-recursive-odd-even-merge-sort-algorithm - * + * * Thanks to Piotr Grochowski for rewriting code to allow this implementation to * work for array lengths other than powers of two! */ -final public class OddEvenMergeSortIterative extends Sort { +public final class OddEvenMergeSortIterative extends Sort { public OddEvenMergeSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Odd-Even Merge (Iterative)"); this.setRunAllSortsName("Iterative Odd-Even Merge Sort"); this.setRunSortName("Iterative Odd-Even Mergesort"); @@ -38,4 +38,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti if(Reads.compareIndices(array, i + j, i + j + k, 0.5, true) > 0) Writes.swap(array, i + j, i + j + k, 0.5, true, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/OddEvenMergeSortParallel.java b/src/main/java/sorts/concurrent/OddEvenMergeSortParallel.java index 90a95e29..746c771f 100644 --- a/src/main/java/sorts/concurrent/OddEvenMergeSortParallel.java +++ b/src/main/java/sorts/concurrent/OddEvenMergeSortParallel.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 Piotr Grochowski @@ -30,10 +30,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class OddEvenMergeSortParallel extends Sort { +public final class OddEvenMergeSortParallel extends Sort { public OddEvenMergeSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Odd-Even Merge (Parallel)"); this.setRunAllSortsName("Parallel Odd-Even Merge Sort"); this.setRunSortName("Parallel Odd-Even Mergesort"); @@ -45,12 +45,12 @@ public OddEvenMergeSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(4096); this.setBogoSort(false); } - + private int[] array; - + private class OddEvenMerge extends Thread { private int lo, m2, n, r; - + OddEvenMerge(int lo, int m2, int n, int r) { this.lo = lo; this.m2 = m2; @@ -61,10 +61,10 @@ public void run() { OddEvenMergeSortParallel.this.oddEvenMerge(lo, m2, n, r); } } - + private class OddEvenMergeSort extends Thread { private int lo, n; - + OddEvenMergeSort(int lo, int n) { this.lo = lo; this.n = n; @@ -73,17 +73,17 @@ public void run() { OddEvenMergeSortParallel.this.oddEvenMergeSort(lo, n); } } - + private void compSwap(int a, int b) { if(Reads.compareIndices(array, a, b, 1, true) == 1) Writes.swap(array, a, b, 1, true, false); } - + private void oddEvenMerge(int lo, int m2, int n, int r) { int m = r * 2; if(m < n) { OddEvenMerge left, right; - + if((n/r)%2 != 0) { left = new OddEvenMerge(lo, (m2+1)/2, n+r, m); right = new OddEvenMerge(lo+r, m2/2, n-r, m); @@ -92,22 +92,22 @@ private void oddEvenMerge(int lo, int m2, int n, int r) { left = new OddEvenMerge(lo, (m2+1)/2, n, m); right = new OddEvenMerge(lo+r, m2/2, n, m); } - + left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch(InterruptedException e) { Thread.currentThread().interrupt(); } if(m2%2 != 0) - for(int i = lo; i + r < lo + n; i += m) + for(int i = lo; i + r < lo + n; i += m) this.compSwap(i, i + r); - + else for(int i = lo + r; i + r < lo + n; i += m) this.compSwap(i, i + r); @@ -118,21 +118,21 @@ private void oddEvenMerge(int lo, int m2, int n, int r) { private void oddEvenMergeSort(int lo, int n) { if (n > 1) { int m = n / 2; - + OddEvenMergeSort left = new OddEvenMergeSort(lo, m); OddEvenMergeSort right = new OddEvenMergeSort(lo + m, n-m); - + left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch(InterruptedException e) { Thread.currentThread().interrupt(); } - + this.oddEvenMerge(lo, m, n, 1); } } @@ -142,4 +142,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.oddEvenMergeSort(0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/OddEvenMergeSortRecursive.java b/src/main/java/sorts/concurrent/OddEvenMergeSortRecursive.java index 1dd80e11..e475f943 100644 --- a/src/main/java/sorts/concurrent/OddEvenMergeSortRecursive.java +++ b/src/main/java/sorts/concurrent/OddEvenMergeSortRecursive.java @@ -6,12 +6,12 @@ /* * This version of Odd-Even Merge Sort was taken from here, written by H.W. Lang: * http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/networks/oemen.htm - * + * * Thanks to Piotr Grochowski for rewriting code to allow this implementation to * work for array lengths other than powers of two! */ -final public class OddEvenMergeSortRecursive extends Sort { +public final class OddEvenMergeSortRecursive extends Sort { public OddEvenMergeSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -80,4 +80,4 @@ void oddEvenMergeSort(int[] array, int lo, int n) { public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.oddEvenMergeSort(array, 0, sortLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/PairwiseMergeSortIterative.java b/src/main/java/sorts/concurrent/PairwiseMergeSortIterative.java index 7c98566c..7e34f1b0 100644 --- a/src/main/java/sorts/concurrent/PairwiseMergeSortIterative.java +++ b/src/main/java/sorts/concurrent/PairwiseMergeSortIterative.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class PairwiseMergeSortIterative extends Sort { +public final class PairwiseMergeSortIterative extends Sort { public PairwiseMergeSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,9 +44,9 @@ public PairwiseMergeSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int end; - + private void compSwap(int[] array, int a, int b) { if(b < this.end && Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); @@ -57,12 +57,12 @@ public void runSort(int[] array, int length, int bucketCount) throws Exception { this.end = length; int n = 1; for(; n < length; n <<= 1); - + for(int k = n >> 1; k > 0; k >>= 1) for(int j = 0; j < length; j += k << 1) for(int i = 0; i < k; i++) this.compSwap(array, j+i, j+k+i); - + for(int k = 2; k < n; k <<= 1) for(int m = k >> 1; m > 0; m >>= 1) for(int j = 0; j < length; j += k<<1) @@ -70,4 +70,4 @@ public void runSort(int[] array, int length, int bucketCount) throws Exception { for(int i = 0; i < m; i++) this.compSwap(array, j+p+i, j+p+m+i); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/PairwiseMergeSortRecursive.java b/src/main/java/sorts/concurrent/PairwiseMergeSortRecursive.java index c7fb43b1..0e9c85a5 100644 --- a/src/main/java/sorts/concurrent/PairwiseMergeSortRecursive.java +++ b/src/main/java/sorts/concurrent/PairwiseMergeSortRecursive.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class PairwiseMergeSortRecursive extends Sort { +public final class PairwiseMergeSortRecursive extends Sort { public PairwiseMergeSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,30 +44,30 @@ public PairwiseMergeSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int end; - + private void compSwap(int[] array, int a, int b) { if(b < this.end && Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); } - + private void pairwiseMerge(int[] array, int a, int b) { int m = (a+b)/2, m1 = (a+m)/2, g = m-m1; - + for(int i = 0; m1+i < m; i++) for(int j = m1, k = g; k > 0; k >>= 1, j -= k-(i&k)) this.compSwap(array, j+i, j+i+k); - + if(b-a > 4) this.pairwiseMerge(array, m, b); } - + private void pairwiseMergeSort(int[] array, int a, int b) { int m = (a+b)/2; - + for(int i = a, j = m; i < m; i++, j++) this.compSwap(array, i, j); - + if(b-a > 2) { this.pairwiseMergeSort(array, a, m); this.pairwiseMergeSort(array, m, b); @@ -80,7 +80,7 @@ public void runSort(int[] array, int length, int bucketCount) throws Exception { this.end = length; int n = 1; for(; n < length; n <<= 1); - + this.pairwiseMergeSort(array, 0, n); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/PairwiseSortIterative.java b/src/main/java/sorts/concurrent/PairwiseSortIterative.java index 92319c75..d771035a 100644 --- a/src/main/java/sorts/concurrent/PairwiseSortIterative.java +++ b/src/main/java/sorts/concurrent/PairwiseSortIterative.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 PiotrGrochowski Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,10 +25,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class PairwiseSortIterative extends Sort { +public final class PairwiseSortIterative extends Sort { public PairwiseSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Pairwise (Iterative)"); this.setRunAllSortsName("Iterative Pairwise Sorting Network"); this.setRunSortName("Iterative Pairwise Sort"); @@ -85,9 +85,9 @@ private void iterativepairwise(int[] array, int length, double sleep) { e = (e * 2) + 1; } } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.iterativepairwise(array, sortLength, 0.5); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/PairwiseSortRecursive.java b/src/main/java/sorts/concurrent/PairwiseSortRecursive.java index 550ade75..4e6a437f 100644 --- a/src/main/java/sorts/concurrent/PairwiseSortRecursive.java +++ b/src/main/java/sorts/concurrent/PairwiseSortRecursive.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 PiotrGrochowski Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,7 +25,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class PairwiseSortRecursive extends Sort { +public final class PairwiseSortRecursive extends Sort { public PairwiseSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -83,4 +83,4 @@ private void pairwiserecursive(int[] array, int start, int end, int gap, double public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.pairwiserecursive(array, 0, sortLength, 1, 0.5); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/WeaveSortIterative.java b/src/main/java/sorts/concurrent/WeaveSortIterative.java index 18dd0c14..bb8178ea 100644 --- a/src/main/java/sorts/concurrent/WeaveSortIterative.java +++ b/src/main/java/sorts/concurrent/WeaveSortIterative.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class WeaveSortIterative extends Sort { +public final class WeaveSortIterative extends Sort { public WeaveSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Weave (Iterative)"); this.setRunAllSortsName("Iterative Weave Sorting Network"); this.setRunSortName("Iterative Weave Sort"); @@ -44,9 +44,9 @@ public WeaveSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int end; - + private void compSwap(int[] array, int a, int b) { if(b < this.end && Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); @@ -57,7 +57,7 @@ public void runSort(int[] array, int length, int bucketCount) { this.end = length; int n = 1; for(; n < length; n*=2); - + for(int i = 1; i < n; i*=2) for(int j = 1; j <= i; j*=2) for(int k = 0; k < n; k += n/j) @@ -65,4 +65,4 @@ public void runSort(int[] array, int length, int bucketCount) { for(int p = 0; p < d; p++, m++) this.compSwap(array, k+m, k+l+p); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/WeaveSortParallel.java b/src/main/java/sorts/concurrent/WeaveSortParallel.java index 8cc455df..f06b6758 100644 --- a/src/main/java/sorts/concurrent/WeaveSortParallel.java +++ b/src/main/java/sorts/concurrent/WeaveSortParallel.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class WeaveSortParallel extends Sort { +public final class WeaveSortParallel extends Sort { private int[] arr; private int length; private double DELAY = 1; @@ -92,4 +92,4 @@ public void runSort(int[] arr, int length, int bucketCount) { this.length = length; this.wrapper(0, 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/concurrent/WeaveSortRecursive.java b/src/main/java/sorts/concurrent/WeaveSortRecursive.java index a529f319..0df6bcdf 100644 --- a/src/main/java/sorts/concurrent/WeaveSortRecursive.java +++ b/src/main/java/sorts/concurrent/WeaveSortRecursive.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class WeaveSortRecursive extends Sort { +public final class WeaveSortRecursive extends Sort { public WeaveSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Weave (Recursive)"); this.setRunAllSortsName("Recursive Weave Sorting Network"); this.setRunSortName("Recursive Weave Sort"); @@ -44,30 +44,30 @@ public WeaveSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int end; - + private void compSwap(int[] array, int a, int b) { if(b < this.end && Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); } - + private void circle(int[] array, int pos, int len, int gap) { if(len < 2) return; - + for(int i = 0; 2*i < (len-1)*gap; i+=gap) this.compSwap(array, pos+i, pos+(len-1)*gap-i); - + this.circle(array, pos, len/2, gap); if(pos+len*gap/2 < this.end) this.circle(array, pos+len*gap/2, len/2, gap); } - + private void weaveCircle(int[] array, int pos, int len, int gap) { if(len < 2) return; - + this.weaveCircle(array, pos, len/2, 2*gap); this.weaveCircle(array, pos+gap, len/2, 2*gap); - + this.circle(array, pos, len, gap); } @@ -76,7 +76,7 @@ public void runSort(int[] array, int length, int bucketCount) { this.end = length; int n = 1; for(; n < length; n*=2); - + this.weaveCircle(array, 0, n, 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/AmericanFlagSort.java b/src/main/java/sorts/distribute/AmericanFlagSort.java index 263d0fbf..8dbc8114 100644 --- a/src/main/java/sorts/distribute/AmericanFlagSort.java +++ b/src/main/java/sorts/distribute/AmericanFlagSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * Copyright 2017 Justin Wetherell Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,7 @@ * distributes items into hundreds of buckets. Non-comparative sorting * algorithms such as radix sort and American flag sort are typically used to * sort large objects such as strings, for which comparison is not a unit-time - * operation. + * operation. *

* Family: Bucket.
* Space: In-place.
@@ -43,12 +43,12 @@ * @author Justin Wetherell */ -final public class AmericanFlagSort extends Sort { +public final class AmericanFlagSort extends Sort { private int NUMBER_OF_BUCKETS = 128; // ex. 10 for base 10 numbers - + public AmericanFlagSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("American Flag"); this.setRunAllSortsName("American Flag Sort, " + this.NUMBER_OF_BUCKETS + " Buckets"); this.setRunSortName("American Flag Sort"); @@ -60,15 +60,15 @@ public AmericanFlagSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // Slightly different than Reads.analyzeMaxLog. private int getMaxNumberOfDigits(int[] array, int length) { int max = Integer.MIN_VALUE; int temp = 0; - + for (int i = 0; i < length; i++) { temp = (int) (Math.log(array[i]) / Math.log(this.NUMBER_OF_BUCKETS)) + 1; - + if (temp > max) max = temp; } @@ -78,49 +78,49 @@ private int getMaxNumberOfDigits(int[] array, int length) { private int getDigit(int integer, int divisor) { return (integer / divisor) % this.NUMBER_OF_BUCKETS; } - + private void sort(int[] array, int start, int length, int divisor) { // First pass - find counts int[] count = new int[this.NUMBER_OF_BUCKETS]; int[] offset = new int[this.NUMBER_OF_BUCKETS]; Writes.changeAllocAmount(2 * this.NUMBER_OF_BUCKETS); int digit = 0; - + for (int i = start; i < length; i++) { Highlights.markArray(1, i); Delays.sleep(0.75); - + int d = array[i]; digit = this.getDigit(d, divisor); - + Writes.write(count, digit, count[digit] + 1, 0, false, true); } - + Writes.write(offset, 0, start + 0, 0, false, true); - + for (int i = 1; i < this.NUMBER_OF_BUCKETS; i++) { Writes.write(offset, i, count[i - 1] + offset[i - 1], 0, false, true); } - + // Second pass - move into position for (int b = 0; b < this.NUMBER_OF_BUCKETS; b++) { while (count[b] > 0) { int origin = offset[b]; int from = origin; int num = array[from]; - + Writes.visualClear(array, from, 0.5); - + do { digit = this.getDigit(num, divisor); int to = offset[digit]; - + Writes.write(offset, digit, offset[digit] + 1, 0, false, true); Writes.write(count, digit, count[digit] - 1, 0, false, true); - + int temp = array[to]; Writes.write(array, to, num, 0.75, true, false); - + num = temp; from = to; } while (from != origin); @@ -131,7 +131,7 @@ private void sort(int[] array, int start, int length, int divisor) { for (int i = 0; i < this.NUMBER_OF_BUCKETS; i++) { int begin = (i > 0) ? offset[i - 1] : start; int end = offset[i]; - + if (end - begin > 1) this.sort(array, begin, end, divisor / this.NUMBER_OF_BUCKETS); } @@ -144,13 +144,13 @@ private void sort(int[] array, int start, int length, int divisor) { public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.NUMBER_OF_BUCKETS = bucketCount; this.setRunAllSortsName("American Flag Sort, " + this.NUMBER_OF_BUCKETS + " Buckets"); - + int numberOfDigits = this.getMaxNumberOfDigits(array, sortLength); // Max number of digits int max = 1; - + for (int i = 0; i < numberOfDigits - 1; i++) max *= this.NUMBER_OF_BUCKETS; - + this.sort(array, 0, sortLength, max); } - } \ No newline at end of file + } diff --git a/src/main/java/sorts/distribute/BinaryQuickSortIterative.java b/src/main/java/sorts/distribute/BinaryQuickSortIterative.java index a800f6c8..6005fc93 100644 --- a/src/main/java/sorts/distribute/BinaryQuickSortIterative.java +++ b/src/main/java/sorts/distribute/BinaryQuickSortIterative.java @@ -20,10 +20,10 @@ * @author Skeen */ -final public class BinaryQuickSortIterative extends BinaryQuickSorting { +public final class BinaryQuickSortIterative extends BinaryQuickSorting { public BinaryQuickSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binary Quick (Iterative with a Queue)"); this.setRunAllSortsName("Iterative Binary Quick Sort"); this.setRunSortName("Iterative Binary Quicksort"); @@ -35,10 +35,10 @@ public BinaryQuickSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int mostSignificantBit = Reads.analyzeBit(array, sortLength); this.binaryQuickSort(array, 0, sortLength - 1, mostSignificantBit); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/BinaryQuickSortRecursive.java b/src/main/java/sorts/distribute/BinaryQuickSortRecursive.java index 770cc716..da6a1b92 100644 --- a/src/main/java/sorts/distribute/BinaryQuickSortRecursive.java +++ b/src/main/java/sorts/distribute/BinaryQuickSortRecursive.java @@ -20,10 +20,10 @@ * @author Skeen */ -final public class BinaryQuickSortRecursive extends BinaryQuickSorting { +public final class BinaryQuickSortRecursive extends BinaryQuickSorting { public BinaryQuickSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binary Quick (Recursive)"); this.setRunAllSortsName("Recursive Binary Quick Sort"); this.setRunSortName("Recursive Binary Quicksort"); @@ -35,10 +35,10 @@ public BinaryQuickSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int mostSignificantBit = Reads.analyzeBit(array, sortLength); this.binaryQuickSortRecursive(array, 0, sortLength - 1, mostSignificantBit); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/ClassicGravitySort.java b/src/main/java/sorts/distribute/ClassicGravitySort.java index a40f99a8..c72311e7 100644 --- a/src/main/java/sorts/distribute/ClassicGravitySort.java +++ b/src/main/java/sorts/distribute/ClassicGravitySort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ClassicGravitySort extends Sort { +public final class ClassicGravitySort extends Sort { public ClassicGravitySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Classic Gravity"); this.setRunAllSortsName("Classic Gravity (Bead) Sort"); this.setRunSortName("Classic Beadsort"); @@ -44,7 +44,7 @@ public ClassicGravitySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int max = Reads.analyzeMax(array, length, 0.5, true); @@ -75,4 +75,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.deleteExternalArray(transpose); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/CountingSort.java b/src/main/java/sorts/distribute/CountingSort.java index a2202888..33e4054a 100644 --- a/src/main/java/sorts/distribute/CountingSort.java +++ b/src/main/java/sorts/distribute/CountingSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CountingSort extends Sort { +public final class CountingSort extends Sort { public CountingSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Counting"); this.setRunAllSortsName("Counting Sort"); this.setRunSortName("Counting Sort"); @@ -48,24 +48,24 @@ public CountingSort(ArrayVisualizer arrayVisualizer) { @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int max = Reads.analyzeMax(array, sortLength, 0, false); - + int[] output = Writes.copyOfArray(array, sortLength); int[] counts = Writes.createExternalArray(max + 1); - - for (int i = 0; i < sortLength; i++) { + + for (int i = 0; i < sortLength; i++) { Writes.write(counts, array[i], counts[array[i]] + 1, 1, false, true); Highlights.markArray(1, i); - } + } - for (int i = 1; i < counts.length; i++) { + for (int i = 1; i < counts.length; i++) { Writes.write(counts, i, counts[i] + counts[i - 1], 1, true, true); - } + } for (int i = sortLength - 1; i >= 0; i--) { output[counts[array[i]] - 1] = array[i]; counts[array[i]]--; } - + // Extra loop to simulate the results from the "output" array being written // to the visual array. for (int i = sortLength - 1; i >= 0; i--) { @@ -76,4 +76,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Writes.deleteExternalArray(output); Writes.deleteExternalArray(counts); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/FlashSort.java b/src/main/java/sorts/distribute/FlashSort.java index dcdffdcf..84a25d62 100644 --- a/src/main/java/sorts/distribute/FlashSort.java +++ b/src/main/java/sorts/distribute/FlashSort.java @@ -6,10 +6,10 @@ import sorts.insert.InsertionSort; import sorts.templates.Sort; -final public class FlashSort extends Sort { +public final class FlashSort extends Sort { public FlashSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Flash"); this.setRunAllSortsName("Flash Sort"); this.setRunSortName("Flashsort"); @@ -21,7 +21,7 @@ public FlashSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // The flashsort algorithm is attributed to Karl-Dietrich Neubert // The translation to C++ is provided by Clint Jed Casper // Refactored in Java by MusicTheorist @@ -29,7 +29,7 @@ public FlashSort(ArrayVisualizer arrayVisualizer) { // sorts an array in place in O(n) time using 20% of the // memory used by the array for storing intermediate, // temporary computations - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { if(sortLength == 0) return; @@ -54,7 +54,7 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti int bigIndex; Highlights.markArray(1, i); - + //which is bigger A(i) or A(i+1) if(Reads.compareValues(array[i], array[i + 1]) == -1) { @@ -79,7 +79,7 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti { min = small; } - + Delays.sleep(1); } @@ -97,7 +97,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) Delays.sleep(1); Highlights.clearMark(1); - + if(max == min) { //all the elements are the same @@ -108,7 +108,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) //note that L is in the range 1...m (hence //the extra 1) int[] L = Writes.createExternalArray(m + 1); - + //O(m) //initialize L to contain all zeros (L[0] is unused) for(int t = 1; t <= m; t++) @@ -131,9 +131,9 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) int K; for(int h = 0; h < sortLength; h++) { - + Highlights.markArray(1, h); - + //classify the A(i) value K = ((int)((array[h] - min) * c)) + 1; @@ -141,7 +141,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) Writes.write(L, K, L[K] + 1, 1, false, true); } Highlights.clearMark(1); - + //O(m) //sum over each L(i) such that each L(i) contains //the number of A(i) values that are in the ith @@ -157,13 +157,13 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) Writes.swap(array, maxIndex, 0, 1, true, false); Highlights.clearMark(1); Highlights.clearMark(2); - + //Except when being iterated upwards, //j always points to the first A(i) that starts //a new class boundary && that class hasn't yet //had all of its elements moved inside its borders; - //This is called a cycle leader since you know + //This is called a cycle leader since you know //that you can begin permuting again here. You know //this because it is the lowest index of the class //and as such A(j) must be out of place or else all @@ -171,7 +171,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) //within the borders of the this class (which means //j wouldn't be pointing to this A(i) in the first place) int j = 0; - + //K is the class of an A(i) value. It is always in the range 1..m K = m; @@ -223,7 +223,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) Writes.write(array, location, evicted, 1, false, false); Highlights.markArray(1, location); evicted = temp; - + //decrease the count for this class //see counting sort for why this is done Writes.write(L, K, L[K] - 1, 0, false, true); @@ -233,7 +233,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) } } Highlights.clearMark(1); - + //-------RECURSION or STRAIGHT INSERTION------- //if the classes do not have the A(i) values uniformly distributed @@ -247,7 +247,7 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) //if the class has 25% more elements than it should int threshold = (int)(1.25 * ((sortLength / m) + 1)); int minElements = 30; - + //for each class decide whether to insertion sort its members //or recursively flashsort its members; //skip the K == m class because it is already sorted @@ -263,10 +263,10 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) //of it then... if(classSize > threshold && classSize > minElements) { - //...attempt to flashsort the class. This will work + //...attempt to flashsort the class. This will work //well if the elements inside the class are uniformly - //distributed throughout the class otherwise it will - //perform badly, O(n^2) worst case, since we will have + //distributed throughout the class otherwise it will + //perform badly, O(n^2) worst case, since we will have //performed another classification and permutation step //and not succeeded in making the problem significantly //smaller for the next level of recursion. However, @@ -282,4 +282,4 @@ else if(Reads.compareValues(array[sortLength - 1], max) == 1) Writes.deleteExternalArray(L); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/GravitySort.java b/src/main/java/sorts/distribute/GravitySort.java index e27d65fd..a83f70ee 100644 --- a/src/main/java/sorts/distribute/GravitySort.java +++ b/src/main/java/sorts/distribute/GravitySort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class GravitySort extends Sort { +public final class GravitySort extends Sort { public GravitySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Gravity"); this.setRunAllSortsName("Gravity (Bead) Sort"); this.setRunSortName("Beadsort"); @@ -44,49 +44,49 @@ public GravitySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int min = array[0], max = array[0]; - + for(int i = 1; i < length; i++) { if(array[i] < min) min = array[i]; if(array[i] > max) max = array[i]; } - + int[] x = Writes.createExternalArray(length); int[] y = Writes.createExternalArray(max - min + 1); - + double delay = Math.max(2d / length, 0.001); - + //save a copy of array-min in x //increase count of the array-min value in y for(int i = 0; i < length; i++) { Writes.write(x, i, array[i]-min, 0, true, true); Writes.write(y, array[i]-min, y[array[i]-min]+1, 1, false, true); } - + //do a partial sum backwards to determine how many elements are greater than a value for(int i = y.length-1; i > 0; i--) Writes.write(y, i-1, y[i-1]+=y[i], 1, true, true); - + //iterate for every integer value in the array range for(int j = y.length-1; j >= 0; j--) { Highlights.markArray(2, length-y[j]); - + //iterate for every item in array and x for(int i = 0; i < length; i++) { Highlights.markArray(1, i); Delays.sleep(delay); - + int inc = (i >= length-y[j] ? 1 : 0) - (x[i] >= j ? 1 : 0); - + //update the main array Writes.write(array, i, array[i]+inc, delay, true, false); } } - + Writes.deleteExternalArray(x); Writes.deleteExternalArray(y); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/InPlaceLSDRadixSort.java b/src/main/java/sorts/distribute/InPlaceLSDRadixSort.java index a41dcb62..4807d927 100644 --- a/src/main/java/sorts/distribute/InPlaceLSDRadixSort.java +++ b/src/main/java/sorts/distribute/InPlaceLSDRadixSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class InPlaceLSDRadixSort extends Sort { +public final class InPlaceLSDRadixSort extends Sort { public InPlaceLSDRadixSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("In-Place LSD Radix"); //this.setRunAllID("In-Place LSD Radix Sort, Base 2"); this.setRunAllSortsName("In-Place LSD Radix Sort, Base 10"); @@ -49,33 +49,33 @@ public InPlaceLSDRadixSort(ArrayVisualizer arrayVisualizer) { @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.setRunAllSortsName("In-Place LSD Radix Sort, Base " + bucketCount); - + int pos = 0; int[] vregs = new int[bucketCount - 1]; Writes.changeAllocAmount(vregs.length); - + int maxpower = Reads.analyzeMaxLog(array, sortLength, bucketCount, 0.5, true); - + for(int p = 0; p <= maxpower; p++){ for(int i = 0; i < vregs.length; i++) { Writes.write(vregs, i, sortLength - 1, 0, false, true); } - + pos = 0; - + for(int i = 0; i < sortLength; i++){ int digit = Reads.getDigit(array[pos], p, bucketCount); - + if(digit == 0) { pos++; Highlights.markArray(0, pos); - } + } else { for(int j = 0; j < vregs.length;j++) Highlights.markArray(j + 1, vregs[j]); - + Writes.multiSwap(array, pos, vregs[digit - 1], bucketCount / 10000d, false, false); - + for(int j = digit - 1; j > 0; j--) { Writes.write(vregs, j - 1, vregs[j - 1] - 1, 0, false, true); } @@ -85,4 +85,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Writes.changeAllocAmount(-vregs.length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/IndexSort.java b/src/main/java/sorts/distribute/IndexSort.java index 81ea6a86..afe29954 100644 --- a/src/main/java/sorts/distribute/IndexSort.java +++ b/src/main/java/sorts/distribute/IndexSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 Gaming32 @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class IndexSort extends Sort { +public final class IndexSort extends Sort { public IndexSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Index"); this.setRunAllSortsName("Simple Static Sort (Index Sort)"); this.setRunSortName("Index Sort (Simple Static Sort)"); @@ -62,4 +62,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Delays.sleep(1); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/LSDRadixSort.java b/src/main/java/sorts/distribute/LSDRadixSort.java index c1c0d4db..03830069 100644 --- a/src/main/java/sorts/distribute/LSDRadixSort.java +++ b/src/main/java/sorts/distribute/LSDRadixSort.java @@ -6,7 +6,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -31,10 +31,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class LSDRadixSort extends Sort { +public final class LSDRadixSort extends Sort { public LSDRadixSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("LSD Radix"); this.setRunAllSortsName("Least Significant Digit Radix Sort, Base 4"); this.setRunSortName("Least Significant Digit Radixsort"); @@ -50,22 +50,22 @@ public LSDRadixSort(ArrayVisualizer arrayVisualizer) { @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.setRunAllSortsName("Least Significant Digit Radix Sort, Base " + bucketCount); - + int highestpower = Reads.analyzeMaxLog(array, sortLength, bucketCount, 0.5, true); - + @SuppressWarnings("unchecked") ArrayList[] registers = new ArrayList[bucketCount]; - + for(int i = 0; i < bucketCount; i++) registers[i] = new ArrayList<>(); - + for(int p = 0; p <= highestpower; p++){ for(int i = 0; i < sortLength; i++){ Highlights.markArray(1, i); - + int digit = Reads.getDigit(array[i], p, bucketCount); Writes.arrayListAdd(registers[digit], array[i]); - + Writes.mockWrite(sortLength, digit, array[i], 1); } @@ -74,4 +74,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Writes.deleteExternalArray(registers); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/MSDRadixSort.java b/src/main/java/sorts/distribute/MSDRadixSort.java index 034c9139..46b818bf 100644 --- a/src/main/java/sorts/distribute/MSDRadixSort.java +++ b/src/main/java/sorts/distribute/MSDRadixSort.java @@ -6,7 +6,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -31,10 +31,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class MSDRadixSort extends Sort { +public final class MSDRadixSort extends Sort { public MSDRadixSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("MSD Radix"); //this.setRunAllID("Most Significant Digit Radix Sort"); this.setRunAllSortsName("Most Significant Digit Radix Sort, Base 4"); @@ -51,34 +51,34 @@ public MSDRadixSort(ArrayVisualizer arrayVisualizer) { private void radixMSD(int[] array, int length, int min, int max, int radix, int pow) { if(min >= max || pow < 0) return; - + Highlights.markArray(2, max - 1); Highlights.markArray(3, min); - + @SuppressWarnings("unchecked") ArrayList[] registers = new ArrayList[radix]; - + for(int i = 0; i < radix; i++) registers[i] = new ArrayList<>(); - + for(int i = min; i < max; i++) { Highlights.markArray(1, i); - + int digit = Reads.getDigit(array[i], pow, radix); Writes.arrayListAdd(registers[digit], array[i]); - + Writes.mockWrite(length, digit, array[i], 1); } - + Highlights.clearMark(2); Highlights.clearMark(3); - + Writes.transcribeMSD(array, registers, 0, min, 0.8, true, false); - + int sum = 0; for(int i = 0; i < registers.length; i++) { this.radixMSD(array, length, sum + min, sum + min + registers[i].size(), radix, pow-1); - + sum += registers[i].size(); Writes.arrayListClear(registers[i]); Writes.changeAuxWrites(registers[i].size()); @@ -86,11 +86,11 @@ private void radixMSD(int[] array, int length, int min, int max, int radix, int Writes.deleteExternalArray(registers); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int highestpower = Reads.analyzeMaxLog(array, sortLength, bucketCount, 0.5, true); - + radixMSD(array, sortLength, 0, sortLength, bucketCount, highestpower); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/PigeonholeSort.java b/src/main/java/sorts/distribute/PigeonholeSort.java index 675bb56c..21988cb4 100644 --- a/src/main/java/sorts/distribute/PigeonholeSort.java +++ b/src/main/java/sorts/distribute/PigeonholeSort.java @@ -7,7 +7,7 @@ * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS * LICENSE OR COPYRIGHT LAW IS PROHIBITED. - * + * * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. * TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN * CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. @@ -15,10 +15,10 @@ // Code refactored from the Python implementation found here: https://en.wikipedia.org/wiki/Pigeonhole_sort -final public class PigeonholeSort extends Sort { +public final class PigeonholeSort extends Sort { public PigeonholeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Pigeonhole"); this.setRunAllSortsName("Pigeonhole Sort"); this.setRunSortName("Pigeonhole Sort"); @@ -30,12 +30,12 @@ public PigeonholeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; - + for(int i = 0; i < sortLength; i++) { if(array[i] < min) { min = array[i]; @@ -44,23 +44,23 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti max = array[i]; } } - + int mi = min; int size = max - mi + 1; int[] holes = Writes.createExternalArray(size); - + for(int x = 0; x < sortLength; x++) { Writes.write(holes, array[x] - mi, holes[array[x] - mi] + 1, 1, false, true); Highlights.markArray(1, x); } - + int j = 0; - + for(int count = 0; count < size; count++) { while(holes[count] > 0) { Writes.write(holes, count, holes[count] - 1, 0, false, true); Writes.write(array, j, count + mi, 1, false, false); - + Highlights.markArray(1, j); j++; } @@ -68,4 +68,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Writes.deleteExternalArray(holes); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/RotateLSDRadixSort.java b/src/main/java/sorts/distribute/RotateLSDRadixSort.java index 6ce62719..f5dad8c1 100644 --- a/src/main/java/sorts/distribute/RotateLSDRadixSort.java +++ b/src/main/java/sorts/distribute/RotateLSDRadixSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RotateLSDRadixSort extends Sort { +public final class RotateLSDRadixSort extends Sort { public RotateLSDRadixSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -116,4 +116,4 @@ public void runSort(int[] array, int length, int bucketCount) { for(int i = 0; i <= max; i++) this.mergeSort(array, 0, length, i); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/RotateMSDRadixSort.java b/src/main/java/sorts/distribute/RotateMSDRadixSort.java index 1ff434f0..e1e8ed83 100644 --- a/src/main/java/sorts/distribute/RotateMSDRadixSort.java +++ b/src/main/java/sorts/distribute/RotateMSDRadixSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RotateMSDRadixSort extends Sort { +public final class RotateMSDRadixSort extends Sort { public RotateMSDRadixSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -162,4 +162,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/ShatterSort.java b/src/main/java/sorts/distribute/ShatterSort.java index 31c131c4..f01b1e69 100644 --- a/src/main/java/sorts/distribute/ShatterSort.java +++ b/src/main/java/sorts/distribute/ShatterSort.java @@ -4,7 +4,7 @@ import sorts.templates.ShatterSorting; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ShatterSort extends ShatterSorting { +public final class ShatterSort extends ShatterSorting { public ShatterSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Shatter"); this.setRunAllSortsName("Shatter Sort"); this.setRunSortName("Shatter Sort"); @@ -44,9 +44,9 @@ public ShatterSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.shatterSort(array, sortLength, bucketCount); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/SimpleShatterSort.java b/src/main/java/sorts/distribute/SimpleShatterSort.java index b988e9c7..2bdbfa40 100644 --- a/src/main/java/sorts/distribute/SimpleShatterSort.java +++ b/src/main/java/sorts/distribute/SimpleShatterSort.java @@ -4,7 +4,7 @@ import sorts.templates.ShatterSorting; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SimpleShatterSort extends ShatterSorting { +public final class SimpleShatterSort extends ShatterSorting { public SimpleShatterSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Simple Shatter"); this.setRunAllSortsName("Simple Shatter Sort"); this.setRunSortName("Simple Shatter Sort"); @@ -44,9 +44,9 @@ public SimpleShatterSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.simpleShatterSort(array, sortLength, bucketCount, (int) (Math.log(sortLength) / Math.log(2)) / 2); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/SimplisticGravitySort.java b/src/main/java/sorts/distribute/SimplisticGravitySort.java index 735e23ac..bf3bc2ad 100644 --- a/src/main/java/sorts/distribute/SimplisticGravitySort.java +++ b/src/main/java/sorts/distribute/SimplisticGravitySort.java @@ -4,11 +4,11 @@ import main.ArrayVisualizer; -final public class SimplisticGravitySort extends Sort { - +public final class SimplisticGravitySort extends Sort { + public SimplisticGravitySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Simplistic Gravity"); this.setRunAllSortsName("Simplistic Gravity Sort (By McDude_73)"); this.setRunSortName("Simplistic Gravity Sort"); @@ -20,11 +20,11 @@ public SimplisticGravitySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + int[] aux; int min; double sleep; - + private void transferFrom(int[] array, int arrayLength, int index) { for(int pointer = 0;pointer < arrayLength && this.aux[pointer] != 0;pointer++) { Highlights.markArray(2, index); @@ -32,7 +32,7 @@ private void transferFrom(int[] array, int arrayLength, int index) { Writes.write(this.aux, pointer, --this.aux[pointer], this.sleep, true, true); } } - + private void transferTo(int[] array, int arrayLength, int index) { for(int pointer = 0;array[index] > this.min;pointer++) { Highlights.markArray(2, index); @@ -40,7 +40,7 @@ private void transferTo(int[] array, int arrayLength, int index) { Writes.write(this.aux, pointer, ++this.aux[pointer], this.sleep, true, true); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.sleep = 10d/length; @@ -63,4 +63,4 @@ public void runSort(int[] array, int length, int bucketCount) { } Writes.deleteExternalArray(this.aux); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/StacklessAmericanFlagSort.java b/src/main/java/sorts/distribute/StacklessAmericanFlagSort.java index eea6af62..2f98c40a 100644 --- a/src/main/java/sorts/distribute/StacklessAmericanFlagSort.java +++ b/src/main/java/sorts/distribute/StacklessAmericanFlagSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020-2021 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StacklessAmericanFlagSort extends Sort { +public final class StacklessAmericanFlagSort extends Sort { public StacklessAmericanFlagSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stackless American Flag"); this.setRunAllSortsName("Stackless American Flag Sort"); this.setRunSortName("Stackless American Flag Sort"); @@ -44,14 +44,14 @@ public StacklessAmericanFlagSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int stabVal(int idx) { if(arrayVisualizer.doingStabilityCheck()) return arrayVisualizer.getStabilityValue(idx); else return idx; } - + private static int shift(int n, int q, int r) { while(q > 0) { n /= r; @@ -59,7 +59,7 @@ private static int shift(int n, int q, int r) { } return n; } - + private int dist(int[] array, int[] cnts, int[] offs, int a, int b, int q, int r) { for(int i = 1; i < r; i++) { Writes.write(cnts, i, cnts[i]+cnts[i-1], 0, false, true); @@ -67,60 +67,60 @@ private int dist(int[] array, int[] cnts, int[] offs, int a, int b, int q, int r } for(int i = 0; i < r-1; i++) { int pos = a+offs[i]; - + if(cnts[i] > offs[i]) { Highlights.markArray(2, pos); int t = array[pos]; - + do { int digit = Reads.getDigit(t, q, r); Writes.write(cnts, digit, cnts[digit]-1, 0, false, true); - + int t1 = array[a+cnts[digit]]; Writes.write(array, a+cnts[digit], t, 0.5, true, false); t = t1; } while(cnts[i] > offs[i]); - + Highlights.clearMark(2); } } int p = a+offs[1]; - + for(int i = 0; i < r; i++) { Writes.write(cnts, i, 0, 0, false, true); Writes.write(offs, i, 0, 0, false, true); } return p; } - + @Override public void runSort(int[] array, int length, int bucketCount) { int r = bucketCount, q = Reads.analyzeMaxLog(array, length, r, 0.5, true), m = 0, i = 0, b = length; - + int[] cnts = new int[r], offs = new int[r]; Writes.changeAllocAmount(2 * r); - + for(int j = i; j < b; j++) { Highlights.markArray(1, j); int digit = Reads.getDigit(array[j], q, r); Writes.write(cnts, digit, cnts[digit]+1, 0.5, false, true); } - + while(i < length) { int p = b-i < 1 ? i : this.dist(array, cnts, offs, i, b, q, r); - + if(q == 0) { m += r; int t = m/r; - + while(t%r == 0) { t /= r; q++; } - + i = b; while(b < length && shift(this.stabVal(array[b]), q+1, r) == shift(m, q+1, r)) { Highlights.markArray(1, b); @@ -132,7 +132,7 @@ public void runSort(int[] array, int length, int bucketCount) { else { b = p; q--; - + for(int j = i; j < b; j++) { Highlights.markArray(1, j); int digit = Reads.getDigit(array[j], q, r); @@ -142,4 +142,4 @@ public void runSort(int[] array, int length, int bucketCount) { } Writes.changeAllocAmount(-2 * r); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/StacklessBinaryQuickSort.java b/src/main/java/sorts/distribute/StacklessBinaryQuickSort.java index e4342840..32fd2c65 100644 --- a/src/main/java/sorts/distribute/StacklessBinaryQuickSort.java +++ b/src/main/java/sorts/distribute/StacklessBinaryQuickSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020-2021 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StacklessBinaryQuickSort extends Sort { +public final class StacklessBinaryQuickSort extends Sort { public StacklessBinaryQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stackless Binary Quick"); this.setRunAllSortsName("Stackless Binary Quick Sort"); this.setRunSortName("Stackless Binary Quicksort"); @@ -44,17 +44,17 @@ public StacklessBinaryQuickSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int stabVal(int idx) { if(arrayVisualizer.doingStabilityCheck()) return arrayVisualizer.getStabilityValue(idx); else return idx; } - + private int partition(int[] array, int a, int b, int bit) { int i = a-1, j = b; - + while(true) { do { i++; @@ -62,31 +62,31 @@ private int partition(int[] array, int a, int b, int bit) { Delays.sleep(0.5); } while(i < j && !Reads.getBit(array[i], bit)); - + do { j--; Highlights.markArray(2, j); Delays.sleep(0.5); } while(j > i && Reads.getBit(array[j], bit)); - + if(i < j) Writes.swap(array, i, j, 1, true, false); else return i; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { int q = Reads.analyzeBit(array, length), m = 0, i = 0, b = length; - + while(i < length) { int p = b-i < 1 ? i : this.partition(array, i, b, q); - + if(q == 0) { m += 2; while(!Reads.getBit(m, q+1)) q++; - + i = b; Highlights.clearMark(2); arrayVisualizer.toggleAnalysis(true); @@ -103,4 +103,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/distribute/StaticSort.java b/src/main/java/sorts/distribute/StaticSort.java index 7fa17912..77548a1e 100644 --- a/src/main/java/sorts/distribute/StaticSort.java +++ b/src/main/java/sorts/distribute/StaticSort.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StaticSort extends Sort { +public final class StaticSort extends Sort { MaxHeapSort heapSorter; InsertionSort insertSorter; diff --git a/src/main/java/sorts/distribute/TimeSort.java b/src/main/java/sorts/distribute/TimeSort.java index 66cfa189..8954af7b 100644 --- a/src/main/java/sorts/distribute/TimeSort.java +++ b/src/main/java/sorts/distribute/TimeSort.java @@ -35,7 +35,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class TimeSort extends Sort { +public final class TimeSort extends Sort { private InsertionSort insertSorter; private volatile int next = 0; @@ -122,4 +122,4 @@ public void run() { Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/BinaryGnomeSort.java b/src/main/java/sorts/exchange/BinaryGnomeSort.java index 3c8ba167..a38519b2 100644 --- a/src/main/java/sorts/exchange/BinaryGnomeSort.java +++ b/src/main/java/sorts/exchange/BinaryGnomeSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BinaryGnomeSort extends Sort { +public final class BinaryGnomeSort extends Sort { public BinaryGnomeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binary Gnome"); this.setRunAllSortsName("Optimized Gnome Sort + Binary Search"); this.setRunSortName("Optimized Gnomesort + Binary Search"); @@ -27,13 +27,13 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti int lo = 0, hi = i; while (lo < hi) { int mid = lo + ((hi - lo) / 2); - + Highlights.markArray(1, lo); Highlights.markArray(3, mid); Highlights.markArray(2, hi); - + Delays.sleep(1); - + if (Reads.compareValues(num, array[mid]) < 0) { // do NOT shift equal elements past each other; this maintains stability! hi = mid; } @@ -46,12 +46,12 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Highlights.clearMark(1); Highlights.clearMark(2); - + int j = i; - while (j > lo) { + while (j > lo) { Writes.swap(array, j, j - 1, 0.05, true, false); j--; } - } + } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/BubbleSort.java b/src/main/java/sorts/exchange/BubbleSort.java index dcedf095..f9e32ab9 100644 --- a/src/main/java/sorts/exchange/BubbleSort.java +++ b/src/main/java/sorts/exchange/BubbleSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BubbleSort extends Sort { +public final class BubbleSort extends Sort { public BubbleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bubble"); this.setRunAllSortsName("Bubble Sort"); this.setRunSortName("Bubblesort"); @@ -44,7 +44,7 @@ public BubbleSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int i = length - 1; i > 0; i--) { @@ -54,7 +54,7 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.swap(array, j, j + 1, 0.075, true, false); sorted = false; } - + Highlights.markArray(1, j); Highlights.markArray(2, j + 1); Delays.sleep(0.025); @@ -62,4 +62,4 @@ public void runSort(int[] array, int length, int bucketCount) { if(sorted) break; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CircleSortIterative.java b/src/main/java/sorts/exchange/CircleSortIterative.java index 480585e7..8ff5d474 100644 --- a/src/main/java/sorts/exchange/CircleSortIterative.java +++ b/src/main/java/sorts/exchange/CircleSortIterative.java @@ -4,7 +4,7 @@ import sorts.templates.IterativeCircleSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -15,10 +15,10 @@ * */ -final public class CircleSortIterative extends IterativeCircleSorting { +public final class CircleSortIterative extends IterativeCircleSorting { public CircleSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Circle (Iterative)"); this.setRunAllSortsName("Iterative Circle Sort"); this.setRunSortName("Iterative Circlesort"); @@ -30,20 +30,20 @@ public CircleSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + public void singleRoutine(int[] array, int length) { this.circleSortRoutine(array, length, 0.1); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.end = sortLength; int n = 1; for(; n < sortLength; n*=2); - + int numberOfSwaps = 0; do { numberOfSwaps = this.circleSortRoutine(array, n, 1); } while (numberOfSwaps != 0); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CircleSortRecursive.java b/src/main/java/sorts/exchange/CircleSortRecursive.java index 4559cb48..9a42f42a 100644 --- a/src/main/java/sorts/exchange/CircleSortRecursive.java +++ b/src/main/java/sorts/exchange/CircleSortRecursive.java @@ -4,7 +4,7 @@ import sorts.templates.CircleSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -15,10 +15,10 @@ * */ -final public class CircleSortRecursive extends CircleSorting { +public final class CircleSortRecursive extends CircleSorting { public CircleSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Circle (Recursive)"); this.setRunAllSortsName("Recursive Circle Sort"); this.setRunSortName("Circlesort"); @@ -30,20 +30,20 @@ public CircleSortRecursive(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + public void singleRoutine(int[] array, int length) { this.circleSortRoutine(array, 0, length - 1, 0, 0.1); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.end = sortLength; int n = 1; for(; n < sortLength; n*=2); - + int numberOfSwaps = 0; do { numberOfSwaps = this.circleSortRoutine(array, 0, n - 1, 0, 1); } while (numberOfSwaps != 0); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CircloidSort.java b/src/main/java/sorts/exchange/CircloidSort.java index 83f8bc7c..01808799 100644 --- a/src/main/java/sorts/exchange/CircloidSort.java +++ b/src/main/java/sorts/exchange/CircloidSort.java @@ -5,7 +5,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 yuji @@ -30,10 +30,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CircloidSort extends Sort { +public final class CircloidSort extends Sort { public CircloidSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Circloid"); this.setRunAllSortsName("Circloid Sort"); this.setRunSortName("Circloid Sort"); @@ -45,7 +45,7 @@ public CircloidSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private boolean circle(int[] array, int left, int right) { int a = left; int b = right; @@ -63,7 +63,7 @@ private boolean circle(int[] array, int left, int right) { } return swapped; } - + private boolean circlePass(int[] array, int left, int right) { if(left >= right) return false; int mid = (left + right) / 2; @@ -71,9 +71,9 @@ private boolean circlePass(int[] array, int left, int right) { boolean r = this.circlePass(array, mid+1, right); return this.circle(array, left, right) || l || r; } - + @Override public void runSort(int[] array, int length, int bucketCount) { while(this.circlePass(array, 0, length-1)); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/ClassicThreeSmoothCombSort.java b/src/main/java/sorts/exchange/ClassicThreeSmoothCombSort.java index a6f6717f..e44a6b3e 100644 --- a/src/main/java/sorts/exchange/ClassicThreeSmoothCombSort.java +++ b/src/main/java/sorts/exchange/ClassicThreeSmoothCombSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 PiotrGrochowski Copyright (c) 2020 aphitorite @@ -26,10 +26,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ClassicThreeSmoothCombSort extends Sort { +public final class ClassicThreeSmoothCombSort extends Sort { public ClassicThreeSmoothCombSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Classic 3-Smooth Comb"); this.setRunAllSortsName("Classic 3-Smooth Comb Sort"); this.setRunSortName("Classic 3-Smooth Combsort"); @@ -41,15 +41,15 @@ public ClassicThreeSmoothCombSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private boolean is3Smooth(int n) { while(n%6 == 0) n /= 6; while(n%3 == 0) n /= 3; while(n%2 == 0) n /= 2; - + return n == 1; } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int g = length-1; g > 0; g--) @@ -58,4 +58,4 @@ public void runSort(int[] array, int length, int bucketCount) { if(Reads.compareIndices(array, i-g, i, 0.5, true) == 1) Writes.swap(array, i-g, i, 0.5, true, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CocktailShakerSort.java b/src/main/java/sorts/exchange/CocktailShakerSort.java index d0800293..ad3ac624 100644 --- a/src/main/java/sorts/exchange/CocktailShakerSort.java +++ b/src/main/java/sorts/exchange/CocktailShakerSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CocktailShakerSort extends Sort { +public final class CocktailShakerSort extends Sort { public CocktailShakerSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Cocktail"); this.setRunAllSortsName("Cocktail Shaker Sort"); this.setRunSortName("Cocktail Shaker Sort"); @@ -54,10 +54,10 @@ private void smartCocktailShaker(int[] array, int start, int end, double sleep) Writes.swap(array, j, j + 1, sleep, true, false); sorted = false; } - + Highlights.markArray(1, j); Highlights.markArray(2, j + 1); - + Delays.sleep(sleep / 2); } for(int j = end + start - i - 1; j > i; j--){ @@ -65,23 +65,23 @@ private void smartCocktailShaker(int[] array, int start, int end, double sleep) Writes.swap(array, j, j - 1, sleep, true, false); sorted = false; } - + Highlights.markArray(1, j); Highlights.markArray(2, j - 1); - + Delays.sleep(sleep / 2); } if(sorted) break; else i++; } } - + public void customSort(int[] array, int start, int end) { this.smartCocktailShaker(array, start, end, 1); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.smartCocktailShaker(array, 0, length, 0.1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CombSort.java b/src/main/java/sorts/exchange/CombSort.java index 85fe9dd1..bdd37ad1 100644 --- a/src/main/java/sorts/exchange/CombSort.java +++ b/src/main/java/sorts/exchange/CombSort.java @@ -4,7 +4,7 @@ import sorts.templates.CombSorting; /* - * + * The MIT License (MIT) Copyright (c) 2012 Daniel Imms, http://www.growingwiththeweb.com @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class CombSort extends CombSorting { +public final class CombSort extends CombSorting { public CombSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Comb"); this.setRunAllSortsName("Comb Sort"); this.setRunSortName("Combsort"); @@ -55,4 +55,4 @@ public int validateAnswer(int answer) { public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.combSort(array, sortLength, bucketCount/100d, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/CompleteGraphSort.java b/src/main/java/sorts/exchange/CompleteGraphSort.java index 5a791e20..120efe0e 100644 --- a/src/main/java/sorts/exchange/CompleteGraphSort.java +++ b/src/main/java/sorts/exchange/CompleteGraphSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2021 aphitorite @@ -27,11 +27,11 @@ this software and associated documentation files (the "Software"), to deal in CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ - -final public class CompleteGraphSort extends Sort { + +public final class CompleteGraphSort extends Sort { public CompleteGraphSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Complete Graph"); this.setRunAllSortsName("Complete Graph Sorting Network"); this.setRunSortName("Complete Graph Sorting Network"); @@ -43,36 +43,36 @@ public CompleteGraphSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void compSwap(int[] array, int a, int b) { if(Reads.compareIndices(array, a, b, 0.0125, true) > 0) Writes.swap(array, a, b, 0.0875, true, false); } - + private void split(int[] array, int a, int m, int b) { if(b-a < 2) return; - + int c = 0, len1 = (b-a)/2; boolean odd = (b-a)%2 == 1; - + if(odd) { if(m-a > b-m) c = a++; else c = --b; } for(int s = 0; s < len1; s++) { int i = a; - + for(int j = s; j < len1; j++) this.compSwap(array, i++, m+j); - + for(int j = 0; j < s; j++) this.compSwap(array, i++, m+j); } if(odd) { - if(c < m) + if(c < m) for(int j = 0; j < len1; j++) this.compSwap(array, c, m+j); - else + else for(int j = 0; j < len1; j++) this.compSwap(array, a+j, c); } @@ -82,21 +82,21 @@ private void split(int[] array, int a, int m, int b) { public void runSort(int[] array, int currentLength, int bucketCount) { int n = currentLength; int d = 2, end = 1 << (int)(Math.log(n-1)/Math.log(2) + 1); - + while(d <= end) { int i = 0, dec = 0; - + while(i < n) { int j = i; dec += n; - + while(dec >= d) { dec -= d; j++; } int k = j; dec += n; - + while(dec >= d) { dec -= d; k++; @@ -107,4 +107,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { d *= 2; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/DualPivotQuickSort.java b/src/main/java/sorts/exchange/DualPivotQuickSort.java index 530fd40d..9c8bb4de 100644 --- a/src/main/java/sorts/exchange/DualPivotQuickSort.java +++ b/src/main/java/sorts/exchange/DualPivotQuickSort.java @@ -4,7 +4,7 @@ import sorts.insert.InsertionSort; import sorts.templates.Sort; -final public class DualPivotQuickSort extends Sort { +public final class DualPivotQuickSort extends Sort { private InsertionSort insertSorter; public DualPivotQuickSort(ArrayVisualizer arrayVisualizer) { @@ -112,4 +112,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti this.insertSorter = new InsertionSort(this.arrayVisualizer); this.dualPivot(array, 0, sortLength - 1, 3); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/ForcedStableQuickSort.java b/src/main/java/sorts/exchange/ForcedStableQuickSort.java index 87a83b9f..3835c43a 100644 --- a/src/main/java/sorts/exchange/ForcedStableQuickSort.java +++ b/src/main/java/sorts/exchange/ForcedStableQuickSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ForcedStableQuickSort extends Sort { +public final class ForcedStableQuickSort extends Sort { public ForcedStableQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -113,4 +113,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.deleteExternalArray(key); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/FunSort.java b/src/main/java/sorts/exchange/FunSort.java index d5a4f054..a52c9024 100644 --- a/src/main/java/sorts/exchange/FunSort.java +++ b/src/main/java/sorts/exchange/FunSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 fungamer2 Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,11 +25,11 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class FunSort extends Sort { - +public final class FunSort extends Sort { + public FunSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Fun"); this.setRunAllSortsName("Fun Sort"); this.setRunSortName("Fun Sort"); @@ -41,7 +41,7 @@ public FunSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + /* Fun Sort - or the chaos of unordered binary search https://www.sciencedirect.com/science/article/pii/S0166218X04001131 @@ -67,7 +67,7 @@ public int binarySearch(int[] array, int start, int end, int value) { Highlights.clearMark(3); return start; } - + @Override public void runSort(int[] array, int length, int bucketCount) { for (int i = 1; i < length; i++) { @@ -86,4 +86,4 @@ public void runSort(int[] array, int length, int bucketCount) { } while (!done); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/GnomeSort.java b/src/main/java/sorts/exchange/GnomeSort.java index 2b909fac..d0417107 100644 --- a/src/main/java/sorts/exchange/GnomeSort.java +++ b/src/main/java/sorts/exchange/GnomeSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class GnomeSort extends Sort { +public final class GnomeSort extends Sort { public GnomeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Gnome"); this.setRunAllSortsName("Gnome Sort"); this.setRunSortName("Gnomesort"); @@ -18,9 +18,9 @@ public GnomeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // Code retrieved from http://www.algostructure.com/sorting/gnomesort.php - + @Override public void runSort(int[] array, int length, int bucketCount) { for (int i = 1; i < length;) @@ -32,11 +32,11 @@ public void runSort(int[] array, int length, int bucketCount) { else { Writes.swap(array, i, i - 1, 0.02, true, false); - + if (i > 1) { i--; } } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/LLQuickSort.java b/src/main/java/sorts/exchange/LLQuickSort.java index cc6e4885..96ab24b6 100644 --- a/src/main/java/sorts/exchange/LLQuickSort.java +++ b/src/main/java/sorts/exchange/LLQuickSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class LLQuickSort extends Sort { +public final class LLQuickSort extends Sort { public LLQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Left/Left Quick"); this.setRunAllSortsName("Quick Sort, Left/Left Pointers"); this.setRunSortName("Left/Left Quicksort"); @@ -18,11 +18,11 @@ public LLQuickSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int partition(int[] array, int lo, int hi) { int pivot = array[hi]; int i = lo; - + for(int j = lo; j < hi; j++) { Highlights.markArray(1, j); if(Reads.compareValues(array[j], pivot) < 0) { @@ -34,7 +34,7 @@ private int partition(int[] array, int lo, int hi) { Writes.swap(array, i, hi, 1, true, false); return i; } - + private void quickSort(int[] array, int lo, int hi) { if(lo < hi) { int p = this.partition(array, lo, hi); @@ -42,9 +42,9 @@ private void quickSort(int[] array, int lo, int hi) { this.quickSort(array, p + 1, hi); } } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.quickSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/LRQuickSort.java b/src/main/java/sorts/exchange/LRQuickSort.java index bbd48039..b90f8711 100644 --- a/src/main/java/sorts/exchange/LRQuickSort.java +++ b/src/main/java/sorts/exchange/LRQuickSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class LRQuickSort extends Sort { +public final class LRQuickSort extends Sort { public LRQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Left/Right Quick"); this.setRunAllSortsName("Quick Sort, Left/Right Pointers"); this.setRunSortName("Left/Right Quicksort"); @@ -20,15 +20,15 @@ public LRQuickSort(ArrayVisualizer arrayVisualizer) { } // Thanks to Timo Bingmann for providing a good reference for Quick Sort w/ LR pointers. - private void quickSort(int[] a, int p, int r) { + private void quickSort(int[] a, int p, int r) { int pivot = p + (r - p + 1) / 2; int x = a[pivot]; - + int i = p; int j = r; Highlights.markArray(3, pivot); - + while (i <= j) { while (Reads.compareValues(a[i], x) == -1){ i++; @@ -49,14 +49,14 @@ private void quickSort(int[] a, int p, int r) { if(j == pivot) { Highlights.markArray(3, i); } - + Writes.swap(a, i, j, 1, true, false); - + i++; j--; } } - + if(p < j) { this.quickSort(a, p, j); } @@ -69,4 +69,4 @@ private void quickSort(int[] a, int p, int r) { public void runSort(int[] array, int currentLength, int bucketCount) { this.quickSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/LRQuickSortParallel.java b/src/main/java/sorts/exchange/LRQuickSortParallel.java index 47f9ce42..7b5adbe6 100644 --- a/src/main/java/sorts/exchange/LRQuickSortParallel.java +++ b/src/main/java/sorts/exchange/LRQuickSortParallel.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class LRQuickSortParallel extends Sort { +public final class LRQuickSortParallel extends Sort { public LRQuickSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Left/Right Quick (Parallel)"); this.setRunAllSortsName("Parallel Quick Sort, Left/Right Pointers"); this.setRunSortName("Parallel Left/Right Quicksort"); @@ -18,9 +18,9 @@ public LRQuickSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] a; - + private class QuickSort extends Thread { private int p, r; QuickSort(int p, int r) { @@ -33,16 +33,16 @@ public void run() { } // Thanks to Timo Bingmann for providing a good reference for Quick Sort w/ LR pointers. - private void quickSort(int p, int r) { + private void quickSort(int p, int r) { if(p < r) { int pivot = p + (r - p + 1) / 2; int x = this.a[pivot]; - + int i = p; int j = r; Highlights.markArray(3, pivot); - + while (i <= j) { while (Reads.compareValues(this.a[i], x) == -1){ i++; @@ -63,19 +63,19 @@ private void quickSort(int p, int r) { if(j == pivot) { Highlights.markArray(3, i); } - + Writes.swap(this.a, i, j, 1, true, false); - + i++; j--; } } - + QuickSort left = new QuickSort(p, j); QuickSort right = new QuickSort(i, r); left.start(); right.start(); - + try { left.join(); right.join(); @@ -90,4 +90,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { this.a = array; this.quickSort(0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OddEvenSort.java b/src/main/java/sorts/exchange/OddEvenSort.java index b61aaf4b..ae6c0cb3 100644 --- a/src/main/java/sorts/exchange/OddEvenSort.java +++ b/src/main/java/sorts/exchange/OddEvenSort.java @@ -8,10 +8,10 @@ * https://www.geeksforgeeks.org/odd-even-sort-brick-sort/ */ -final public class OddEvenSort extends Sort { +public final class OddEvenSort extends Sort { public OddEvenSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Odd-Even"); this.setRunAllSortsName("Odd-Even Sort"); this.setRunSortName("Odd-Even Sort"); @@ -27,29 +27,29 @@ public OddEvenSort(ArrayVisualizer arrayVisualizer) { @Override public void runSort(int[] array, int length, int bucketCount) { boolean sorted = false; - + while (!sorted) { sorted = true; - + for (int i = 1; i < length - 1; i += 2) { if(Reads.compareValues(array[i], array[i + 1]) == 1) { Writes.swap(array, i, i + 1, 0.075, true, false); sorted = false; } - + Highlights.markArray(1, i); Delays.sleep(0.025); } - + for (int i = 0; i < length - 1; i += 2) { if(Reads.compareValues(array[i], array[i + 1]) == 1) { Writes.swap(array, i, i + 1, 0.075, true, false); sorted = false; } - + Highlights.markArray(2, i); Delays.sleep(0.025); } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OptimizedBubbleSort.java b/src/main/java/sorts/exchange/OptimizedBubbleSort.java index 65fdda28..c40e36fd 100644 --- a/src/main/java/sorts/exchange/OptimizedBubbleSort.java +++ b/src/main/java/sorts/exchange/OptimizedBubbleSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class OptimizedBubbleSort extends Sort { +public final class OptimizedBubbleSort extends Sort { public OptimizedBubbleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Bubble"); this.setRunAllSortsName("Optimized Bubble Sort"); this.setRunSortName("Optimized Bubblesort"); @@ -18,7 +18,7 @@ public OptimizedBubbleSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int consecSorted; @@ -32,4 +32,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OptimizedCocktailShakerSort.java b/src/main/java/sorts/exchange/OptimizedCocktailShakerSort.java index d85b4090..50f11dd5 100644 --- a/src/main/java/sorts/exchange/OptimizedCocktailShakerSort.java +++ b/src/main/java/sorts/exchange/OptimizedCocktailShakerSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class OptimizedCocktailShakerSort extends Sort { +public final class OptimizedCocktailShakerSort extends Sort { public OptimizedCocktailShakerSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Cocktail"); this.setRunAllSortsName("Optimized Cocktail Shaker Sort"); this.setRunSortName("Optimized Cocktailsort"); @@ -18,7 +18,7 @@ public OptimizedCocktailShakerSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int start = 0, end = length - 1; start < end; ) { @@ -30,7 +30,7 @@ public void runSort(int[] array, int length, int bucketCount) { } else consecSorted++; } end -= consecSorted; - + consecSorted = 1; for(int i = end; i > start; i--) { if(Reads.compareIndices(array, i - 1, i, 0.025, true) > 0){ @@ -41,4 +41,4 @@ public void runSort(int[] array, int length, int bucketCount) { start += consecSorted; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OptimizedGnomeSort.java b/src/main/java/sorts/exchange/OptimizedGnomeSort.java index 2e5e3b84..0e934b81 100644 --- a/src/main/java/sorts/exchange/OptimizedGnomeSort.java +++ b/src/main/java/sorts/exchange/OptimizedGnomeSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class OptimizedGnomeSort extends Sort { +public final class OptimizedGnomeSort extends Sort { public OptimizedGnomeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Gnome"); this.setRunAllSortsName("Optimized Gnome Sort"); this.setRunSortName("Optimized Gnomesort"); @@ -18,11 +18,11 @@ public OptimizedGnomeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // Taken from https://en.wikipedia.org/wiki/Gnome_sort private void smartGnomeSort(int[] array, int lowerBound, int upperBound, double sleep) { int pos = upperBound; - + while(pos > lowerBound && Reads.compareValues(array[pos - 1], array[pos]) == 1) { Writes.swap(array, pos - 1, pos, sleep, true, false); pos--; @@ -34,11 +34,11 @@ public void customSort(int[] array, int low, int high, double sleep) { smartGnomeSort(array, low, i, sleep); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int i = 1; i < length; i++) { smartGnomeSort(array, 0, i, 0.05); - } + } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OptimizedStoogeSort.java b/src/main/java/sorts/exchange/OptimizedStoogeSort.java index 734b63d9..cd67f897 100644 --- a/src/main/java/sorts/exchange/OptimizedStoogeSort.java +++ b/src/main/java/sorts/exchange/OptimizedStoogeSort.java @@ -5,10 +5,10 @@ // Code refactored from: https://www.ijitee.org/wp-content/uploads/papers/v8i12/L31671081219.pdf // Written by Professors Amit Kishor and Pankaj Pratap Singh -final public class OptimizedStoogeSort extends Sort { +public final class OptimizedStoogeSort extends Sort { public OptimizedStoogeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Stooge"); this.setRunAllSortsName("Optimized Stooge Sort"); this.setRunSortName("Optimized Stoogesort"); @@ -20,67 +20,67 @@ public OptimizedStoogeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void forward(int[] array, int left, int right) { while(left < right) { int index = right; - + while(left < index) { Highlights.markArray(1, left); Highlights.markArray(2, index); Delays.sleep(0.05); - + if(Reads.compareValues(array[left], array[index]) > 0) { Writes.swap(array, left, index, 0.075, true, false); } left++; index--; } - + left = 0; right--; } } - + private void backward(int[] array, int left, int right) { int length = right; - + while(left < right) { int index = left; - + while(index < right) { Highlights.markArray(1, index); Highlights.markArray(2, right); Delays.sleep(0.05); - + if(Reads.compareValues(array[index], array[right]) > 0) { Writes.swap(array, index, right, 0.075, true, false); } index++; right--; } - + left++; right = length; } } - + private void exchange(int[] array, int length) { int left = 0; int right = length - 1; - + while(left < right) { Highlights.markArray(1, left); Highlights.markArray(2, right); Delays.sleep(0.05); - + if(Reads.compareValues(array[left], array[right]) > 0) { Writes.swap(array, left, right, 0.075, true, false); } left++; right--; } - + this.forward(array, 0, length - 2); this.backward(array, 1, length - 1); } @@ -89,4 +89,4 @@ private void exchange(int[] array, int length) { public void runSort(int[] array, int sortLength, int bucketCount) { this.exchange(array, sortLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/OptimizedStoogeSortStudio.java b/src/main/java/sorts/exchange/OptimizedStoogeSortStudio.java index 1b56484c..5df5f52a 100644 --- a/src/main/java/sorts/exchange/OptimizedStoogeSortStudio.java +++ b/src/main/java/sorts/exchange/OptimizedStoogeSortStudio.java @@ -14,10 +14,10 @@ * @author EilrahcF - Key ideas / concepts */ -final public class OptimizedStoogeSortStudio extends Sort { +public final class OptimizedStoogeSortStudio extends Sort { public OptimizedStoogeSortStudio(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Stooge (The Studio version)"); this.setRunAllSortsName("Optimized Stooge Sort"); this.setRunSortName("Optistooge Sort"); @@ -29,7 +29,7 @@ public OptimizedStoogeSortStudio(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private boolean compSwap(int[] array, int a, int b) { if(Reads.compareIndices(array, a, b, 0.025, true) == 1) { Writes.swap(array, a, b, 0.075, false, false); @@ -37,22 +37,22 @@ private boolean compSwap(int[] array, int a, int b) { } return false; } - + private boolean stoogeSort(int[] array, int a, int m, int b, boolean merge) { if(a >= m) return false; if(b-a == 2) return this.compSwap(array, a, m); - + boolean lChange = false; boolean rChange = false; - + int a2 = (a+a+b)/3; int b2 = (a+b+b+2)/3; - + if(m < b2) { lChange = this.stoogeSort(array, a, m, b2, merge); - + if(merge) { rChange = this.stoogeSort(array, Math.max(a+b2-m, a2), b2, b, true); if(rChange) this.stoogeSort(array, a+b2-m, a2, 2*a2-a, true); @@ -73,4 +73,4 @@ private boolean stoogeSort(int[] array, int a, int m, int b, boolean merge) { public void runSort(int[] array, int currentLength, int bucketCount) { this.stoogeSort(array, 0, 1, currentLength, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/SillySort.java b/src/main/java/sorts/exchange/SillySort.java index c85bde29..24df82da 100644 --- a/src/main/java/sorts/exchange/SillySort.java +++ b/src/main/java/sorts/exchange/SillySort.java @@ -6,10 +6,10 @@ // Written by Tom Duff, and found here: http://home.tiac.net/~cri_d/cri/2001/badsort.html // from https://stackoverflow.com/questions/2609857/are-there-any-worse-sorting-algorithms-than-bogosort-a-k-a-monkey-sort/ -final public class SillySort extends Sort { +public final class SillySort extends Sort { public SillySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Silly"); this.setRunAllSortsName("Silly Sort"); this.setRunSortName("Sillysort"); @@ -21,32 +21,32 @@ public SillySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(150); this.setBogoSort(false); } - + private void sillySort(int[] array, int i, int j) { int m; - + if (i < j) { /* find the middle of the array */ m = i + ((j - i) / 2); - - /* - * use this function (recursively) to find put the minimum elements of + + /* + * use this function (recursively) to find put the minimum elements of * each half into the first elements of each half */ this.sillySort(array, i, m); this.sillySort(array, m + 1, j); - - /* + + /* * Choose the smallest element of the two halves, and put that element in * the first position */ - if (Reads.compareValues(array[i], array[m + 1]) >= 0) { + if (Reads.compareValues(array[i], array[m + 1]) >= 0) { Writes.swap(array, i, m + 1, 1, true, false); } - + Highlights.markArray(1, i); Highlights.markArray(2, m + 1); - + this.sillySort(array, i + 1, j); } } @@ -55,4 +55,4 @@ private void sillySort(int[] array, int i, int j) { public void runSort(int[] array, int currentLength, int bucketCount) { this.sillySort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/SlowSort.java b/src/main/java/sorts/exchange/SlowSort.java index 5ed71bb1..ec09f6fd 100644 --- a/src/main/java/sorts/exchange/SlowSort.java +++ b/src/main/java/sorts/exchange/SlowSort.java @@ -5,10 +5,10 @@ // Code refactored from Python: http://wiki.c2.com/?SlowSort -final public class SlowSort extends Sort { +public final class SlowSort extends Sort { public SlowSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Slow"); this.setRunAllSortsName("Slow Sort"); this.setRunSortName("Slowsort"); @@ -20,24 +20,24 @@ public SlowSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(150); this.setBogoSort(false); } - - private void slowSort(int[] A, int i, int j) { + + private void slowSort(int[] A, int i, int j) { if (i >= j) { return; } - + int m = i + ((j - i) / 2); - + this.slowSort(A, i, m); this.slowSort(A, m + 1, j); - + if (Reads.compareValues(A[m], A[j]) == 1) { Writes.swap(A, m, j, 1, true, false); } - + Highlights.markArray(1, j); Highlights.markArray(2, m); - + this.slowSort(A, i, j - 1); } @@ -45,4 +45,4 @@ private void slowSort(int[] A, int i, int j) { public void runSort(int[] array, int currentLength, int bucketCount) { this.slowSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/SnuffleSort.java b/src/main/java/sorts/exchange/SnuffleSort.java index fdb6e3aa..f67d6bbc 100644 --- a/src/main/java/sorts/exchange/SnuffleSort.java +++ b/src/main/java/sorts/exchange/SnuffleSort.java @@ -42,4 +42,4 @@ private void snuffleSort(int[] arr, int start, int stop) { public void runSort(int[] array, int length, int buckets) { this.snuffleSort(array, 0, length - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/StablePermutationSort.java b/src/main/java/sorts/exchange/StablePermutationSort.java index f641f70a..39605da8 100644 --- a/src/main/java/sorts/exchange/StablePermutationSort.java +++ b/src/main/java/sorts/exchange/StablePermutationSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StablePermutationSort extends BogoSorting { +public final class StablePermutationSort extends BogoSorting { public StablePermutationSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -84,4 +84,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.permute(array, idx, length); Writes.deleteExternalArray(idx); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/StableQuickSort.java b/src/main/java/sorts/exchange/StableQuickSort.java index 32d8af80..e86a33ac 100644 --- a/src/main/java/sorts/exchange/StableQuickSort.java +++ b/src/main/java/sorts/exchange/StableQuickSort.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StableQuickSort extends Sort { +public final class StableQuickSort extends Sort { private int length; public StableQuickSort(ArrayVisualizer arrayVisualizer) { @@ -110,4 +110,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.length = length; this.stableQuickSort(array, 0, length - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/StableQuickSortParallel.java b/src/main/java/sorts/exchange/StableQuickSortParallel.java index 716906e0..fa823dfd 100644 --- a/src/main/java/sorts/exchange/StableQuickSortParallel.java +++ b/src/main/java/sorts/exchange/StableQuickSortParallel.java @@ -6,7 +6,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2021 aphitorite @@ -30,10 +30,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class StableQuickSortParallel extends Sort { +public final class StableQuickSortParallel extends Sort { public StableQuickSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stable Quick (Parallel)"); this.setRunAllSortsName("Parallel Stable Quick Sort"); this.setRunSortName("Parallel Stable Quicksort"); @@ -45,10 +45,10 @@ public StableQuickSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; private int[] tmp; - + private class QuickSortInt extends Thread { private int a, b; QuickSortInt(int a, int b) { @@ -69,21 +69,21 @@ public void run() { StableQuickSortParallel.this.quickSortExt(a, b); } } - + private int partitionInt(int a, int b) { Random r = new Random(); int p = a + r.nextInt(b-a); - + int piv = array[p]; int j = a, k = b-1; - + while(j < p && Reads.compareValues(array[j], piv) <= 0) j++; if(j < p) Writes.write(tmp, k--, array[j], 1, false, true); - + for(int i = j+1; i < p; i++) { if(Reads.compareValues(array[i], piv) <= 0) Writes.write(array, j++, array[i], 1, true, false); - + else { Highlights.markArray(2, k); Writes.write(tmp, k--, array[i], 1, false, true); @@ -92,26 +92,26 @@ private int partitionInt(int a, int b) { for(int i = p+1; i < b; i++) { if(Reads.compareValues(array[i], piv) < 0) Writes.write(array, j++, array[i], 1, true, false); - + else { Highlights.markArray(2, k); Writes.write(tmp, k--, array[i], 1, false, true); } } Writes.write(array, j, piv, 1, true, false); - + return j; } private int partitionExt(int a, int b) { Random r = new Random(); int p = a + r.nextInt(b-a); - + int piv = tmp[p]; int j = b-1, k = a; - + while(j > p && Reads.compareValues(tmp[j], piv) > 0) j--; if(j > p) Writes.write(array, k++, tmp[j], 1, true, false); - + for(int i = j-1; i > p; i--) { if(Reads.compareValues(tmp[i], piv) > 0) { Highlights.markArray(2, j); @@ -127,22 +127,22 @@ private int partitionExt(int a, int b) { else Writes.write(array, k++, tmp[i], 1, true, false); } Writes.write(array, k, piv, 1, true, false); - + return k; } - + private void quickSortInt(int a, int b) { int len = b-a; - + if(len < 2) return; - + int p = this.partitionInt(a, b); - + QuickSortInt left = new QuickSortInt(a, p); QuickSortExt right = new QuickSortExt(p+1, b); left.start(); right.start(); - + try { left.join(); right.join(); @@ -152,19 +152,19 @@ private void quickSortInt(int a, int b) { } private void quickSortExt(int a, int b) { int len = b-a; - + if(len < 2) { if(len == 1) Writes.write(array, a, tmp[a], 1, true, false); return; } - + int p = this.partitionExt(a, b); - + QuickSortInt left = new QuickSortInt(a, p); QuickSortExt right = new QuickSortExt(p+1, b); left.start(); right.start(); - + try { left.join(); right.join(); @@ -172,7 +172,7 @@ private void quickSortExt(int a, int b) { Thread.currentThread().interrupt(); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; @@ -180,4 +180,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.quickSortInt(0, length); Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/StoogeSort.java b/src/main/java/sorts/exchange/StoogeSort.java index 6e4edffd..b47037ab 100644 --- a/src/main/java/sorts/exchange/StoogeSort.java +++ b/src/main/java/sorts/exchange/StoogeSort.java @@ -7,17 +7,17 @@ * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS * LICENSE OR COPYRIGHT LAW IS PROHIBITED. - * + * * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. * TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN * CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. */ // Code refactored from: https://en.wikipedia.org/wiki/Stooge_sort -final public class StoogeSort extends Sort { +public final class StoogeSort extends Sort { public StoogeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stooge"); this.setRunAllSortsName("Stooge Sort"); this.setRunSortName("Stoogesort"); @@ -29,15 +29,15 @@ public StoogeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(1024); this.setBogoSort(false); } - + private void stoogeSort(int[] A, int i, int j) { if (Reads.compareIndices(A, i, j, 0.0025, true) == 1) { Writes.swap(A, i, j, 0.005, true, false); } - + if (j - i + 1 >= 3) { int t = (j - i + 1) / 3; - + this.stoogeSort(A, i, j-t); this.stoogeSort(A, i+t, j); this.stoogeSort(A, i, j-t); @@ -48,4 +48,4 @@ private void stoogeSort(int[] A, int i, int j) { public void runSort(int[] array, int currentLength, int bucketCount) { this.stoogeSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/SwaplessBubbleSort.java b/src/main/java/sorts/exchange/SwaplessBubbleSort.java index 5ebd7e31..1ce9facb 100644 --- a/src/main/java/sorts/exchange/SwaplessBubbleSort.java +++ b/src/main/java/sorts/exchange/SwaplessBubbleSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 Josiah (Gaming32) Glosson @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SwaplessBubbleSort extends Sort { +public final class SwaplessBubbleSort extends Sort { public SwaplessBubbleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Swapless Bubble"); this.setRunAllSortsName("Swapless Bubble Sort"); this.setRunSortName("Swapless Bubblesort"); @@ -44,7 +44,7 @@ public SwaplessBubbleSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int last; @@ -70,4 +70,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.write(array, i - 1, comp, 0.075, true, false); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/TableSort.java b/src/main/java/sorts/exchange/TableSort.java index a97fb3d5..a930d487 100644 --- a/src/main/java/sorts/exchange/TableSort.java +++ b/src/main/java/sorts/exchange/TableSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class TableSort extends Sort { +public final class TableSort extends Sort { public TableSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,90 +44,90 @@ public TableSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private boolean stableComp(int[] array, int[] table, int a, int b) { int comp = Reads.compareIndices(array, table[a], table[b], 0.5, true); - + return comp > 0 || (comp == 0 && Reads.compareOriginalIndices(table, a, b, 0.5, false) > 0); } - + private void medianOfThree(int[] array, int[] table, int a, int b) { int m = a+(b-1-a)/2; - + if(this.stableComp(array, table, a, m)) Writes.swap(table, a, m, 1, true, true); - + if(this.stableComp(array, table, m, b-1)) { Writes.swap(table, m, b-1, 1, true, true); - + if(this.stableComp(array, table, a, m)) return; } - + Writes.swap(table, a, m, 1, true, true); } - + private int partition(int[] array, int[] table, int a, int b, int p) { int i = a-1, j = b; Highlights.markArray(3, p); - + while(true) { do i++; while(i < j && !this.stableComp(array, table, i, p)); - + do j--; while(j >= i && this.stableComp(array, table, j, p)); - + if(i < j) Writes.swap(table, i, j, 1, true, true); else return j; } } - + private void quickSort(int[] array, int[] table, int a, int b) { if(b-a < 3) { if(b-a == 2 && this.stableComp(array, table, a, a+1)) Writes.swap(table, a, a+1, 1, true, true); return; } - + this.medianOfThree(array, table, a, b); int p = this.partition(array, table, a+1, b, a); Writes.swap(table, a, p, 1, true, true); - + this.quickSort(array, table, a, p); this.quickSort(array, table, p+1, b); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { int[] table = Writes.createExternalArray(currentLength); for(int i = 0; i < currentLength; i++) Writes.write(table, i, i, 0.5, true, true); - + this.quickSort(array, table, 0, currentLength); Highlights.clearMark(3); - + for(int i = 0; i < table.length; i++) { Highlights.markArray(2, i); - + if(Reads.compareOriginalValues(i, table[i]) != 0) { int t = array[i]; int j = i, next = table[i]; - + do { Writes.write(array, j, array[next], 1, true, false); Writes.write(table, j, j, 1, true, true); - + j = next; next = table[next]; } while(Reads.compareOriginalValues(next, i) != 0); - + Writes.write(array, j, t, 1, true, false); Writes.write(table, j, j, 1, true, true); } } - + Writes.deleteExternalArray(table); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/ThreeSmoothCombSortIterative.java b/src/main/java/sorts/exchange/ThreeSmoothCombSortIterative.java index ae22e23c..43499989 100644 --- a/src/main/java/sorts/exchange/ThreeSmoothCombSortIterative.java +++ b/src/main/java/sorts/exchange/ThreeSmoothCombSortIterative.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2020 aphitorite @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class ThreeSmoothCombSortIterative extends Sort { +public final class ThreeSmoothCombSortIterative extends Sort { public ThreeSmoothCombSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("3-Smooth Comb (Iterative)"); this.setRunAllSortsName("Iterative 3-Smooth Comb Sort"); this.setRunSortName("Iterative 3-Smooth Combsort"); @@ -43,7 +43,7 @@ public ThreeSmoothCombSortIterative(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void compSwap(int[] array, int a, int b) { if(Reads.compareIndices(array, a, b, 0.5, true) == 1) Writes.swap(array, a, b, 0.5, true, false); @@ -52,16 +52,16 @@ private void compSwap(int[] array, int a, int b) { @Override public void runSort(int[] array, int length, int bucketCount) { int pow2 = (int)(Math.log(length-1)/Math.log(2)); - + for(int k = pow2; k >= 0; k--) { int pow3 = (int)((Math.log(length) - k*Math.log(2))/Math.log(3)); - + for(int j = pow3; j >= 0; j--) { int gap = (int)(Math.pow(2, k)*Math.pow(3, j)); - + for(int i = 0; i+gap < length; i++) this.compSwap(array, i, i+gap); } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/ThreeSmoothCombSortParallel.java b/src/main/java/sorts/exchange/ThreeSmoothCombSortParallel.java index e3c2a116..b3601f76 100644 --- a/src/main/java/sorts/exchange/ThreeSmoothCombSortParallel.java +++ b/src/main/java/sorts/exchange/ThreeSmoothCombSortParallel.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 PiotrGrochowski Copyright (c) 2020 aphitorite @@ -26,10 +26,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ThreeSmoothCombSortParallel extends Sort { +public final class ThreeSmoothCombSortParallel extends Sort { public ThreeSmoothCombSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("3-Smooth Comb (Parallel)"); this.setRunAllSortsName("Parallel 3-Smooth Comb Sort"); this.setRunSortName("Parallel 3-Smooth Combsort"); @@ -41,9 +41,9 @@ public ThreeSmoothCombSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; - + private class RecursiveComb extends Thread { private int pos, gap, end; RecursiveComb(int pos, int gap, int end) { @@ -55,7 +55,7 @@ public void run() { ThreeSmoothCombSortParallel.this.recursiveComb(pos, gap, end); } } - + private class PowerOfThree extends Thread { private int pos, gap, end; PowerOfThree(int pos, int gap, int end) { @@ -67,15 +67,15 @@ public void run() { ThreeSmoothCombSortParallel.this.powerOfThree(pos, gap, end); } } - + private void recursiveComb(int pos, int gap, int end) { if(pos+gap > end) return; - + RecursiveComb a = new RecursiveComb(pos, gap*2, end); RecursiveComb b = new RecursiveComb(pos+gap, gap*2, end); a.start(); b.start(); - + try { a.join(); b.join(); @@ -87,14 +87,14 @@ private void recursiveComb(int pos, int gap, int end) { private void powerOfThree(int pos, int gap, int end) { if(pos+gap > end) return; - + PowerOfThree a = new PowerOfThree(pos, gap*3, end); PowerOfThree b = new PowerOfThree(pos+gap, gap*3, end); PowerOfThree c = new PowerOfThree(pos+2*gap, gap*3, end); a.start(); b.start(); c.start(); - + try { a.join(); b.join(); @@ -106,10 +106,10 @@ private void powerOfThree(int pos, int gap, int end) { if(Reads.compareIndices(this.array, i, i+gap, 0.5, true) == 1) Writes.swap(this.array, i, i+gap, 0.5, false, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.recursiveComb(0, 1, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/ThreeSmoothCombSortRecursive.java b/src/main/java/sorts/exchange/ThreeSmoothCombSortRecursive.java index ef76dda4..f30b8201 100644 --- a/src/main/java/sorts/exchange/ThreeSmoothCombSortRecursive.java +++ b/src/main/java/sorts/exchange/ThreeSmoothCombSortRecursive.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 PiotrGrochowski Copyright (c) 2020 aphitorite @@ -26,10 +26,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ThreeSmoothCombSortRecursive extends Sort { +public final class ThreeSmoothCombSortRecursive extends Sort { public ThreeSmoothCombSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("3-Smooth Comb (Recursive)"); this.setRunAllSortsName("Recursive 3-Smooth Comb Sort"); this.setRunSortName("Recursive 3-Smooth Combsort"); @@ -44,27 +44,27 @@ public ThreeSmoothCombSortRecursive(ArrayVisualizer arrayVisualizer) { private void recursiveComb(int[] array, int pos, int gap, int end) { if(pos+gap > end) return; - + this.recursiveComb(array, pos, gap*2, end); this.recursiveComb(array, pos+gap, gap*2, end); - + this.powerOfThree(array, pos, gap, end); } private void powerOfThree(int[] array, int pos, int gap, int end) { if(pos+gap > end) return; - + this.powerOfThree(array, pos, gap*3, end); this.powerOfThree(array, pos+gap, gap*3, end); this.powerOfThree(array, pos+2*gap, gap*3, end); - + for(int i = pos; i+gap < end; i+=gap) if(Reads.compareIndices(array, i, i+gap, 0.5, true) == 1) Writes.swap(array, i, i+gap, 0.5, false, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.recursiveComb(array, 0, 1, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/UnoptimizedBubbleSort.java b/src/main/java/sorts/exchange/UnoptimizedBubbleSort.java index 92df1a09..5dab95fb 100644 --- a/src/main/java/sorts/exchange/UnoptimizedBubbleSort.java +++ b/src/main/java/sorts/exchange/UnoptimizedBubbleSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class UnoptimizedBubbleSort extends Sort { +public final class UnoptimizedBubbleSort extends Sort { public UnoptimizedBubbleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Unoptimized Bubble"); this.setRunAllSortsName("Unoptimized Bubble Sort"); this.setRunSortName("Unoptimized Bubblesort"); @@ -51,7 +51,7 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti while(!sorted) { sorted = true; - for(int i = 0; i < sortLength - 1; i++) { + for(int i = 0; i < sortLength - 1; i++) { if(Reads.compareValues(array[i], array[i + 1]) == 1){ Writes.swap(array, i, i + 1, 0.075, true, false); sorted = false; @@ -61,6 +61,6 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti Highlights.markArray(2, i + 1); Delays.sleep(0.05); } - } + } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/exchange/UnoptimizedCocktailShakerSort.java b/src/main/java/sorts/exchange/UnoptimizedCocktailShakerSort.java index 3eac9f78..0fb801f1 100644 --- a/src/main/java/sorts/exchange/UnoptimizedCocktailShakerSort.java +++ b/src/main/java/sorts/exchange/UnoptimizedCocktailShakerSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class UnoptimizedCocktailShakerSort extends Sort { +public final class UnoptimizedCocktailShakerSort extends Sort { public UnoptimizedCocktailShakerSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Unoptimized Cocktail Shaker"); this.setRunAllSortsName("Unoptimized Cocktail Shaker Sort"); this.setRunSortName("Unoptimized Cocktailsort"); @@ -52,33 +52,33 @@ private void cocktailShaker(int[] array, int start, int end, double sleep) { if(Reads.compareValues(array[j], array[j + 1]) == 1) { Writes.swap(array, j, j + 1, sleep, true, false); } - + Highlights.markArray(1, j); Highlights.markArray(2, j + 1); - + Delays.sleep(sleep / 2); } for(int j = end + start - i - 1; j > i; j--){ if(Reads.compareValues(array[j], array[j - 1]) == -1) { Writes.swap(array, j, j - 1, sleep, true, false); } - + Highlights.markArray(1, j); Highlights.markArray(2, j - 1); - + Delays.sleep(sleep / 2); } - + i++; } } - + public void customSort(int[] array, int start, int end) { this.cocktailShaker(array, start, end, 1); } - + @Override public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.cocktailShaker(array, 0, sortLength, 0.1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/AdaptiveGrailSort.java b/src/main/java/sorts/hybrid/AdaptiveGrailSort.java index eb48979d..667c9811 100644 --- a/src/main/java/sorts/hybrid/AdaptiveGrailSort.java +++ b/src/main/java/sorts/hybrid/AdaptiveGrailSort.java @@ -5,20 +5,20 @@ /* * MIT License - * + * * Copyright (c) 2013 Andrey Astrelin * Copyright (c) 2020 The Holy Grail Sort Project - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -48,16 +48,16 @@ /** * Adaptive Grail Sort - * + * * O(1) space stable worst case O(n log n) algorithm * designed to take advantage of partially ordered data with constant memory - * + * * @author aphitorite */ -final public class AdaptiveGrailSort extends Sort { +public final class AdaptiveGrailSort extends Sort { public AdaptiveGrailSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Adaptive Grail"); this.setRunAllSortsName("Adaptive Grail Sort (Block Merge Sort)"); this.setRunSortName("Adaptive Grailsort"); @@ -69,19 +69,19 @@ public AdaptiveGrailSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + enum Subarray { LEFT, RIGHT; } - + private int minRun; - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + //changes len sized blocks order ABC -> BCA private void multiTriSwap(int[] array, int a, int b, int c, int len) { Highlights.clearMark(2); @@ -92,28 +92,28 @@ private void multiTriSwap(int[] array, int a, int b, int c, int len) { Writes.write(array, c+i, temp, 0.333, true, false); } } - + private void insertTo(int[] array, int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[(a--)-1], 0.5, true, false); Writes.write(array, b, temp, 0.5, true, false); } - + private void insertToBW(int[] array, int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a < b) Writes.write(array, a, array[(a++)+1], 0.5, true, false); Writes.write(array, a, temp, 0.5, true, false); } - + private void shift(int[] array, int a, int m, int b) { while(m < b) Writes.swap(array, a++, m++, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 1 && r > 1) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -133,41 +133,41 @@ private void rotate(int[] array, int a, int m, int b) { if(r == 1) this.insertTo(array, m, a); else if(l == 1) this.insertToBW(array, a, b-1); } - + private int leftBinarySearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; - else + else a = m+1; } - + return a; } - + private int rightBinarySearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; - else + else a = m+1; } - + return a; } - + private int buildUniqueRun(int[] array, int a, int n) { int nKeys = 1, i = a+1; - + //build run at start if(Reads.compareIndices(array, i-1, i, 1, true) == -1){ i++; nKeys++; - + while(nKeys < n && Reads.compareIndices(array, i-1, i, 1, true) == -1) { i++; nKeys++; @@ -176,25 +176,25 @@ private int buildUniqueRun(int[] array, int a, int n) { else if(Reads.compareIndices(array, i-1, i, 1, true) == 1) { i++; nKeys++; - + while(nKeys < n && Reads.compareIndices(array, i-1, i, 1, true) == 1) { i++; nKeys++; } Writes.reversal(array, a, i-1, 1, true, false); } - + return nKeys; } - + private int buildUniqueRunBW(int[] array, int b, int n) { int nKeys = 1, i = b-1; - + //build run at end if(Reads.compareIndices(array, i-1, i, 1, true) == -1){ i--; nKeys++; - + while(nKeys < n && Reads.compareIndices(array, i-1, i, 1, true) == -1) { i--; nKeys++; @@ -203,17 +203,17 @@ private int buildUniqueRunBW(int[] array, int b, int n) { else if(Reads.compareIndices(array, i-1, i, 1, true) == 1) { i--; nKeys++; - + while(nKeys < n && Reads.compareIndices(array, i-1, i, 1, true) == 1) { i--; nKeys++; } Writes.reversal(array, i, b-1, 1, true, false); } - + return nKeys; } - + private int findKeys(int[] array, int a, int b, int nKeys, int n) { int p = a, pEnd = a+nKeys; @@ -222,14 +222,14 @@ private int findKeys(int[] array, int a, int b, int nKeys, int n) { Highlights.markArray(1, i); Delays.sleep(1); int loc = this.leftBinarySearch(array, p, pEnd, array[i]); - + if(pEnd == loc || Reads.compareValues(array[i], array[loc]) != 0) { this.rotate(array, p, pEnd, i); int inc = i-pEnd; loc += inc; p += inc; pEnd += inc; - + this.insertTo(array, pEnd, loc); nKeys++; pEnd++; @@ -248,7 +248,7 @@ private int findKeysBW(int[] array, int a, int b, int nKeys, int n) { Highlights.markArray(1, i); Delays.sleep(1); int loc = this.leftBinarySearch(array, p, pEnd, array[i]); - + if(pEnd == loc || Reads.compareValues(array[i], array[loc]) != 0) { this.rotate(array, i+1, p, pEnd); int inc = p-(i+1); @@ -256,27 +256,27 @@ private int findKeysBW(int[] array, int a, int b, int nKeys, int n) { pEnd -= inc; p -= inc+1; nKeys++; - + this.insertToBW(array, i, loc-1); } } this.rotate(array, p, pEnd, b); return nKeys; } - + //instead of insertion level find & create runs divisible by minRun private void buildRuns(int[] array, int a, int b) { int i = a+1, j = a; - + while(i < b) { if(Reads.compareIndices(array, i-1, i++, 1, true) == 1) { while(i < b && Reads.compareIndices(array, i-1, i, 1, true) == 1) i++; Writes.reversal(array, j, i-1, 1, true, false); } else while(i < b && Reads.compareIndices(array, i-1, i, 1, true) <= 0) i++; - + if(i < b) j = i - (i-j-1)%this.minRun - 1;//a%b, if(a%b == 0) -> a = b - + while(i-j < this.minRun && i < b) { this.insertTo(array, i, this.rightBinarySearch(array, j, i, array[i])); i++; @@ -284,17 +284,17 @@ private void buildRuns(int[] array, int a, int b) { j = i++; } } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, i, this.rightBinarySearch(array, a, i, array[i])); } - + private void mergeWithBufRest(int[] array, int a, int m, int b, int p, int pLen) { int i = 0, j = m, k = a; - + while(i < pLen && j < b) { - if(Reads.compareValues(array[p+i], array[j]) <= 0) + if(Reads.compareValues(array[p+i], array[j]) <= 0) Writes.swap(array, k++, p+(i++), 1, true, false); else Writes.swap(array, k++, j++, 1, true, false); @@ -307,132 +307,132 @@ private void mergeWithBuf(int[] array, int a, int m, int b, int p) { this.multiSwap(array, p, a, l); this.mergeWithBufRest(array, a, m, b, p, l); } - + private void mergeWithBufBW(int[] array, int a, int m, int b, int p) { int pLen = b-m; this.multiSwap(array, m, p, pLen); - + int i = pLen-1, j = m-1, k = b-1; - + while(i >= 0 && j >= a) { - if(Reads.compareValues(array[p+i], array[j]) >= 0) + if(Reads.compareValues(array[p+i], array[j]) >= 0) Writes.swap(array, k--, p+(i--), 1, true, false); else Writes.swap(array, k--, j--, 1, true, false); } while(i >= 0) Writes.swap(array, k--, p+(i--), 1, true, false); } - + private void inPlaceMerge(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > 0) { k = this.leftBinarySearch(array, j+1, b, array[i]); this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } } - + private void inPlaceMergeBW(int[] array, int a, int m, int b) { int i = m-1, j = b-1, k; - + while(j > i && i >= a){ if(Reads.compareValues(array[i], array[j]) > 0) { k = this.rightBinarySearch(array, a, i, array[j]); this.rotate(array, k, i+1, j+1); - + j -= (i+1)-k; i = k-1; - } + } else j--; } } - + private void mergeWithoutBuf(int[] array, int a, int m, int b) { if(m-a > b-m) this.inPlaceMergeBW(array, a, m, b); else this.inPlaceMerge(array, a, m, b); } - + private boolean checkSorted(int[] array, int a, int m, int b) { return Reads.compareValues(array[m-1], array[m]) > 0; } - + private boolean checkReverseBounds(int[] array, int a, int m, int b) { if(Reads.compareValues(array[a], array[b-1]) == 1) { this.rotate(array, a, m, b); return false; } - + return true; } - + private boolean checkBounds(int[] array, int a, int m, int b) { - return this.checkSorted(array, a, m, b) + return this.checkSorted(array, a, m, b) && this.checkReverseBounds(array, a, m, b); } - + private Subarray grailGetSubarray(int[] array, int t, int mKey) { if(Reads.compareValues(array[t], array[mKey]) < 0) return Subarray.LEFT; - + else return Subarray.RIGHT; } - + //returns mKey final position private int blockSelectSort(int[] array, int p, int t, int r, int d, int lCount, int bCount, int bLen) { int mKey = lCount; - + for(int j = 0, k = lCount+1; j < k-1; j++) { int min = j; - + for(int i = Math.max(lCount-r, j+1); i < k; i++) { int comp = Reads.compareIndices(array, p+d + i*bLen, p+d + min*bLen, 2, true); if(comp < 0 || (comp == 0 && Reads.compareValues(array[t+i], array[t+min]) < 0)) min = i; } - + if(min != j) { this.multiSwap(array, p + j*bLen, p + min*bLen, bLen); Writes.swap(array, t+j, t+min, 1, true, false); - + if(k < bCount && min == k-1) k++; } if(min == mKey) mKey = j; } - + return t+mKey; } - + //special thanks to @Anonymous0726 for this idea private void grailSortKeys(int[] array, int b, int p, int mKey) { Writes.swap(array, p, mKey, 1, true, false); int i = mKey, j = i+1, k = p+1; - + while(j < b) { if(Reads.compareValues(array[j], array[p]) < 0) Writes.swap(array, i++, j, 1, true, false); - + else Writes.swap(array, k++, j, 1, true, false); - + j++; } - + this.multiSwap(array, i, p, b-i); } - + private void grailSortKeysWithoutBuf(int[] array, int b, int mKey) { int i = mKey, j = i+1; - + while(j < b) { if(Reads.compareValues(array[j], array[i]) < 0) this.insertTo(array, j, i++); - + j++; } } @@ -440,29 +440,29 @@ private void grailSortKeysWithoutBuf(int[] array, int b, int mKey) { //special thanks to @Anonymous0726 for this idea private int grailMergeBlocks(int[] array, int a, int m, int b, int p) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareValues(array[i], array[j]) <= 0) Writes.swap(array, p++, i++, 1, true, false); - + else Writes.swap(array, p++, j++, 1, true, false); } - + if(i > p) while(i < m) Writes.swap(array, p++, i++, 1, true, false); return j; } - + //same as grailMergeBlocks() except reverses equal items order private int grailMergeBlocksRev(int[] array, int a, int m, int b, int p) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareValues(array[i], array[j]) < 0) Writes.swap(array, p++, i++, 1, true, false); - + else Writes.swap(array, p++, j++, 1, true, false); } - + if(i > p) while(i < m) Writes.swap(array, p++, i++, 1, true, false); return j; } @@ -473,13 +473,13 @@ private void grailBlockMerge(int[] array, int a, int m, int b, int t, int p, int int b1 = b - (b-m-1)%bLen - 1, i = a+bLen, j = a, key = t-1, lCount = (m-i)/bLen, bCount = (b1-i)/bLen, l = -1, r = lCount-1; - + this.multiTriSwap(array, p, m-bLen, a, bLen); this.insertToBW(array, t, t+lCount-1); - + int mKey = this.blockSelectSort(array, i, t, 1, bLen-1, lCount, bCount, bLen); Subarray frag = Subarray.LEFT; - + while(l < lCount && r < bCount) { if(frag == Subarray.LEFT) { do { @@ -488,13 +488,13 @@ private void grailBlockMerge(int[] array, int a, int m, int b, int t, int p, int key++; } while(l < lCount && this.grailGetSubarray(array, key, mKey) == Subarray.LEFT); - + if(l == lCount) { i = this.grailMergeBlocks(array, i, j, b, i-bLen); this.mergeWithBufRest(array, i-bLen, i, b, p, bLen); } else i = this.grailMergeBlocks(array, i, j, j+bLen-1, i-bLen); - + frag = Subarray.RIGHT; } else { @@ -504,72 +504,72 @@ private void grailBlockMerge(int[] array, int a, int m, int b, int t, int p, int key++; } while(r < bCount && this.grailGetSubarray(array, key, mKey) == Subarray.RIGHT); - + if(r == bCount) { this.shift(array, i-bLen, i, b); this.multiSwap(array, p, b-bLen, bLen); } else i = this.grailMergeBlocksRev(array, i, j, j+bLen-1, i-bLen); - + frag = Subarray.LEFT; } } - + this.grailSortKeys(array, t+bCount, p, mKey); } - + //TODO: rewrite strat 2 merge to be more adaptive - + //1024 items, 8 unique, linear, final merge pass //old: 5812 comps & 5762 writes //new: ??? - + /*private int grailMergeBlocksWithoutBuf(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > 0) { k = this.leftBinarySearch(array, j+1, b, array[i]); this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } - + return i; } - + private int grailMergeBlocksWithoutBufRev(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) >= 0) { k = this.rightBinarySearch(array, j+1, b, array[i]); this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } - + return i; }*/ - + //old private void grailBlockMergeWithoutBuf(int[] array, int a, int m, int b, int t, int bLen) { int a1 = a + (m-a)%bLen, b1 = b - (b-m)%bLen, i = a, j = a1, key = t, lCount = (m-j)/bLen + 1, bCount = (b1-j)/bLen + 1, l = 0, r = lCount; - + int mKey = this.blockSelectSort(array, j, t, 0, 0, lCount-1, bCount-1, bLen); Subarray frag = Subarray.LEFT; - + while(l < lCount && r < bCount) { Subarray next = this.grailGetSubarray(array, key++, mKey); - + if(next == frag) { if(frag == Subarray.LEFT) l++; else r++; @@ -577,16 +577,16 @@ private void grailBlockMergeWithoutBuf(int[] array, int a, int m, int b, int t, } else {//grailMergeBlocksWithoutBuf() int m2 = j, b2 = j+bLen, k; - + if(frag == Subarray.LEFT) { while(i < m2 && m2 < b2) { if(Reads.compareValues(array[i], array[m2]) > 0) { k = this.leftBinarySearch(array, m2+1, b2, array[i]); this.rotate(array, i, m2, k); - + i += k-m2; m2 = k; - } + } else i++; } } @@ -595,14 +595,14 @@ private void grailBlockMergeWithoutBuf(int[] array, int a, int m, int b, int t, if(Reads.compareValues(array[i], array[m2]) >= 0) { k = this.rightBinarySearch(array, m2+1, b2, array[i]); this.rotate(array, i, m2, k); - + i += k-m2; m2 = k; - } + } else i++; } } - + if(i < m2) {//right side is merged first if(next == Subarray.LEFT) l++; else r++; @@ -613,33 +613,33 @@ private void grailBlockMergeWithoutBuf(int[] array, int a, int m, int b, int t, frag = next; } } - + j += bLen; } - + if(l < lCount) this.inPlaceMergeBW(array, a, b1, b); this.grailSortKeysWithoutBuf(array, t+bCount-1, mKey); } - + private void smartMerge(int[] array, int a, int m, int b, int p) { if(this.checkBounds(array, a, m, b)) { a = this.rightBinarySearch(array, a, m-1, array[m]); this.mergeWithBuf(array, a, m, b, p); } } - + private void smartMergeBW(int[] array, int a, int m, int b, int p) { if(this.checkBounds(array, a, m, b)) { b = this.leftBinarySearch(array, m+1, b, array[m-1]); this.mergeWithBufBW(array, a, m, b, p); } } - + private void smartBlockMerge(int[] array, int a, int m, int b, int t, int p, int bLen) { if(this.checkBounds(array, a, m, b)) { int n = this.rightBinarySearch(array, a, m-1, array[m]); b = this.leftBinarySearch(array, m+1, b, array[m-1]); - + if(this.checkReverseBounds(array, n, m, b)) { if(m-n <= bLen || b-m <= bLen) { if(b-m < m-n) this.mergeWithBufBW(array, n, m, b, p); @@ -652,61 +652,61 @@ private void smartBlockMerge(int[] array, int a, int m, int b, int t, int p, int } } } - + private void smartBlockMergeWithoutBuf(int[] array, int a, int m, int b, int t, int bLen) { if(this.checkBounds(array, a, m, b)) { a = this.rightBinarySearch(array, a, m-1, array[m]); - + if(m-a <= bLen) this.inPlaceMerge(array, a, m, b); else this.grailBlockMergeWithoutBuf(array, a, m, b, t, bLen); } } - + private void smartInPlaceMerge(int[] array, int a, int m, int b) { if(this.checkSorted(array, a, m, b)) this.inPlaceMergeBW(array, a, m, b); } - + private void redistBuffer(int[] array, int a, int m, int b) { int rPos = this.leftBinarySearch(array, m, b, array[a]); this.rotate(array, a, m, rPos); - + int dist = rPos-m; a += dist; m += dist; - + int a1 = a+(m-a)/2; rPos = this.leftBinarySearch(array, m, b, array[a1]); this.rotate(array, a1, m, rPos); - + dist = rPos-m; a1 += dist; m += dist; - + this.mergeWithoutBuf(array, a, a1-dist, a1); this.mergeWithoutBuf(array, a1, m, b); } - + private void redistBufferBW(int[] array, int a, int m, int b) { int rPos = this.rightBinarySearch(array, a, m, array[b-1]); this.rotate(array, rPos, m, b); - + int dist = m-rPos; b -= dist; m -= dist; - + int b1 = m+(b-m)/2; rPos = this.rightBinarySearch(array, a, m, array[b1-1]); this.rotate(array, rPos, m, b1); - + dist = m-rPos; b1 -= dist; m -= dist; - + this.mergeWithoutBuf(array, b1, b1+dist, b); this.mergeWithoutBuf(array, a, m, b1); } - + private void inPlaceMergeSort(int[] array, int a, int b) { this.buildRuns(array, a, b); @@ -714,84 +714,84 @@ private void inPlaceMergeSort(int[] array, int a, int b) { for(int i, j = this.minRun; j < len; j *= 2) { for(i = a; i + 2*j <= b; i += 2*j) this.smartInPlaceMerge(array, i, i+j, i+2*j); - + if(i + j < b) this.smartInPlaceMerge(array, i, i+j, b); } } - + private void grailAdaptiveSortWithoutBuf(int[] array, int a, int b, int keys, int ideal, boolean bwBuf) { int len = b-a, bLen; for(bLen = Math.min(keys, this.minRun); 2*bLen <= keys; bLen *= 2); int tLen = keys-bLen; - + int i, j = this.minRun, t, p, a1, b1; - + if(bwBuf) { p = b-bLen; a1 = a; b1 = p-tLen; t = b1; } else { p = a+tLen; a1 = p+bLen; b1 = b; t = a; } - + //insertion level this.buildRuns(array, a1, b1); - + //merge with buffer level while(j <= bLen && j < len) { for(i = a1; i + 2*j <= b1; i += 2*j) this.smartMerge(array, i, i+j, i+2*j, p); - + if(i + j < b1) this.smartMergeBW(array, i, i+j, b1, p); - + j *= 2; } - + if(bLen/2 >= this.minRun && bLen/2 >= (keys+1)/2) { this.binaryInsertion(array, p, p+bLen); - + bLen /= 2; tLen = keys-bLen; p += bLen; } - + //block merge level while(tLen >= 2*j/bLen - 1 && j < len) { for(i = a1; i + 2*j <= b1; i += 2*j) this.smartBlockMerge(array, i, i+j, i+2*j, t, p, bLen); - + if(i + j < b1) { - if(b1 - (i+j) > bLen) + if(b1 - (i+j) > bLen) this.smartBlockMerge(array, i, i+j, b1, t, p, bLen); - + else this.smartMergeBW(array, i, i+j, b1, p); } - + j *= 2; } - + this.binaryInsertion(array, p, p+bLen); tLen = keys-keys%2; - + //block merge w/o buffer level while(j < len) { bLen = 2*j / tLen; - + for(i = a1; i + 2*j <= b1; i += 2*j) this.smartBlockMergeWithoutBuf(array, i, i+j, i+2*j, t, bLen); - + if(i + j < b1) { - if(b1 - (i+j) > bLen) + if(b1 - (i+j) > bLen) this.smartBlockMergeWithoutBuf(array, i, i+j, b1, t, bLen); - + else this.smartInPlaceMerge(array, i, i+j, b1); } - + j *= 2; } - + //buffer redistribution if(bwBuf) { a = this.rightBinarySearch(array, a, b1, array[b1]); @@ -804,10 +804,10 @@ private void grailAdaptiveSortWithoutBuf(int[] array, int a, int b, int keys, in else this.mergeWithoutBuf(array, a, a1, b); } } - + protected void grailAdaptiveSort(int[] array, int a, int b) { int len = b-a; - + //insertion on small len if(len < 31) { this.binaryInsertion(array, a, b); @@ -817,87 +817,87 @@ protected void grailAdaptiveSort(int[] array, int a, int b) { if(len < 63) { this.minRun = (len+1)/2; this.buildRuns(array, a, b); - + int m = a+this.minRun; if(this.checkBounds(array, a, m, b)) this.redistBufferBW(array, a, m, b); - + return; } //calculate optimal minRun & block len for(this.minRun = len; this.minRun >= 32; this.minRun = (this.minRun+1)/2); - + int bLen; for(bLen = this.minRun; bLen*bLen < len; bLen *= 2); - + int tLen = len/bLen - 2, ideal = tLen + bLen; - + //choose direction to find keys boolean bwBuf; int rRun = this.buildUniqueRunBW(array, b, ideal), lRun = 0; - + if(rRun == ideal) bwBuf = true; else { lRun = this.buildUniqueRun(array, a, ideal); - + if(lRun == ideal) bwBuf = false; else bwBuf = (rRun < 16 && lRun < 16) || rRun >= lRun; } - + //find bLen + tLen unique buffer keys int keys = bwBuf ? this.findKeysBW(array, a, b, rRun, ideal) : this.findKeys(array, a, b, lRun, ideal); - + if(keys < ideal) { if(keys == 1) return; else if(keys <= 4) this.inPlaceMergeSort(array, a, b); else this.grailAdaptiveSortWithoutBuf(array, a, b, keys, ideal, bwBuf); return; } - + int i, j = this.minRun, t, p, a1, b1; - + if(bwBuf) { p = b-bLen; a1 = a; b1 = p-tLen; t = b1; } else { p = a+tLen; a1 = p+bLen; b1 = b; t = a; } - + //insertion level this.buildRuns(array, a1, b1); - + //merge with buffer level while(j <= bLen && j < len) { for(i = a1; i + 2*j <= b1; i += 2*j) this.smartMerge(array, i, i+j, i+2*j, p); - + if(i + j < b1) this.smartMergeBW(array, i, i+j, b1, p); - + j *= 2; } - + //block merge level while(j < len) { for(i = a1; i + 2*j <= b1; i += 2*j) this.smartBlockMerge(array, i, i+j, i+2*j, t, p, bLen); - + if(i + j < b1) { - if(b1 - (i+j) > bLen) + if(b1 - (i+j) > bLen) this.smartBlockMerge(array, i, i+j, b1, t, p, bLen); - + else this.smartMergeBW(array, i, i+j, b1, p); } - + j *= 2; } this.binaryInsertion(array, p, p+bLen); - + //buffer redistribution if(bwBuf) { a = this.rightBinarySearch(array, a, b1, array[b1]); @@ -908,9 +908,9 @@ protected void grailAdaptiveSort(int[] array, int a, int b) { this.redistBuffer(array, a, a1, b); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.grailAdaptiveSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/BinaryMergeSort.java b/src/main/java/sorts/hybrid/BinaryMergeSort.java index face697b..19175f36 100644 --- a/src/main/java/sorts/hybrid/BinaryMergeSort.java +++ b/src/main/java/sorts/hybrid/BinaryMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.MergeSorting; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BinaryMergeSort extends MergeSorting { +public final class BinaryMergeSort extends MergeSorting { public BinaryMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binary Merge"); this.setRunAllSortsName("Binary Merge Sort"); this.setRunSortName("Binary Mergesort"); @@ -44,9 +44,9 @@ public BinaryMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.mergeSort(array, length, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/BufferPartitionMergeSort.java b/src/main/java/sorts/hybrid/BufferPartitionMergeSort.java index 9fc3cd18..9aab914a 100644 --- a/src/main/java/sorts/hybrid/BufferPartitionMergeSort.java +++ b/src/main/java/sorts/hybrid/BufferPartitionMergeSort.java @@ -7,7 +7,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 yuji, implemented by aphitorite @@ -32,10 +32,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BufferPartitionMergeSort extends Sort { +public final class BufferPartitionMergeSort extends Sort { public BufferPartitionMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Buffer Partition Merge (BPM)"); this.setRunAllSortsName("Buffer Partition Merge Sort"); this.setRunSortName("BPMSort"); @@ -47,22 +47,22 @@ public BufferPartitionMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private InsertionSort insSort; private BinaryInsertionSort binInsSort; - + private void shiftBW(int[] array, int a, int m, int b) { while(m > a) Writes.swap(array, --b, --m, 1, true, false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -78,45 +78,45 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private void inPlaceMerge(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > 0) { k = j; while(++k < b && Reads.compareIndices(array, i, k, 0, false) > 0); - + this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } } - + private void medianOfThree(int[] array, int a, int b) { int m = a+(b-1-a)/2; - + if(Reads.compareIndices(array, a, m, 1, true) == 1) Writes.swap(array, a, m, 1, true, false); - + if(Reads.compareIndices(array, m, b-1, 1, true) == 1) { Writes.swap(array, m, b-1, 1, true, false); - + if(Reads.compareIndices(array, a, m, 1, true) == 1) return; } - + Writes.swap(array, a, m, 1, true, false); } - + //lite version private void medianOfMedians(int[] array, int a, int b, int s) { int end = b, start = a, i, j; boolean ad = true; - + while(end - start > 1) { j = start; Highlights.markArray(2, j); @@ -134,11 +134,11 @@ private void medianOfMedians(int[] array, int a, int b, int s) { end = j; } } - + private int partition(int[] array, int a, int b) { int i = a, j = b; Highlights.markArray(3, a); - + while(true) { do { i++; @@ -146,86 +146,86 @@ private int partition(int[] array, int a, int b) { Delays.sleep(0.5); } while(i < j && Reads.compareIndices(array, i, a, 0, false) == 1); - + do { j--; Highlights.markArray(2, j); Delays.sleep(0.5); } while(j >= i && Reads.compareIndices(array, j, a, 0, false) == -1); - + if(i < j) Writes.swap(array, i, j, 1, true, false); else return j; } } - + private int quickSelect(int[] array, int a, int b, int m) { boolean badPartition = false, mom = false; int m1 = (m+b+1)/2; - + while(true) { if(badPartition) { this.medianOfMedians(array, a, b, 5); mom = true; } else this.medianOfThree(array, a, b); - + int p = this.partition(array, a, b); Writes.swap(array, a, p, 1, true, false); - + int l = Math.max(1, p-a); int r = Math.max(1, b-(p+1)); badPartition = !mom && (l/r >= 16 || r/l >= 16); - + if(p >= m && p < m1) return p; else if(p < m) a = p+1; else b = p; } } - + private void merge(int[] array, int a, int m, int b, int p) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareIndices(array, i, j, 0, false) <= 0) Writes.swap(array, p++, i++, 1, true, false); - else + else Writes.swap(array, p++, j++, 1, true, false); } - + while(i < m) Writes.swap(array, p++, i++, 1, true, false); while(j < b) Writes.swap(array, p++, j++, 1, true, false); } - + private int mergeFW(int[] array, int p, int a, int m, int b) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareIndices(array, i, j, 0, false) <= 0) Writes.swap(array, p++, i++, 1, true, false); - else + else Writes.swap(array, p++, j++, 1, true, false); } - + if(i < m) return i; else return j; } - + public static int getMinLevel(int n) { while(n >= 32) n = (n+3)/4; return n; } - + private void mergeSort(int[] array, int a, int b, int p) { int len = b-a; if(len < 2) return; - + int i, pos, j = getMinLevel(len); - + for(i = a; i+j <= b; i+=j) this.binInsSort.customBinaryInsert(array, i, i+j, 0.25); this.binInsSort.customBinaryInsert(array, i, b, 0.25); - + while(j < len) { pos = p; for(i = a; i+2*j <= b; i+=2*j, pos+=2*j) @@ -234,9 +234,9 @@ private void mergeSort(int[] array, int a, int b, int p) { this.merge(array, i, i+j, b, pos); else while(i < b) Writes.swap(array, i++, pos++, 1, true, false); - + j *= 2; - + pos = a; for(i = p; i+2*j <= p+len; i+=2*j, pos+=2*j) this.merge(array, i, i+j, i+2*j, pos); @@ -244,35 +244,35 @@ private void mergeSort(int[] array, int a, int b, int p) { this.merge(array, i, i+j, p+len, pos); else while(i < p+len) Writes.swap(array, i++, pos++, 1, true, false); - + j *= 2; } } - + private void sort(int[] array, int a, int b) { int minLvl = (int)Math.sqrt(b-a); - + int m = (a+b+1)/2; this.mergeSort(array, m, b, a); - + while(m-a > minLvl) { int m1 = (a+m+1)/2; - + m1 = this.quickSelect(array, a, m, m1); this.mergeSort(array, m1, m, a); - + int bSize = m1-a; int m2 = Math.min(m1+bSize, b); m1 = this.mergeFW(array, a, m1, m, m2); - + while(m1 < m) { this.shiftBW(array, m1, m, m2); m1 = m2-(m-m1); a = m1-bSize; m = m2; - + if(m == b) break; - + m2 = Math.min(m2+bSize, b); m1 = this.mergeFW(array, a, m1, m, m2); } @@ -282,11 +282,11 @@ private void sort(int[] array, int a, int b) { this.binInsSort.customBinaryInsert(array, a, m, 0.25); this.inPlaceMerge(array, a, m, b); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.insSort = new InsertionSort(this.arrayVisualizer); this.binInsSort = new BinaryInsertionSort(this.arrayVisualizer); this.sort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/ChaliceSort.java b/src/main/java/sorts/hybrid/ChaliceSort.java index 92690f05..48f2c5b1 100644 --- a/src/main/java/sorts/hybrid/ChaliceSort.java +++ b/src/main/java/sorts/hybrid/ChaliceSort.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ChaliceSort extends BlockMergeSorting { +public final class ChaliceSort extends BlockMergeSorting { public ChaliceSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -765,4 +765,4 @@ else if(keys < 2*kLen) { Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/CircularGrailSort.java b/src/main/java/sorts/hybrid/CircularGrailSort.java index d61d423d..840327c2 100644 --- a/src/main/java/sorts/hybrid/CircularGrailSort.java +++ b/src/main/java/sorts/hybrid/CircularGrailSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CircularGrailSort extends Sort { +public final class CircularGrailSort extends Sort { public CircularGrailSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Circular Grail"); this.setRunAllSortsName("Circular Grail Sort"); this.setRunSortName("Circular Grailsort"); @@ -44,37 +44,37 @@ public CircularGrailSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int n; - + private void circSwap(int[] array, int a, int b) { Writes.swap(array, a%n, b%n, 1, true, false); } private int circCompareIndices(int[] array, int a, int b) { return Reads.compareValues(array[a%n], array[b%n]); } - + private void shiftFW(int[] array, int a, int m, int b) { while(m < b) this.circSwap(array, a++, m++); } private void shiftBW(int[] array, int a, int m, int b) { while(m > a) this.circSwap(array, --b, --m); } - + private void insertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) while(i > a && this.circCompareIndices(array, i-1, i) > 0) this.circSwap(array, i, --i); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) this.circSwap(array, a+i, b+i); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -90,31 +90,31 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private void inPlaceMerge(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(this.circCompareIndices(array, i, j) > 0) { k = j; while(++k < b && this.circCompareIndices(array, i, k) > 0); - + this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } } - + private int merge(int[] array, int p, int a, int m, int b, boolean full) { int i = a, j = m; - + while(i < m && j < b) { if(this.circCompareIndices(array, i, j) <= 0) this.circSwap(array, p++, i++); - + else this.circSwap(array, p++, j++); } @@ -123,37 +123,37 @@ private int merge(int[] array, int p, int a, int m, int b, boolean full) { } else if(full) this.shiftFW(array, p, j, b); - + return i < m ? i : j; } - + private boolean blockLessThan(int[] array, int a, int b, int bLen) { int cmp = this.circCompareIndices(array, a, b); - + return cmp == -1 || (cmp == 0 && this.circCompareIndices(array, a+bLen-1, b+bLen-1) == -1); } - + private void blockMerge(int[] array, int a, int m, int b, int bLen) { int b1 = b-(b-m-1)%bLen-1; - + if(b1 > m) { int b2 = b1; - + for(int i = m-bLen; i > a && this.blockLessThan(array, b1, i, bLen); i -= bLen, b2 -= bLen); - + for(int j = a; j < b1-bLen; j += bLen) { int min = j; - + for(int i = min+bLen; i < b1; i += bLen) if(this.blockLessThan(array, i, min, bLen)) min = i; - + if(min != j) this.multiSwap(array, j, min, bLen); } int f = a; - + for(int i = a+bLen; i < b2; i += bLen) { f = this.merge(array, f-bLen, f, i, i+bLen, false); - + if(f < i) { this.shiftBW(array, f, i, i+bLen); f += bLen; @@ -163,21 +163,21 @@ private void blockMerge(int[] array, int a, int m, int b, int bLen) { } else this.merge(array, a-bLen, a, m, b, true); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.n = length; - + if(length <= 16) { this.insertion(array, 0, length); return; } - + int bLen = 1; for(; bLen*bLen < length; bLen *= 2); - + int i = bLen, j = 1, len = length-i, b = length; - + while(j <= bLen) { for(; i+2*j < b; i += 2*j) this.merge(array, i-j, i, i+j, i+2*j, true); @@ -185,7 +185,7 @@ public void runSort(int[] array, int length, int bucketCount) { this.merge(array, i-j, i, i+j, b, true); else this.shiftFW(array, i-j, i, b); - + i = b+bLen-j; b = i+len; j *= 2; @@ -197,14 +197,14 @@ public void runSort(int[] array, int length, int bucketCount) { this.blockMerge(array, i, i+j, b, bLen); else this.shiftFW(array, i-bLen, i, b); - + i = b; b += len; j *= 2; } this.insertion(array, i-bLen, i); this.inPlaceMerge(array, i-bLen, i, b); - + this.rotate(array, 0, (i-bLen)%n, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/CocktailMergeSort.java b/src/main/java/sorts/hybrid/CocktailMergeSort.java index 27b92d24..74d31371 100644 --- a/src/main/java/sorts/hybrid/CocktailMergeSort.java +++ b/src/main/java/sorts/hybrid/CocktailMergeSort.java @@ -59,4 +59,4 @@ public void runSort(int[] array, int sortLength, int bucketCount) throws Excepti TimSorting.sort(this.timSortInstance, array, sortLength); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/DropMergeSort.java b/src/main/java/sorts/hybrid/DropMergeSort.java index 9cfa106d..6f55ecac 100644 --- a/src/main/java/sorts/hybrid/DropMergeSort.java +++ b/src/main/java/sorts/hybrid/DropMergeSort.java @@ -27,7 +27,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DropMergeSort extends Sort { +public final class DropMergeSort extends Sort { public DropMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); diff --git a/src/main/java/sorts/hybrid/EctaSort.java b/src/main/java/sorts/hybrid/EctaSort.java index b12f110e..c9141d55 100644 --- a/src/main/java/sorts/hybrid/EctaSort.java +++ b/src/main/java/sorts/hybrid/EctaSort.java @@ -5,7 +5,7 @@ import sorts.insert.BinaryDoubleInsertionSort; /* - * + * MIT License Copyright (c) 2020-2021 aphitorite @@ -30,10 +30,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class EctaSort extends Sort { +public final class EctaSort extends Sort { public EctaSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Ecta"); this.setRunAllSortsName("Ecta Sort"); this.setRunSortName("Ectasort"); @@ -45,63 +45,63 @@ public EctaSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int getMinRun(int n) { int mRun = n; for(; mRun >= 32; mRun = (mRun+1)/2); - + return mRun; } - + private void shift(int[] array, int a, int m, int b) { while(m < b) Writes.write(array, a++, array[m++], 1, true, false); } private void shiftBW(int[] array, int a, int m, int b) { while(m > a) Writes.write(array, --b, array[--m], 1, true, false); } - + private void mergeTo(int[] array, int a, int m, int b, int p) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareIndices(array, i, j, 0, false) <= 0) Writes.write(array, p++, array[i++], 1, true, false); - else + else Writes.write(array, p++, array[j++], 1, true, false); } while(i < m) Writes.write(array, p++, array[i++], 1, true, false); while(j < b) Writes.write(array, p++, array[j++], 1, true, false); } - + private void pingPongMerge(int[] array, int a, int m1, int m2, int m3, int b, int p) { int p1 = p + m2-a, pEnd = p + b-a; - + this.mergeTo(array, a, m1, m2, p); this.mergeTo(array, m2, m3, b, p1); this.mergeTo(array, p, p1, pEnd, a); } - + private void merge(int[] array, int a, int m, int b, int p) { int len = b-m, pEnd = p+len-1; Writes.arraycopy(array, m--, array, p, len, 1, true, false); - + while(m >= a && pEnd >= p) { if(Reads.compareValues(array[m], array[pEnd]) > 0) Writes.write(array, --b, array[m--], 1, true, false); - + else Writes.write(array, --b, array[pEnd--], 1, true, false); } while(pEnd >= p) Writes.write(array, --b, array[pEnd--], 1, true, false); } - + private void mergeFromBuf(int[] array, int[] buf, int a, int m, int b, int bufLen) { int i = 0; - + while(i < bufLen && m < b) { Highlights.markArray(2, i); - - if(Reads.compareValues(buf[i], array[m]) <= 0) + + if(Reads.compareValues(buf[i], array[m]) <= 0) Writes.write(array, a++, buf[i++], 1, true, false); else Writes.write(array, a++, array[m++], 1, true, false); @@ -111,78 +111,78 @@ private void mergeFromBuf(int[] array, int[] buf, int a, int m, int b, int bufLe Writes.write(array, a++, buf[i++], 1, true, false); } } - + private void dualMergeFromBufBW(int[] array, int[] buf, int a, int a1, int m, int b, int bufLen) { int i = bufLen-1; bufLen -= b-(m--); - + while(i >= bufLen && m >= a1) { Highlights.markArray(2, i); - - if(Reads.compareValues(buf[i], array[m]) > 0) + + if(Reads.compareValues(buf[i], array[m]) > 0) Writes.write(array, --b, buf[i--], 1, true, false); else Writes.write(array, --b, array[m--], 1, true, false); } if(m < a1) while(i >= 0) Writes.write(array, --b, buf[i--], 1, true, false); - else + else this.mergeFromBuf(array, buf, a, a1, b, bufLen); } - + private int mergeSort(int[] array, int a, int b, int p, int mRun, int bufLen) { BinaryDoubleInsertionSort smallSort = new BinaryDoubleInsertionSort(this.arrayVisualizer); int i = a, j = mRun; - + for(; i+j <= b; i += j) smallSort.customDoubleInsert(array, i, i+j, 0.5); smallSort.customDoubleInsert(array, i, b, 0.5); - + while(4*j <= bufLen) { for(i = a; i+4*j <= b; i += 4*j) this.pingPongMerge(array, i, i+j, i+2*j, i+3*j, i+4*j, p); - + if(i+3*j < b) this.pingPongMerge(array, i, i+j, i+2*j, i+3*j, b, p); else if(i+2*j < b) this.pingPongMerge(array, i, i+j, i+2*j, b, b, p); - else if(i+j < b) + else if(i+j < b) this.merge(array, i, i+j, b, p); - + j *= 4; } while(j <= bufLen) { for(i = a; i+2*j <= b; i += 2*j) this.merge(array, i, i+j, i+2*j, p); - - if(i+j < b) + + if(i+j < b) this.merge(array, i, i+j, b, p); - + j *= 2; } - + return j; } - + private void blockCycle(int[] array, int[] keys, int a, int bLen, int t, int p, boolean excl, boolean fw) { int s = fw ? bLen : -bLen; - + for(int i = 0; i < t; i++) { if(Reads.compareOriginalValues(i, keys[i]) != 0) { Writes.arraycopy(array, a + i*s, array, p, bLen, 1, true, false); int j = i, next = keys[i]; - + do { if(!(excl && j == t-1)) Writes.arraycopy(array, a + next*s, array, a + j*s, bLen, 1, true, false); Highlights.markArray(2, j); Writes.write(keys, j, j, 1, false, true); - + j = next; next = keys[next]; } while(Reads.compareOriginalValues(next, i) != 0); - + Writes.arraycopy(array, p, array, a + j*s, bLen, 1, true, false); Highlights.markArray(2, j); Writes.write(keys, j, j, 1, false, true); @@ -190,15 +190,15 @@ private void blockCycle(int[] array, int[] keys, int a, int bLen, int t, int p, } Highlights.clearMark(2); } - + private void ectaMergeFW(int[] array, int[] tags, int a, int m, int b, int bLen) { int i = a, j = m, t = 0, tc = 0; int[] s = {2*bLen, 0}; int[] p = {a-2*bLen, m}; - + do { int c = s[0] < bLen ? 1 : 0; - + for(int k = 0; k < bLen; k++) { if(i < m && j < b) { if(Reads.compareValues(array[i], array[j]) <= 0) { @@ -221,14 +221,14 @@ else if(i < m) { } p[c] += bLen; s[c] -= bLen; - + Highlights.markArray(2, tc); Writes.write(tags, tc++, c == 0 ? t++ : -1, 1, false, true); } while(i < m || j < b); - + if(s[0] > 0) Writes.write(tags, tc, t++, 1, false, true); - + for(int k = 2; k < tc; k++) { if(tags[k] == -1) { Highlights.markArray(2, k); @@ -241,10 +241,10 @@ private void ectaMergeBW(int[] array, int[] tags, int a, int m, int b, int bLen) int i = b-1, j = m-1, t = 0, tc = 0; int[] s = {2*bLen, 0}; int[] p = {b+2*bLen, m}; - + do { int c = s[0] < bLen ? 1 : 0; - + for(int k = 1; k <= bLen; k++) { if(i >= m && j >= a) { if(Reads.compareValues(array[i], array[j]) >= 0) { @@ -267,14 +267,14 @@ else if(i >= m) { } p[c] -= bLen; s[c] -= bLen; - + Highlights.markArray(2, tc); Writes.write(tags, tc++, c == 0 ? t++ : -1, 1, false, true); } while(i >= m || j >= a); - + if(s[0] > 0) Writes.write(tags, tc, t++, 1, false, true); - + for(int k = 2; k < tc; k++) { if(tags[k] == -1) { Highlights.markArray(2, k); @@ -283,7 +283,7 @@ else if(i >= m) { } this.blockCycle(array, tags, b+bLen, bLen, t, a, s[0] > 0, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { if(length < 256) { @@ -294,70 +294,70 @@ public void runSort(int[] array, int length, int bucketCount) { else { int mRun = this.getMinRun(length), bufLen = length/2; int[] buf = Writes.createExternalArray(bufLen); - + Writes.arraycopy(array, bufLen, buf, 0, bufLen, 1, true, true); this.mergeSort(array, 0, bufLen, bufLen, mRun, bufLen); - + Writes.arraycopy(buf, 0, array, bufLen, bufLen, 1, true, false); Writes.arraycopy(array, 0, buf, 0, bufLen, 1, true, true); this.mergeSort(array, bufLen, length, 0, mRun, bufLen); - + this.mergeFromBuf(array, buf, 0, bufLen, length, bufLen); Writes.deleteExternalArray(buf); } return; } - + int mRun = this.getMinRun(length), bLen = mRun; for(; bLen*bLen < length/2; bLen *= 2); int bufLen = 2*bLen + length%bLen; - + int a = bufLen, b = length, len = b-a; - + int[] buf = Writes.createExternalArray(bufLen); int[] tags = Writes.createExternalArray(len/bLen+1); - + Writes.arraycopy(array, a, buf, 0, bufLen, 1, true, true); this.mergeSort(array, 0, a, a, this.getMinRun(bufLen), bufLen); - + Writes.arraycopy(buf, 0, array, a, bufLen, 1, true, false); Writes.arraycopy(array, 0, buf, 0, bufLen, 1, true, true); - + int i = a, j = this.mergeSort(array, a, b, 0, mRun, bufLen); boolean bw = false; - + while(j < len) { for(i = a; i+2*j <= b; i += 2*j) this.ectaMergeFW(array, tags, i, i+j, i+2*j, bLen); - + if(i+j < b) this.ectaMergeFW(array, tags, i, i+j, b, bLen); else this.shift(array, i-2*bLen, i, b); - + j *= 2; a -= 2*bLen; b -= 2*bLen; - + if(j >= len) { bw = true; break; } - + for(i = a; i+2*j <= b; i += 2*j); - + if(i+j < b) this.ectaMergeBW(array, tags, i, i+j, b, bLen); else this.shiftBW(array, i, b, b+2*bLen); - + for(i -= 2*j; i >= a; i -= 2*j) this.ectaMergeBW(array, tags, i, i+j, i+2*j, bLen); - + j *= 2; a += 2*bLen; b += 2*bLen; } if(bw) this.dualMergeFromBufBW(array, buf, 0, a, b, length, bufLen); else this.mergeFromBuf(array, buf, 0, a, b, bufLen); - + Writes.deleteExternalArray(buf); Writes.deleteExternalArray(tags); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/FifthMergeSort.java b/src/main/java/sorts/hybrid/FifthMergeSort.java index 15fa8ace..a9223b26 100644 --- a/src/main/java/sorts/hybrid/FifthMergeSort.java +++ b/src/main/java/sorts/hybrid/FifthMergeSort.java @@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ -final public class FifthMergeSort extends Sort { +public final class FifthMergeSort extends Sort { protected final class IndexPair { public int aEnd, bEnd; diff --git a/src/main/java/sorts/hybrid/FlanSort.java b/src/main/java/sorts/hybrid/FlanSort.java index cd45dda5..8594a47d 100644 --- a/src/main/java/sorts/hybrid/FlanSort.java +++ b/src/main/java/sorts/hybrid/FlanSort.java @@ -6,7 +6,7 @@ import java.util.Random; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -31,10 +31,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class FlanSort extends MultiWayMergeSorting { +public final class FlanSort extends MultiWayMergeSorting { public FlanSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Flan"); this.setRunAllSortsName("Flan Sort"); this.setRunSortName("Flansort"); @@ -46,13 +46,13 @@ public FlanSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - - //unstable sorting algorithm performing an average of + + //unstable sorting algorithm performing an average of //O(n log n) comparisons and O(n) moves in O(1) memory - + private final int G = 14; private final int R = 4; - + private int medianOfThree(int[] array, int a, int m, int b) { if(Reads.compareValues(array[m], array[a]) > 0) { if(Reads.compareValues(array[m], array[b]) < 0) @@ -73,190 +73,190 @@ private int medianOfThree(int[] array, int a, int m, int b) { } //when shuffled the first 9 and 27 items will be accessed instead respectively private int ninther(int[] array, int a, int b) { - int s = (b-a)/9; - + int s = (b-a)/9; + int a1 = this.medianOfThree(array, a, a+ s, a+2*s); int m1 = this.medianOfThree(array, a+3*s, a+4*s, a+5*s); int b1 = this.medianOfThree(array, a+6*s, a+7*s, a+8*s); - + return this.medianOfThree(array, a1, m1, b1); } private int medianOfThreeNinthers(int[] array, int a, int b) { int s = (b-a)/3; - + int a1 = this.ninther(array, a, a+s); int m1 = this.ninther(array, a+s, a+2*s); int b1 = this.ninther(array, a+2*s, b); - + return this.medianOfThree(array, a1, m1, b1); } - + private void shiftBW(int[] array, int a, int m, int b) { while(m > a) Writes.swap(array, --b, --m, 1, true, false); } - + private int leftBlockSearch(int[] array, int a, int b, int val) { int s = G+1; - + while(a < b) { int m = a+(((b-a)/s)/2)*s; Highlights.markArray(3, m); Delays.sleep(0.25); - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; - else + else a = m+s; } - + Highlights.clearMark(3); return a; } private int rightBlockSearch(int[] array, int a, int b, int val) { int s = G+1; - + while(a < b) { int m = a+(((b-a)/s)/2)*s; Highlights.markArray(3, m); Delays.sleep(0.25); - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; - else + else a = m+s; } - + Highlights.clearMark(3); return a; } - + private int rightBinSearch(int[] array, int a, int b, int val, boolean bw) { int cmp = bw ? 1 : -1; - + while(a < b) { int m = a+(b-a)/2; Highlights.markArray(3, m); Delays.sleep(0.25); - - if(Reads.compareValues(val, array[m]) == cmp) + + if(Reads.compareValues(val, array[m]) == cmp) b = m; else a = m+1; } - + Highlights.clearMark(3); return a; } - + private void insertTo(int[] array, int tmp, int a, int b) { Highlights.clearMark(2); while(a > b) Writes.write(array, a, array[--a], 0.5, true, false); Writes.write(array, b, tmp, 0.5, true, false); } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, array[i], i, this.rightBinSearch(array, a, i, array[i], false)); } - + private void kWayMerge(int[] array, int[] heap, int[] pa, int s, int b, int p, int size) { if(size < 2) { if(size == 1) while(pa[0] < b) Writes.swap(array, p++, pa[0]++, 1, true, false); return; } int a = pa[0]; - + for(int i = 0; i < size; i++) Writes.write(heap, i, i, 0, false, true); for(int i = (size-1)/2; i >= 0; i--) this.siftDown(array, heap, pa, heap[i], i, size); - + while(size > 0) { int min = heap[0]; - + Writes.swap(array, p++, pa[min], 0, true, false); Writes.write(pa, min, pa[min]+1, 1, false, true); if(pa[min] == Math.min(a+(min+1)*s, b)) this.siftDown(array, heap, pa, heap[--size], 0, size); - else + else this.siftDown(array, heap, pa, heap[0], 0, size); } } - + private void retrieve(int[] array, int i, int p, int pEnd, int bsv, boolean bw) { - int j = i-1, m; - + int j = i-1, m; + for(int k = pEnd-(G+1); k > p+G;) { m = this.rightBinSearch(array, k-G, k, bsv, bw)-1; k -= G+1; - + while(m >= k) Writes.swap(array, j--, m--, 1, true, false); } - + m = this.rightBinSearch(array, p, p+G, bsv, bw)-1; while(m >= p) Writes.swap(array, j--, m--, 1, true, false); } - + //buffer length is at least sortLength*(G+1)-1 private void librarySort(int[] array, int a, int b, int p, int bsv, boolean bw) { int len = b-a; - + if(len < 32) { this.binaryInsertion(array, a, b); return; } - + Random rng = new Random(); - + int s = len; while(s >= 32) s = (s-1)/R + 1; - + int i = a+s, j = a+R*s, pEnd = p + (s+1)*(G+1)+G; this.binaryInsertion(array, a, i); for(int k = 0; k < s; k++) //scatter elements to make G sized gaps b/w them Writes.swap(array, a+k, p + k*(G+1)+G, 1, true, false); - + while(i < b) { if(i == j) { //rebalancing (retrieve from buffer & rescatter) this.retrieve(array, i, p, pEnd, bsv, bw); - + s = i-a; pEnd = p + (s+1)*(G+1)+G; j = a+(j-a)*R; - - for(int k = 0; k < s; k++) + + for(int k = 0; k < s; k++) Writes.swap(array, a+k, p + k*(G+1)+G, 1, true, false); } - + int bLoc = this.leftBlockSearch(array, p+G, pEnd-(G+1), array[i]); //search gap location - + if(Reads.compareValues(array[i], array[bLoc]) == 0) { //handle equal values to prevent worst case O(n^2) int eqEnd = this.rightBlockSearch(array, bLoc+(G+1), pEnd-(G+1), array[i]); //find the endpoint of the gaps with equal head element bLoc += rng.nextInt((eqEnd-bLoc)/(G+1))*(G+1); //choose a random gap from the range of gaps } - + int loc = this.rightBinSearch(array, bLoc-G, bLoc, bsv, bw); //search next empty space in gap - + if(loc == bLoc) { //if there is no empty space filled elements in gap are split - do bLoc += G+1; + do bLoc += G+1; while(bLoc < pEnd && this.rightBinSearch(array, bLoc-G, bLoc, bsv, bw) == bLoc); - - if(bLoc == pEnd) { //rebalancing + + if(bLoc == pEnd) { //rebalancing this.retrieve(array, i, p, pEnd, bsv, bw); - + s = i-a; pEnd = p + (s+1)*(G+1)+G; j = a+(j-a)*R; - - for(int k = 0; k < s; k++) + + for(int k = 0; k < s; k++) Writes.swap(array, a+k, p + k*(G+1)+G, 1, true, false); } else { //if a gap is full find next non full gap to the right & shift the space down int rotP = this.rightBinSearch(array, bLoc-G, bLoc, bsv, bw); int rotS = bLoc - Math.max(rotP, bLoc - G/2); //for odd G whether its floor or ceil(G/2) doesnt matter - this.shiftBW(array, loc-rotS, bLoc-rotS, bLoc); + this.shiftBW(array, loc-rotS, bLoc-rotS, bLoc); } } else { @@ -267,25 +267,25 @@ private void librarySort(int[] array, int a, int b, int p, int bsv, boolean bw) } this.retrieve(array, b, p, pEnd, bsv, bw); } - + @Override public void runSort(int[] array, int length, int bucketCount) { //to benefit from worst case O(n log n) comparisons & O(n) moves //we would normally shuffle the array before sorting //but for the sake of demonstration this step is omitted - int[] pa = new int[G+2]; + int[] pa = new int[G+2]; int[] heap = new int[G+2]; - + int alloc = pa.length + heap.length; Writes.changeAllocAmount(alloc); - + int a = 0, b = length; - + while(b-a >= 32) { int piv = array[this.medianOfThreeNinthers(array, a, b)]; - + //partition -> [a][E > piv][i][E == piv][j][E < piv][b] int i1 = a, i = a-1, j = b, j1 = b; - + for(;;) { while(++i < j) { int cmp = Reads.compareIndexValue(array, i, piv, 0.5, true); @@ -293,14 +293,14 @@ public void runSort(int[] array, int length, int bucketCount) { //to benefit fro else if(cmp < 0) break; } Highlights.clearMark(2); - + while(--j > i) { int cmp = Reads.compareIndexValue(array, j, piv, 0.5, true); if(cmp == 0) Writes.swap(array, --j1, j, 1, true, false); else if(cmp > 0) break; } Highlights.clearMark(2); - + if(i < j) { Writes.swap(array, i, j, 1, true, false); Highlights.clearMark(2); @@ -308,27 +308,27 @@ public void runSort(int[] array, int length, int bucketCount) { //to benefit fro else { if(i1 == b) return; else if(j < i) j++; - + while(i1 > a) Writes.swap(array, --i, --i1, 1, true, false); while(j1 < b) Writes.swap(array, j++, j1++, 1, true, false); - + break; } } - + int left = i-a, right = b-j, m, kCnt = 0; - + if(left <= right) { //sort the smaller partition using larger partition as space m = b-left; left = Math.max((right+1)/(G+1), 16); - + for(int k = a; k < i; k += left) { this.librarySort(array, k, Math.min(k+left, i), j, piv, true); Writes.write(pa, kCnt++, k, 0, false, true); } - + this.kWayMerge(array, heap, pa, left, i, m, kCnt); - + //swap items eq to pivot next to sorted area //eq items zone: [i][E == piv][j][E < piv][m][sorted area] if(j-i < m-j) { @@ -343,14 +343,14 @@ public void runSort(int[] array, int length, int bucketCount) { //to benefit fro else { m = a+right; right = Math.max((left+1)/(G+1), 16); - + for(int k = j; k < b; k += right) { this.librarySort(array, k, Math.min(k+right, b), a, piv, false); Writes.write(pa, kCnt++, k, 0, false, true); } - + this.kWayMerge(array, heap, pa, right, b, a, kCnt); - + //eq items zone: [sorted area][m][E > piv][i][E == piv][j] if(i-m < j-i) { while(m < i) Writes.swap(array, m++, --j, 1, true, false); @@ -365,4 +365,4 @@ public void runSort(int[] array, int length, int bucketCount) { //to benefit fro this.binaryInsertion(array, a, b); Writes.changeAllocAmount(-alloc); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/FluxSort.java b/src/main/java/sorts/hybrid/FluxSort.java index 0c058294..a273a7f4 100644 --- a/src/main/java/sorts/hybrid/FluxSort.java +++ b/src/main/java/sorts/hybrid/FluxSort.java @@ -26,7 +26,7 @@ a copy of this software and associated documentation files (the CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - + Ported to arrayV by mg-2018 and aphitorite, 2021 */ @@ -34,10 +34,10 @@ a copy of this software and associated documentation files (the fluxsort 1.1.3.3 */ -final public class FluxSort extends QuadSorting { +public final class FluxSort extends QuadSorting { public FluxSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Flux"); this.setRunAllSortsName("Flux Sort"); this.setRunSortName("Fluxsort"); @@ -49,38 +49,38 @@ public FluxSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private final int FLUX_OUT = 24; - + private boolean fluxAnalyze(int[] array, int nmemb) { int cnt, balance = 0; int pta; - + pta = 0; cnt = nmemb; - + while(--cnt > 0) if(Reads.compareIndices(array, pta, ++pta, 0.5, true) > 0) balance++; - + if(balance == 0) return false; - + if(balance == nmemb-1) { Writes.reversal(array, 0, nmemb-1, 1, true, false); return false; } - + if(balance <= nmemb / 6 || balance >= nmemb / 6 * 5) { this.quadSort(array, 0, nmemb); return false; } - + return true; } - + private int medianOfFive(int[] array, int v0, int v1, int v2, int v3, int v4) { int[] t = new int[4]; int val; - + val = (Reads.compareIndices(array, v0, v1, 1, true)+1)/2; t[0] = val; t[1] = val^1; val = (Reads.compareIndices(array, v0, v2, 1, true)+1)/2; t[0] += val; t[2] = val^1; val = (Reads.compareIndices(array, v0, v3, 1, true)+1)/2; t[0] += val; t[3] = val^1; @@ -103,7 +103,7 @@ private int medianOfFive(int[] array, int v0, int v1, int v2, int v3, int v4) { return t[3] == 2 ? v3 : v4; } - + private int medianOfThree(int[] array, int v0, int v1, int v2) { int[] t = new int[2]; int val; @@ -117,48 +117,48 @@ private int medianOfThree(int[] array, int v0, int v1, int v2) { return t[1] == 1 ? v1 : v2; } - + private int medianOfFifteen(int[] array, int ptx, int nmemb) { int v0, v1, v2, v3, v4, div = nmemb/16; - + v0 = this.medianOfThree(array, ptx+div*2, ptx+div*1, ptx+div*3); v1 = this.medianOfThree(array, ptx+div*5, ptx+div*4, ptx+div*6); v2 = this.medianOfThree(array, ptx+div*8, ptx+div*7, ptx+div*9); v3 = this.medianOfThree(array, ptx+div*11, ptx+div*10, ptx+div*12); v4 = this.medianOfThree(array, ptx+div*14, ptx+div*13, ptx+div*15); - + return this.medianOfFive(array, v2, v0, v1, v3, v4); } - + private int medianOfNine(int[] array, int ptx, int nmemb) { int v0, v1, v2, div = nmemb/16; - + v0 = this.medianOfThree(array, ptx+div*2, ptx+div*1, ptx+div*4); v1 = this.medianOfThree(array, ptx+div*8, ptx+div*6, ptx+div*10); v2 = this.medianOfThree(array, ptx+div*14, ptx+div*12, ptx+div*15); - + return this.medianOfThree(array, v0, v1, v2); } - + private void fluxPartition(int[] array, int[] swap, int[] main, int start, int nmemb) { int val; int aSize, sSize; int pta, pts, ptx, pte, piv; - + ptx = main == array ? start : 0; - + piv = main[nmemb > 1024 ? this.medianOfFifteen(main, ptx, nmemb) : this.medianOfNine(main, ptx, nmemb)]; - + pte = ptx+nmemb; - + pta = start; pts = 0; - + while(ptx < pte) { Highlights.markArray(3, ptx); val = (Reads.compareValues(main[ptx], piv)+1)/2; Delays.sleep(0.25); - + Writes.write(array, pta, main[ptx], 0.25, true, false); pta += val^1; Highlights.markArray(2, pts); Writes.write(swap, pts, main[ptx], 0.25, false, true); pts += val; @@ -166,10 +166,10 @@ private void fluxPartition(int[] array, int[] swap, int[] main, int start, int n ptx++; } Highlights.clearMark(3); - + sSize = pts; aSize = nmemb-sSize; - + if(aSize <= sSize/16 || sSize <= FLUX_OUT) { Writes.arraycopy(swap, 0, array, pta, sSize, 0.5, true, false); this.quadSortSwap(array, swap, pta, sSize); @@ -182,11 +182,11 @@ private void fluxPartition(int[] array, int[] swap, int[] main, int start, int n else this.fluxPartition(array, swap, array, start, aSize); } - + public void fluxsort(int[] array, int nmemb) { if(nmemb < 32) this.quadSort(array, 0, nmemb); - + else if(this.fluxAnalyze(array, nmemb)) { int[] swap = Writes.createExternalArray(nmemb); @@ -195,9 +195,9 @@ else if(this.fluxAnalyze(array, nmemb)) { Writes.deleteExternalArray(swap); } } - + @Override public void runSort(int[] array, int nmemb, int bucketCount) { this.fluxsort(array, nmemb); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/GrailSort.java b/src/main/java/sorts/hybrid/GrailSort.java index 96b6c473..06c8b66d 100644 --- a/src/main/java/sorts/hybrid/GrailSort.java +++ b/src/main/java/sorts/hybrid/GrailSort.java @@ -4,7 +4,7 @@ import sorts.templates.GrailSorting; /* - * + * The MIT License (MIT) Copyright (c) 2013 Andrey Astrelin @@ -47,10 +47,10 @@ this software and associated documentation files (the "Software"), to deal in /* */ /*********************************************************/ -final public class GrailSort extends GrailSorting { +public final class GrailSort extends GrailSorting { public GrailSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Grail"); //this.setRunAllID("Grail Sort (Block Merge Sort)"); //this.setRunAllSortsName("Grail Sort [Block Merge Sort]"); @@ -65,15 +65,15 @@ public GrailSort(ArrayVisualizer arrayVisualizer) { this.setBogoSort(false); this.setQuestion("Enter external buffer type (0 = in-place, 1 = static, 2 = dynamic):", 0); } - + public void rotateLength(int[] array, int leftLength, int rightLength) { this.grailRotate(array, 0, leftLength, rightLength); } - + public void customSort(int[] array, int start, int end) { this.grailCommonSort(array, start, end, null, 0, 0); } - + @Override public void runSort(int[] array, int length, int bucketCount) { switch(bucketCount) { @@ -82,7 +82,7 @@ public void runSort(int[] array, int length, int bucketCount) { this.grailCommonSort(array, 0, length, ExtBuf, 0, this.getStaticBuffer()); Writes.deleteExternalArray(ExtBuf); break; - + case 2: int tempLen = 1; while(tempLen * tempLen < length) tempLen *= 2; @@ -90,9 +90,9 @@ public void runSort(int[] array, int length, int bucketCount) { this.grailCommonSort(array, 0, length, DynExtBuf, 0, tempLen); Writes.deleteExternalArray(DynExtBuf); break; - + default: this.grailCommonSort(array, 0, length, null, 0, 0); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/HybridCombSort.java b/src/main/java/sorts/hybrid/HybridCombSort.java index a5876268..cf7d076e 100644 --- a/src/main/java/sorts/hybrid/HybridCombSort.java +++ b/src/main/java/sorts/hybrid/HybridCombSort.java @@ -4,7 +4,7 @@ import sorts.templates.CombSorting; /* - * + * The MIT License (MIT) Copyright (c) 2012 Daniel Imms, http://www.growingwiththeweb.com @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class HybridCombSort extends CombSorting { +public final class HybridCombSort extends CombSorting { public HybridCombSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Hybrid Comb"); this.setRunAllSortsName("Hybrid Comb Sort"); this.setRunSortName("Hybrid Combsort"); @@ -48,4 +48,4 @@ public HybridCombSort(ArrayVisualizer arrayVisualizer) { public void runSort(int[] array, int currentLength, int bucketCount) { this.combSort(array, currentLength, 1.3, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/ImprovedBlockSelectionSort.java b/src/main/java/sorts/hybrid/ImprovedBlockSelectionSort.java index 37d40b3c..99b862bb 100644 --- a/src/main/java/sorts/hybrid/ImprovedBlockSelectionSort.java +++ b/src/main/java/sorts/hybrid/ImprovedBlockSelectionSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ImprovedBlockSelectionSort extends Sort { +public final class ImprovedBlockSelectionSort extends Sort { public ImprovedBlockSelectionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,21 +44,21 @@ public ImprovedBlockSelectionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + public static int sqrt(int n) { int i = 1; for(; i*i < n; i*=2); return i; } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -74,71 +74,71 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private int inPlaceMerge(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > 0) { k = j; do k++; while(k < b && Reads.compareIndices(array, i, k, 0, false) > 0); - + this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } - + return i; } - + private void inPlaceMergeBW(int[] array, int a, int m, int b) { int i = m-1, j = b-1, k; - + while(j > i && i >= a){ if(Reads.compareValues(array[i], array[j]) > 0) { k = i; do k--; while(k >= a && Reads.compareIndices(array, k, j, 0, false) > 0); - + this.rotate(array, k+1, i+1, j+1); - + j -= i-k; i = k; - } + } else j--; } } - + private int selectRange(int[] array, int a, int b, int bLen) { int min = a; a += bLen; - + while(a < b) { int comp = Reads.compareIndices(array, a, min, 0, false); - + if(comp == -1 || (comp == 0 && Reads.compareIndices(array, a+bLen-1, min+bLen-1, 0, false) == -1)) min = a; - + a += bLen; } return min; } - + private void blockSelect(int[] array, int a, int m, int b, int bLen) { int k = a, j = m; - + while(k < m && Reads.compareIndices(array, k, m, 0.5, true) <= 0) k += bLen; if(k == m) return; - + int i = m; this.multiSwap(array, k, j, bLen); k += bLen; j += bLen; - + while(k < j && j < b) { if(Reads.compareIndices(array, i, j, 0.5, true) <= 0) { if(k != i) this.multiSwap(array, k, i, bLen); @@ -159,27 +159,27 @@ private void blockSelect(int[] array, int a, int m, int b, int bLen) { k += bLen; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int i, j = 1; j < length; j *= 2) { int bLen = sqrt(j), n = j, b = length-length%bLen; - + while(n > 16) { for(i = 0; i+j < b; i += 2*j) for(int k = i; k+n < Math.min(i+2*j, b); k += n) this.blockSelect(array, k, k+n, Math.min(k+2*n, b), bLen); - + n = bLen; bLen = sqrt(bLen); } - + for(i = 0; i+j < b; i += 2*j) for(int k = i, f = i; k+n < Math.min(i+2*j, b); k += n) f = this.inPlaceMerge(array, f, k+n, Math.min(k+2*n, b)); - + this.inPlaceMergeBW(array, length-length%(2*j), b, length); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/IntroCircleSortIterative.java b/src/main/java/sorts/hybrid/IntroCircleSortIterative.java index d1e71287..9fbeeff1 100644 --- a/src/main/java/sorts/hybrid/IntroCircleSortIterative.java +++ b/src/main/java/sorts/hybrid/IntroCircleSortIterative.java @@ -5,7 +5,7 @@ import sorts.templates.IterativeCircleSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -16,10 +16,10 @@ * */ -final public class IntroCircleSortIterative extends IterativeCircleSorting { +public final class IntroCircleSortIterative extends IterativeCircleSorting { public IntroCircleSortIterative(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Intro Circle (Iterative)"); this.setRunAllSortsName("Iterative Introspective Circle Sort"); this.setRunSortName("Iterative Introspective Circlesort"); @@ -37,13 +37,13 @@ public void runSort(int[] array, int length, int bucketCount) { this.end = length; int threshold = 0, n = 1; for(; n < length; n*=2, threshold++); - + threshold /= 2; int iterations = 0; - + do { iterations++; - + if(iterations >= threshold) { BinaryInsertionSort binaryInserter = new BinaryInsertionSort(this.arrayVisualizer); binaryInserter.customBinaryInsert(array, 0, length, 0.1); @@ -51,4 +51,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } while (this.circleSortRoutine(array, n, 1) != 0); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/IntroCircleSortRecursive.java b/src/main/java/sorts/hybrid/IntroCircleSortRecursive.java index 09edbefe..9165e972 100644 --- a/src/main/java/sorts/hybrid/IntroCircleSortRecursive.java +++ b/src/main/java/sorts/hybrid/IntroCircleSortRecursive.java @@ -5,7 +5,7 @@ import sorts.templates.CircleSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -16,10 +16,10 @@ * */ -final public class IntroCircleSortRecursive extends CircleSorting { +public final class IntroCircleSortRecursive extends CircleSorting { public IntroCircleSortRecursive(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Intro Circle (Recursive)"); this.setRunAllSortsName("Recursive Introspective Circle Sort"); this.setRunSortName("Introspective Circlesort"); @@ -37,13 +37,13 @@ public void runSort(int[] array, int length, int bucketCount) { this.end = length; int threshold = 0, n = 1; for(; n < length; n*=2, threshold++); - + threshold /= 2; int iterations = 0; - + do { iterations++; - + if(iterations >= threshold) { BinaryInsertionSort binaryInserter = new BinaryInsertionSort(this.arrayVisualizer); binaryInserter.customBinaryInsert(array, 0, length, 0.1); @@ -51,4 +51,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } while (this.circleSortRoutine(array, 0, n - 1, 0, 1) != 0); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/IntroSort.java b/src/main/java/sorts/hybrid/IntroSort.java index a51929ea..75033c67 100644 --- a/src/main/java/sorts/hybrid/IntroSort.java +++ b/src/main/java/sorts/hybrid/IntroSort.java @@ -10,7 +10,7 @@ // Modifications: Bernhard Pfahringer // changes include: local insertion sort, no global array -final public class IntroSort extends Sort { +public final class IntroSort extends Sort { private MaxHeapSort heapSorter; private int middle; @@ -140,4 +140,4 @@ public void runSort(int[] array, int length, int bucketCount) { InsertionSort sort = new InsertionSort(this.arrayVisualizer); sort.customInsertSort(array, 0, length, 0.5, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/KotaSort.java b/src/main/java/sorts/hybrid/KotaSort.java index be82b1e0..213db753 100644 --- a/src/main/java/sorts/hybrid/KotaSort.java +++ b/src/main/java/sorts/hybrid/KotaSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.KotaSorting; -final public class KotaSort extends KotaSorting { +public final class KotaSort extends KotaSorting { public KotaSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Kota"); //this.setRunAllID("Grail Sort (Block Merge Sort)"); //this.setRunAllSortsName("Grail Sort [Block Merge Sort]"); @@ -20,7 +20,7 @@ public KotaSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { if(Delays.getSleepRatio() == 55.1) { @@ -31,4 +31,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.kotaSort(array, 0, length); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/LazierestSort.java b/src/main/java/sorts/hybrid/LazierestSort.java index 0bf84750..31d1c611 100644 --- a/src/main/java/sorts/hybrid/LazierestSort.java +++ b/src/main/java/sorts/hybrid/LazierestSort.java @@ -5,7 +5,7 @@ import utils.IndexedRotations; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class LazierestSort extends Sort { +public final class LazierestSort extends Sort { public LazierestSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); this.setSortListName("Lazierest Stable"); @@ -44,35 +44,35 @@ public LazierestSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int ceilCbrt(int n) { int a = 0, b = Math.min(1291, n); - + while(a < b) { int m = (a+b)/2; - + if(m*m*m >= n) b = m; else a = m+1; } - + return a; } - + private void insertTo(int[] array, int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[--a], 0.5, true, false); Writes.write(array, b, temp, 0.5, true, false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -88,75 +88,75 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private int leftBinSearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; - else + else a = m+1; } - + return a; } private int rightBinSearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; - else + else a = m+1; } - + return a; } - + private int leftExpSearch(int[] array, int a, int b, int val) { int i = 1; while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) > 0) i *= 2; - + return this.leftBinSearch(array, a+i/2, Math.min(b, a-1+i), val); } private int rightExpSearch(int[] array, int a, int b, int val) { int i = 1; while(b-i >= a && Reads.compareValues(val, array[b-i]) < 0) i *= 2; - + return this.rightBinSearch(array, Math.max(a, b-i+1), b-i/2, val); } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, i, this.rightBinSearch(array, a, i, array[i])); } - + private void inPlaceMergeFW(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b){ if(Reads.compareValues(array[i], array[j]) == 1) { k = this.leftExpSearch(array, j+1, b, array[i]); IndexedRotations.cycleReverse(array, i, j, k, 0.75, true, false); - + i += k-j; j = k; - } + } else i++; } } private void inPlaceMergeBW(int[] array, int a, int m, int b) { int i = m-1, j = b-1, k; - + while(j > i && i >= a){ if(Reads.compareValues(array[i], array[j]) > 0) { k = this.rightExpSearch(array, a, i, array[j]); this.rotate(array, k, i+1, j+1); - + j -= (i+1)-k; i = k-1; - } + } else j--; } } @@ -164,32 +164,32 @@ private void inPlaceMerge(int[] array, int a, int m, int b) { if(b-m < m-a) this.inPlaceMergeBW(array, a, m, b); else this.inPlaceMergeFW(array, a, m, b); } - + private void fragmentedMerge(int[] array, int a, int m, int b, int s) { int i = a+(m-a)%s; - + while(i < m) { int j = this.leftExpSearch(array, m, b, array[i]); IndexedRotations.cycleReverse(array, i, m, j, 0.75, true, false); - + int rLen = j-m; j = i; i += rLen; m += rLen; - + this.inPlaceMerge(array, a, j, i); a = i; i += s; } this.inPlaceMerge(array, Math.max(a, i-s), i, b); } - + public void lazierestStableSort(int[] array, int a, int b) { int len = b-a, s = this.ceilCbrt(len), s1 = s*s; - + for(int i = len%s; i <= b; i += s) this.binaryInsertion(array, Math.max(a, i-s), i); - + for(int i = b-s, j = b; i > a; i -= s) { if(j-i == s1) { j -= s1; @@ -197,14 +197,14 @@ public void lazierestStableSort(int[] array, int a, int b) { } this.inPlaceMergeFW(array, Math.max(a, i-s), i, j); } - + for(int i = b-s1; i > a; i -= s1) this.fragmentedMerge(array, Math.max(a, i-s1), i, b, s); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { if(currentLength <= 16) this.binaryInsertion(array, 0, currentLength); else this.lazierestStableSort(array, 0, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/LaziestSort.java b/src/main/java/sorts/hybrid/LaziestSort.java index 6468fc4a..19590800 100644 --- a/src/main/java/sorts/hybrid/LaziestSort.java +++ b/src/main/java/sorts/hybrid/LaziestSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class LaziestSort extends Sort { +public final class LaziestSort extends Sort { public LaziestSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); this.setSortListName("Laziest Stable"); @@ -43,22 +43,22 @@ public LaziestSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void insertTo(int[] array, int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[--a], 0.5, true, false); Writes.write(array, b, temp, 0.5, true, false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -74,81 +74,81 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private int leftBinSearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; - else + else a = m+1; } - + return a; } - + private int rightBinSearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; - else + else a = m+1; } - + return a; } - + private int leftExpSearch(int[] array, int a, int b, int val) { int i = 1; while(a-1+i < b && Reads.compareValues(val, array[a-1+i]) > 0) i *= 2; - + return this.leftBinSearch(array, a+i/2, Math.min(b, a-1+i), val); } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, i, this.rightBinSearch(array, a, i, array[i])); } - + private void inPlaceMerge(int[] array, int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b){ if(Reads.compareValues(array[i], array[j]) == 1) { k = this.leftExpSearch(array, j+1, b, array[i]); this.rotate(array, i, j, k); - + i += k-j; j = k; - } + } else i++; } } - + protected void laziestStableSort(int[] array, int start, int end) { int len = end - start; if(len <= 16) { this.binaryInsertion(array, start, end); return; } - + int i, blockLen = Math.max(16, (int)Math.sqrt(len)); for(i = start; i+2*blockLen < end; i+=blockLen) { this.binaryInsertion(array, i, i+blockLen); } this.binaryInsertion(array, i, end); - - while(i-blockLen >= start) { + + while(i-blockLen >= start) { this.inPlaceMerge(array, i-blockLen, i, end); i-=blockLen; } } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.laziestStableSort(array, 0, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/MedianMergeSort.java b/src/main/java/sorts/hybrid/MedianMergeSort.java index f197d118..58fef81e 100644 --- a/src/main/java/sorts/hybrid/MedianMergeSort.java +++ b/src/main/java/sorts/hybrid/MedianMergeSort.java @@ -7,7 +7,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 yuji, implemented by aphitorite @@ -32,10 +32,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class MedianMergeSort extends Sort { +public final class MedianMergeSort extends Sort { public MedianMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Median Merge"); this.setRunAllSortsName("Median Mergesort"); this.setRunSortName("Median Mergesort"); @@ -47,31 +47,31 @@ public MedianMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private InsertionSort insSort; private BinaryInsertionSort binInsSort; - + private void medianOfThree(int[] array, int a, int b) { int m = a+(b-1-a)/2; - + if(Reads.compareIndices(array, a, m, 1, true) == 1) Writes.swap(array, a, m, 1, true, false); - + if(Reads.compareIndices(array, m, b-1, 1, true) == 1) { Writes.swap(array, m, b-1, 1, true, false); - + if(Reads.compareIndices(array, a, m, 1, true) == 1) return; } - + Writes.swap(array, a, m, 1, true, false); } - + //lite version private void medianOfMedians(int[] array, int a, int b, int s) { int end = b, start = a, i, j; boolean ad = true; - + while(end - start > 1) { j = start; Highlights.markArray(2, j); @@ -89,12 +89,12 @@ private void medianOfMedians(int[] array, int a, int b, int s) { end = j; } } - + public int partition(int[] array, int a, int b, int p) { int i = a - 1; int j = b; Highlights.markArray(3, p); - + while(true) { do { i++; @@ -102,48 +102,48 @@ public int partition(int[] array, int a, int b, int p) { Delays.sleep(0.5); } while(i < j && Reads.compareIndices(array, i, p, 0, false) == -1); - + do { j--; Highlights.markArray(2, j); Delays.sleep(0.5); } while(j >= i && Reads.compareIndices(array, j, p, 0, false) == 1); - + if(i < j) Writes.swap(array, i, j, 1, true, false); else return j; } } - + private void merge(int[] array, int a, int m, int b, int p) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareIndices(array, i, j, 0, false) <= 0) Writes.swap(array, p++, i++, 1, true, false); - else + else Writes.swap(array, p++, j++, 1, true, false); } - + while(i < m) Writes.swap(array, p++, i++, 1, true, false); while(j < b) Writes.swap(array, p++, j++, 1, true, false); } - + public static int getMinLevel(int n) { while(n >= 32) n = (n+3)/4; return n; } - + private void mergeSort(int[] array, int a, int b, int p) { int length = b-a; if(length < 2) return; - + int i, pos, j = getMinLevel(length); - + for(i = a; i+j <= b; i+=j) this.binInsSort.customBinaryInsert(array, i, i+j, 0.25); this.binInsSort.customBinaryInsert(array, i, b, 0.25); - + while(j < length) { pos = p; for(i = a; i+2*j <= b; i+=2*j, pos+=2*j) @@ -152,9 +152,9 @@ private void mergeSort(int[] array, int a, int b, int p) { this.merge(array, i, i+j, b, pos); else while(i < b) Writes.swap(array, i++, pos++, 1, true, false); - + j *= 2; - + pos = a; for(i = p; i+2*j <= p+length; i+=2*j, pos+=2*j) this.merge(array, i, i+j, i+2*j, pos); @@ -162,29 +162,29 @@ private void mergeSort(int[] array, int a, int b, int p) { this.merge(array, i, i+j, p+length, pos); else while(i < p+length) Writes.swap(array, i++, pos++, 1, true, false); - + j *= 2; } } - + private void medianMergeSort(int[] array, int a, int b) { int start = a, end = b; boolean badPartition = false, mom = false; - + while(end - start > 16) { if(badPartition) { this.medianOfMedians(array, start, end, 5); mom = true; } else this.medianOfThree(array, start, end); - + int p = this.partition(array, start+1, end, start); Writes.swap(array, start, p, 1, true, false); - + int left = p-start; int right = end-(p+1); badPartition = !mom && ((left == 0 || right == 0) || (left/right >= 16 || right/left >= 16)); - + if(left <= right) { this.mergeSort(array, start, p, p+1); start = p+1; @@ -196,11 +196,11 @@ private void medianMergeSort(int[] array, int a, int b) { } this.binInsSort.customBinaryInsert(array, start, end, 0.25); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.insSort = new InsertionSort(this.arrayVisualizer); this.binInsSort = new BinaryInsertionSort(this.arrayVisualizer); this.medianMergeSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/MergeInsertionSort.java b/src/main/java/sorts/hybrid/MergeInsertionSort.java index c90b4407..694f1349 100644 --- a/src/main/java/sorts/hybrid/MergeInsertionSort.java +++ b/src/main/java/sorts/hybrid/MergeInsertionSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class MergeInsertionSort extends Sort { +public final class MergeInsertionSort extends Sort { public MergeInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,18 +44,18 @@ public MergeInsertionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void blockSwap(int[] array, int a, int b, int s, double sleep) { while(s-- > 0) Writes.swap(array, a--, b--, sleep, true, false); } - + private void blockInsert(int[] array, int a, int b, int s, double sleep) { while(a-s >= b) { this.blockSwap(array, a-s, a, s, sleep); a -= s; } } - + private void blockReversal(int[] array, int a, int b, int s, double sleep) { b -= s; while(b > a) { @@ -64,28 +64,28 @@ private void blockReversal(int[] array, int a, int b, int s, double sleep) { b -= s; } } - + private int blockSearch(int[] array, int a, int b, int s, int val) { while(a < b) { int m = a+(((b-a)/s)/2)*s; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; else a = m+s; } - + return a; } - + private void order(int[] array, int a, int b, int s, double sleep) { for(int i = a, j = i+s; j < b; i+=s, j+=2*s) this.blockInsert(array, j, i, s, sleep); - + int m = a+(((b-a)/s)/2)*s; this.blockReversal(array, m, b, s, 1); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int k = 1; @@ -93,23 +93,23 @@ public void runSort(int[] array, int length, int bucketCount) { for(int i = 2*k-1; i < length; i+=2*k) if(Reads.compareValues(array[i-k], array[i]) > 0) this.blockSwap(array, i-k, i, k, 1); - + k *= 2; } - + double delay = 12; while(k > 0) { int a = k-1, i = a+2*k, g = 2, p = 4; double sleep = Math.min(1, delay); - + while(i+2*k*g-k <= length) { this.order(array, i, i+2*k*g-k, k, sleep); int b = a+k*(p-1); - + i += k*g-k; for(int j = i; j < i+k*g; j+=k) this.blockInsert(array, j, this.blockSearch(array, a, b, k, array[j]), k, sleep); - + i += k*g+k; g = p-g; p *= 2; @@ -118,9 +118,9 @@ public void runSort(int[] array, int length, int bucketCount) { this.blockInsert(array, i, this.blockSearch(array, a, i, k, array[i]), k, sleep); i += 2*k; } - + k /= 2; delay /= 2; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/OptimizedBottomUpMergeSort.java b/src/main/java/sorts/hybrid/OptimizedBottomUpMergeSort.java index f04871c5..696dc02d 100644 --- a/src/main/java/sorts/hybrid/OptimizedBottomUpMergeSort.java +++ b/src/main/java/sorts/hybrid/OptimizedBottomUpMergeSort.java @@ -4,10 +4,10 @@ import sorts.insert.BinaryInsertionSort; import sorts.templates.Sort; -final public class OptimizedBottomUpMergeSort extends Sort { +public final class OptimizedBottomUpMergeSort extends Sort { public OptimizedBottomUpMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Bottom-Up Merge"); //this.setRunAllID("Bottom-Up Merge Sort w/ Binary Insert (std::stable_sort)"); this.setRunAllSortsName("Optimized Bottom-Up Merge Sort [std::stable_sort]"); @@ -20,7 +20,7 @@ public OptimizedBottomUpMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + /** * Iterative merge sort algorithm --- as a static method @@ -99,7 +99,7 @@ private void mergePass (int[] x, int[] y, int s, int n, boolean activeSound) Writes.write(y, j, x[j], 1, false, activeSound); // copy last segment to y Highlights.markArray(1, j); Highlights.clearMark(2); - } + } }// end mergePass() @@ -109,12 +109,12 @@ private void mergePass (int[] x, int[] y, int s, int n, boolean activeSound) */ private void stableSort (int[] a, int n) { BinaryInsertionSort binaryInserter = new BinaryInsertionSort(this.arrayVisualizer); - + if(n < 16) { binaryInserter.customBinaryInsert(a, 0, 16, 0.35); return; } - + // Sort a[0:n-1] using merge sort. int s = 16; // segment size int[] b = Writes.createExternalArray(n); @@ -137,9 +137,9 @@ private void stableSort (int[] a, int n) { Writes.deleteExternalArray(b); }// end MergeArray class - + @Override public void runSort(int[] array, int length, int bucketCount) { this.stableSort(array, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/OptimizedDualPivotQuickSort.java b/src/main/java/sorts/hybrid/OptimizedDualPivotQuickSort.java index 77a4f5cd..8cbc4600 100644 --- a/src/main/java/sorts/hybrid/OptimizedDualPivotQuickSort.java +++ b/src/main/java/sorts/hybrid/OptimizedDualPivotQuickSort.java @@ -7,12 +7,12 @@ // code retrieved from https://codeblab.com/wp-content/uploads/2009/09/DualPivotQuicksort.pdf // written by Vladimir Yaroslavskiy -final public class OptimizedDualPivotQuickSort extends Sort { +public final class OptimizedDualPivotQuickSort extends Sort { private InsertionSort insertSorter; - + public OptimizedDualPivotQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Dual-Pivot Quick"); //this.setRunAllID("Optimized Dual-Pivot Quick Sort"); this.setRunAllSortsName("Optimized Dual-Pivot Quick Sort [Arrays.sort]"); @@ -25,23 +25,23 @@ public OptimizedDualPivotQuickSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void dualPivot(int[] array, int left, int right, int divisor) { int length = right - left; - + // insertion sort for tiny array if(length < 27) { Highlights.clearMark(2); insertSorter.customInsertSort(array, left, right + 1, 0.333, false); return; } - + int third = length / divisor; - + // "medians" int med1 = left + third; int med2 = right - third; - + if(med1 <= left) { med1 = left + 1; } @@ -56,18 +56,18 @@ private void dualPivot(int[] array, int left, int right, int divisor) { Writes.swap(array, med1, right, 1, true, false); Writes.swap(array, med2, left, 1, true, false); } - + // pivots int pivot1 = array[left]; int pivot2 = array[right]; - + // pointers int less = left + 1; int great = right - 1; - + Highlights.markArray(2, less); Highlights.markArray(3, great); - + // sorting for(int k = less; k <= great; k++) { if(Reads.compareIndexValue(array, k, pivot1, 0.5, true) == -1) { @@ -82,7 +82,7 @@ else if(Reads.compareIndexValue(array, k, pivot2, 0.5, true) == 1) { } Writes.swap(array, k, great--, 0.5, false, false); Highlights.markArray(3, great); - + if(Reads.compareIndexValue(array, k, pivot1, 0.5, true) == -1) { Writes.swap(array, k, less++, 0.5, false, false); Highlights.markArray(2, less); @@ -90,23 +90,23 @@ else if(Reads.compareIndexValue(array, k, pivot2, 0.5, true) == 1) { } } Highlights.clearAllMarks(); - + // swaps int dist = great - less; - + if(dist < 13) { divisor++; } Writes.swap(array, less - 1, left, 1, true, false); Writes.swap(array, great + 1, right, 1, true, false); - + // subarrays this.dualPivot(array, left, less - 2, divisor); this.dualPivot(array, great + 2, right, divisor); - + Highlights.markArray(2, less); Highlights.markArray(3, great); - + // equal elements if(dist > length - 13 && pivot1 != pivot2) { for(int k = less; k <= great; k++) { @@ -117,7 +117,7 @@ else if(Reads.compareIndexValue(array, k, pivot2, 0.5, true) == 1) { else if(Reads.compareIndexValue(array, k, pivot2, 0.5, true) == 0) { Writes.swap(array, k, great--, 0.5, false, false); Highlights.markArray(3, great); - + if(Reads.compareIndexValue(array, k, pivot1, 0.5, true) == 0) { Writes.swap(array, k, less++, 0.5, false, false); Highlights.markArray(2, less); @@ -126,7 +126,7 @@ else if(Reads.compareIndexValue(array, k, pivot2, 0.5, true) == 0) { } } Highlights.clearAllMarks(); - + // subarray if(pivot1 < pivot2) { this.dualPivot(array, less, great, divisor); @@ -138,4 +138,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { this.insertSorter = new InsertionSort(this.arrayVisualizer); this.dualPivot(array, 0, currentLength - 1, 3); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/OptimizedLazyStableSort.java b/src/main/java/sorts/hybrid/OptimizedLazyStableSort.java index 288bdb1f..50913ea6 100644 --- a/src/main/java/sorts/hybrid/OptimizedLazyStableSort.java +++ b/src/main/java/sorts/hybrid/OptimizedLazyStableSort.java @@ -38,7 +38,7 @@ this software and associated documentation files (the "Software"), to deal in /* */ /*****************************************/ -final public class OptimizedLazyStableSort extends GrailSorting { +public final class OptimizedLazyStableSort extends GrailSorting { public OptimizedLazyStableSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -104,4 +104,4 @@ protected void grailLazyStableSort(int[] arr, int pos, int len) { public void runSort(int[] array, int length, int bucketCount) { this.grailLazyStableSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/OptimizedRotateMergeSort.java b/src/main/java/sorts/hybrid/OptimizedRotateMergeSort.java index 40c4059d..dd773fde 100644 --- a/src/main/java/sorts/hybrid/OptimizedRotateMergeSort.java +++ b/src/main/java/sorts/hybrid/OptimizedRotateMergeSort.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class OptimizedRotateMergeSort extends Sort { +public final class OptimizedRotateMergeSort extends Sort { final int MIN_RUN = 32; int[] tmp; diff --git a/src/main/java/sorts/hybrid/OptimizedWeaveMergeSort.java b/src/main/java/sorts/hybrid/OptimizedWeaveMergeSort.java index 0616fc01..d01ddefe 100644 --- a/src/main/java/sorts/hybrid/OptimizedWeaveMergeSort.java +++ b/src/main/java/sorts/hybrid/OptimizedWeaveMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class OptimizedWeaveMergeSort extends Sort { +public final class OptimizedWeaveMergeSort extends Sort { public OptimizedWeaveMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized Weave Merge"); this.setRunAllSortsName("Optimized Weave Merge Sort"); this.setRunSortName("Optimized Weave Mergesort"); @@ -44,21 +44,21 @@ public OptimizedWeaveMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void insertTo(int[] array, int a, int b) { int temp = array[a]; while(a > b) Writes.write(array, a, array[--a], 0.25, true, false); Writes.write(array, b, temp, 0.25, true, false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -74,31 +74,31 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + //pow of 2 only (O(n)) private void bitReversal(int[] array, int a, int b) { int len = b-a, m = 0; int d1 = len>>1, d2 = d1+(d1>>1); - + for(int i = 1; i < len-1; i++) { int j = d1; - + for( - int k = i, n = d2; - (k&1) == 0; + int k = i, n = d2; + (k&1) == 0; j -= n, k >>= 1, n >>= 1 ); m += j; if(m > i) Writes.swap(array, a+i, a+m, 1, true, false); } } - + private void weaveInsert(int[] array, int a, int b, boolean right) { int i = a, j = i+1; - + while(j < b) { while(i < j && Reads.compareIndices(array, i, j, 0, true) < (right ? 1 : 0)) i++; - + if(i == j) { right = !right; j++; @@ -110,16 +110,16 @@ private void weaveInsert(int[] array, int a, int b, boolean right) { } Highlights.clearMark(3); } - + //000111 -> 010101 T //00011 -> 01010 T //00111 -> 10101 F private void weaveMerge(int[] array, int a, int m, int b) { if(b-a < 2) return; - + int a1 = a, b1 = b; boolean right = true; - + if((b-a)%2 == 1) { if(m-a < b-m) { a1--; @@ -127,15 +127,15 @@ private void weaveMerge(int[] array, int a, int m, int b) { } else b1++; } - + for(int e = b1, f; e-a1 > 2; e = f) { m = (a1+e)/2; int p = 1 << (int)(Math.log(m-a1)/Math.log(2)); - + this.rotate(array, m-p, m, e-p); m = e-p; f = m-p; - + this.bitReversal(array, f, m); this.bitReversal(array, m, e); this.bitReversal(array, f, e); @@ -147,10 +147,10 @@ private void weaveMerge(int[] array, int a, int m, int b) { @Override public void runSort(int[] array, int currentLength, int bucketCount) { int n = currentLength, d = 1 << (int)(Math.log(n-1)/Math.log(2) + 1); - + while(d > 1) { int i = 0, dec = 0; - + while(i < n) { int j = i; dec += n; @@ -170,4 +170,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { d /= 2; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/PDQBranchedSort.java b/src/main/java/sorts/hybrid/PDQBranchedSort.java index 505a883e..2a11130c 100644 --- a/src/main/java/sorts/hybrid/PDQBranchedSort.java +++ b/src/main/java/sorts/hybrid/PDQBranchedSort.java @@ -5,7 +5,7 @@ import sorts.templates.PDQSorting; /* - * + * pdqsort.h - Pattern-defeating quicksort. Copyright (c) 2015 Orson Peters This software is provided 'as-is', without any express or implied warranty. In no event will the @@ -21,10 +21,10 @@ * */ -final public class PDQBranchedSort extends PDQSorting { +public final class PDQBranchedSort extends PDQSorting { public PDQBranchedSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Pattern-Defeating Quick (PDQ)"); this.setRunAllSortsName("Pattern-Defeating Quick Sort"); this.setRunSortName("Pattern-Defeating Quicksort"); @@ -36,15 +36,15 @@ public PDQBranchedSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + public void customSort(int[] array, int low, int high) { this.newHeapSorter(new MaxHeapSort(this.arrayVisualizer)); pdqLoop(array, low, high, false, pdqLog(high - low)); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.newHeapSorter(new MaxHeapSort(this.arrayVisualizer)); pdqLoop(array, 0, currentLength, false, pdqLog(currentLength)); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/PDQBranchlessSort.java b/src/main/java/sorts/hybrid/PDQBranchlessSort.java index cb2e3d76..863b884c 100644 --- a/src/main/java/sorts/hybrid/PDQBranchlessSort.java +++ b/src/main/java/sorts/hybrid/PDQBranchlessSort.java @@ -5,7 +5,7 @@ import sorts.templates.PDQSorting; /* - * + * pdqsort.h - Pattern-defeating quicksort. Copyright (c) 2015 Orson Peters This software is provided 'as-is', without any express or implied warranty. In no event will the @@ -21,10 +21,10 @@ * */ -final public class PDQBranchlessSort extends PDQSorting { +public final class PDQBranchlessSort extends PDQSorting { public PDQBranchlessSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Pattern-Defeating Quick (Branchless PDQ)"); this.setRunAllSortsName("Branchless Pattern-Defeating Quick Sort"); this.setRunSortName("Branchless Pattern-Defeating Quicksort"); @@ -36,7 +36,7 @@ public PDQBranchlessSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { visualizeAux(); @@ -44,4 +44,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { pdqLoop(array, 0, currentLength, true, pdqLog(currentLength)); deleteAux(); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/ParallelBlockMergeSort.java b/src/main/java/sorts/hybrid/ParallelBlockMergeSort.java index 3570431d..51613644 100644 --- a/src/main/java/sorts/hybrid/ParallelBlockMergeSort.java +++ b/src/main/java/sorts/hybrid/ParallelBlockMergeSort.java @@ -5,7 +5,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -30,10 +30,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ParallelBlockMergeSort extends Sort { +public final class ParallelBlockMergeSort extends Sort { public ParallelBlockMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Parallel Block Merge"); this.setRunAllSortsName("Parallel Block Merge Sort"); this.setRunSortName("Parallel Block Mergesort"); @@ -45,12 +45,12 @@ public ParallelBlockMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; - + private class BlockMergeSort extends Thread { private int a, b; - + BlockMergeSort(int a, int b) { this.a = a; this.b = b; @@ -59,28 +59,28 @@ public void run() { ParallelBlockMergeSort.this.blockMergeSort(a, b); } } - + private int sqrt(int n) { int a = 0, b = Math.min(46341, n); - + while(a < b) { int m = (a+b)/2; - + if(m*m >= n) b = m; else a = m+1; } - + return a; } - + private void multiSwap(int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(m-r, m, r); @@ -96,49 +96,49 @@ private void rotate(int a, int m, int b) { } } } - + private void inPlaceMergeFW(int a, int m, int b) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > 0) { k = j; while(++k < b && Reads.compareIndices(array, i, k, 0, false) > 0); - + this.rotate(i, j, k); - + i += k-j; j = k; - } + } else i++; } } private void inPlaceMergeBW(int a, int m, int b) { int i = m-1, j = b-1, k; - + while(j > i && i >= a){ if(Reads.compareValues(array[i], array[j]) > 0) { k = i; while(--k >= a && Reads.compareIndices(array, k, j, 0, false) > 0); - + this.rotate(k+1, i+1, j+1); - + j -= i-k; i = k; - } + } else j--; } } - + private void mergeFW(int p, int a, int m, int b) { int len2 = m-a, pEnd = p+len2; - + this.multiSwap(p, a, len2); - + while(p < pEnd && m < b) { if(Reads.compareValues(array[p], array[m]) <= 0) Writes.swap(array, a++, p++, 1, true, false); - + else Writes.swap(array, a++, m++, 1, true, false); } while(p < pEnd) @@ -146,87 +146,87 @@ private void mergeFW(int p, int a, int m, int b) { } private void mergeBW(int p, int a, int m, int b) { int len2 = b-m, pEnd = p+len2-1; - + this.multiSwap(p, m, len2); - + m--; while(pEnd >= p && m >= a) { if(Reads.compareValues(array[pEnd], array[m]) >= 0) Writes.swap(array, --b, pEnd--, 1, true, false); - + else Writes.swap(array, --b, m--, 1, true, false); } while(pEnd >= p) Writes.swap(array, --b, pEnd--, 1, true, false); } - + private int findKeys(int a, int b, int n) { int p = a, found = 1, pEnd = p+found, i = pEnd; - + while(found < n) { while(i < b && Reads.compareIndices(array, pEnd-1, i, 1, true) == 0) i++; if(i == b) break; - + this.rotate(p, pEnd, i); - + p += i-pEnd; pEnd = ++i; - + found++; } this.rotate(a, p, pEnd); - + return found; } - + private int selectMin(int a, int b, int bLen) { int min = a; for(int i = min+bLen; i < b; i += bLen) if(Reads.compareValues(array[i], array[min]) < 0) min = i; - + return min; } - + private void blockMerge(int a, int m, int b) { if(Reads.compareValues(array[m-1], array[m]) <= 0) return; - + else if(Reads.compareValues(array[a], array[b-1]) > 0) { this.rotate(a, m, b); return; } - + int len1 = m-a; - + int bLen = this.sqrt(len1); int tLen = len1/bLen; int idl = bLen+tLen; - + int keys = this.findKeys(a, m, idl); int a1 = a+keys; len1 -= keys; - + if(keys == idl) { int b1 = b-(b-m)%bLen; - + int t = a; int p = a+tLen; - + int i = a1+(len1-1)%bLen+1; - + for(int j = i, k = t; j < m; j += bLen, k++) Writes.swap(array, j, k, 10, true, false); - + while(i < m && m < b1) { if(Reads.compareValues(array[i-1], array[m+bLen-1]) > 0) { this.multiSwap(i, m, bLen); this.mergeBW(p, a1, i, i+bLen); - + m += bLen; } else { int min = this.selectMin(i, m, bLen); - + if(min != i) this.multiSwap(i, min, bLen); Writes.swap(array, t++, i, 1, true, false); } @@ -235,13 +235,13 @@ else if(Reads.compareValues(array[a], array[b-1]) > 0) { if(i < m) { do { int min = this.selectMin(i, m, bLen); - + this.multiSwap(i, min, bLen); Writes.swap(array, t++, i, 1, true, false); i += bLen; } while(i < m); - + this.mergeBW(p, a1, b1, b); } else { @@ -252,7 +252,7 @@ else if(Reads.compareValues(array[a], array[b-1]) > 0) { if(m == b1) this.mergeBW(p, a1, b1, b); else this.mergeFW(p, m-bLen+1, m, b); } - + BinaryInsertionSort insertion = new BinaryInsertionSort(this.arrayVisualizer); insertion.customBinaryInsert(array, p, a1, 0.5); this.inPlaceMergeFW(a, a+keys, b); @@ -260,23 +260,23 @@ else if(Reads.compareValues(array[a], array[b-1]) > 0) { else if(keys > 1) { bLen = (len1-1)/keys+1; int b1 = b-(b-m)%bLen; - + int t = a; int i = a1+(len1-1)%bLen+1; - + for(int j = i, k = t; j < m; j += bLen, k++) Writes.swap(array, j, k, 10, true, false); - + while(i < m && m < b1) { if(Reads.compareValues(array[i-1], array[m+bLen-1]) > 0) { this.multiSwap(i, m, bLen); this.inPlaceMergeBW(a1, i, i+bLen); - + m += bLen; } else { int min = this.selectMin(i, m, bLen); - + if(min != i) this.multiSwap(i, min, bLen); Writes.swap(array, t++, i, 1, true, false); } @@ -285,13 +285,13 @@ else if(keys > 1) { if(i < m) { do { int min = this.selectMin(i, m, bLen); - + this.multiSwap(i, min, bLen); Writes.swap(array, t++, i, 1, true, false); i += bLen; } while(i < m); - + this.inPlaceMergeBW(a1, b1, b); } else { @@ -306,36 +306,36 @@ else if(keys > 1) { } else this.inPlaceMergeFW(a, m, b); } - + private void blockMergeSort(int a, int b) { if(b-a < 32) { BinaryInsertionSort insertion = new BinaryInsertionSort(this.arrayVisualizer); insertion.customBinaryInsert(array, a, b, 0.5); - + return; } - + int m = a+(b-a)/2; - + BlockMergeSort left = new BlockMergeSort(a, m); BlockMergeSort right = new BlockMergeSort(m, b); left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - + this.blockMerge(a, m, b); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.blockMergeSort(0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/ParallelGrailSort.java b/src/main/java/sorts/hybrid/ParallelGrailSort.java index 2c3d8c2f..f78800a7 100644 --- a/src/main/java/sorts/hybrid/ParallelGrailSort.java +++ b/src/main/java/sorts/hybrid/ParallelGrailSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ParallelGrailSort extends Sort { +public final class ParallelGrailSort extends Sort { public ParallelGrailSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Parallel Grail"); this.setRunAllSortsName("Parallel Grail Sort"); this.setRunSortName("Parallel Grailsort"); @@ -44,12 +44,12 @@ public ParallelGrailSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; - + private class GrailCommonSort extends Thread { private int a, b, nKeys; - + GrailCommonSort(int a, int b, int nKeys) { this.a = a; this.b = b; @@ -59,10 +59,10 @@ public void run() { ParallelGrailSort.this.grailCommonSort(a, b, nKeys); } } - + private class LazyStableSort extends Thread { private int a, b; - + LazyStableSort(int a, int b) { this.a = a; this.b = b; @@ -71,10 +71,10 @@ public void run() { ParallelGrailSort.this.lazyStableSort(a, b); } } - + private class RedistFW extends Thread { private int a, m, b; - + RedistFW(int a, int m, int b) { this.a = a; this.m = m; @@ -86,7 +86,7 @@ public void run() { } private class RedistBW extends Thread { private int a, m, b; - + RedistBW(int a, int m, int b) { this.a = a; this.m = m; @@ -96,35 +96,35 @@ public void run() { ParallelGrailSort.this.redistBW(a, m, b); } } - + private int sqrt(int n) { int a = 0, b = Math.min(46341, n); - + while(a < b) { int m = (a+b)/2; - + if(m*m >= n) b = m; else a = m+1; } - + return a; } - + private void shiftFW(int a, int m, int b) { while(m < b) Writes.swap(array, a++, m++, 1, true, false); } private void shiftBW(int a, int m, int b) { while(m > a) Writes.swap(array, --b, --m, 1, true, false); } - + private void multiSwap(int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(m-r, m, r); @@ -140,95 +140,95 @@ private void rotate(int a, int m, int b) { } } } - + private void insertTo(int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[--a], 0.5, true, false); Writes.write(array, b, temp, 0.5, true, false); } - + private int leftBinSearch(int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; - else + else a = m+1; } - + return a; } private int rightBinSearch(int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; - else + else a = m+1; } - + return a; } - + private void binaryInsertion(int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(i, this.rightBinSearch(a, i, array[i])); } - + private int mergeFW(int p, int a, int m, int b, boolean fwEq) { int i = a, j = m; - + while(i < m && j < b) { if(Reads.compareValues(array[i], array[j]) < (fwEq ? 1 : 0)) Writes.swap(array, p++, i++, 1, true, false); - + else Writes.swap(array, p++, j++, 1, true, false); } - + int f = i < m ? i : j; if(i < m && p < i) this.shiftFW(p, i, m); - + return f; } - + private int inPlaceMergeFW(int a, int m, int b, boolean fwEq) { int i = a, j = m, k; - + while(i < j && j < b) { if(Reads.compareValues(array[i], array[j]) > (fwEq ? 0 : -1)) { k = fwEq ? this.leftBinSearch(j+1, b, array[i]) : this.rightBinSearch(j+1, b, array[i]); - + this.rotate(i, j, k); - + i += k-j; j = k; - } + } else i++; } - + return i; } private void inPlaceMergeBW(int a, int m, int b, boolean fwEq) { int i = m-1, j = b-1, k; - + while(j > i && i >= a){ if(Reads.compareValues(array[i], array[j]) > (fwEq ? 0 : -1)) { k = fwEq ? this.rightBinSearch(a, i, array[j]) : this.leftBinSearch(a, i, array[j]); - + this.rotate(k, i+1, j+1); - + j -= (i+1)-k; i = k-1; - } + } else j--; } } - + private int findKeys(int a, int b, int n) { int p = a, nKeys = 1, pEnd = a+nKeys; @@ -237,14 +237,14 @@ private int findKeys(int a, int b, int n) { Highlights.markArray(1, i); Delays.sleep(1); int loc = this.leftBinSearch(p, pEnd, array[i]); - + if(pEnd == loc || Reads.compareValues(array[i], array[loc]) != 0) { this.rotate(p, pEnd, i); int inc = i-pEnd; loc += inc; p += inc; pEnd += inc; - + this.insertTo(pEnd, loc); nKeys++; pEnd++; @@ -253,61 +253,61 @@ private int findKeys(int a, int b, int n) { this.rotate(a, p, pEnd); return nKeys; } - + private void blockSelect(int a, int b, int t, int bLen) { for(int j = a; j < b; j += bLen) { int min = j; - + for(int i = min+bLen; i < b; i += bLen) { int cmp = Reads.compareValues(array[i], array[min]); - + if(cmp < 0 || (cmp == 0 && Reads.compareValues(array[t+(i-a)/bLen], array[t+(min-a)/bLen]) < 0)) min = i; } - + if(min != j) { this.multiSwap(j, min, bLen); Writes.swap(array, t+(j-a)/bLen, t+(min-a)/bLen, 1, true, false); } } } - + private void blockMerge(int t, int a, int m, int b, int bLen) { int a1 = a+(m-a)%bLen; int b1 = b-(b-m)%bLen; int i = a1, l = i-bLen, r = m; - + int mKey = array[t+(m-i)/bLen]; int f = a; boolean frag = true; - + this.blockSelect(a1, b1, t, bLen); - + while(l < m && r < b1) { boolean curr = Reads.compareValues(array[t++], mKey) < 0; - + if(frag != curr) { f = this.mergeFW(f-bLen, f, i, i+bLen, frag); - + if(f < i) { this.shiftBW(f, i, i+bLen); f += bLen; } else frag = curr; - + if(frag) r += bLen; else l += bLen; } else { this.shiftFW(f-bLen, f, i); f = i; - + if(frag) l += bLen; else r += bLen; } i += bLen; } - + if(l < m) { f = this.mergeFW(f-bLen, f, b1, b, true); if(f >= b1) this.shiftFW(f-bLen, f, b); @@ -318,39 +318,39 @@ private void blockMergeFewKeys(int t, int a, int m, int b, int bLen) { int a1 = a+(m-a)%bLen; int b1 = b-(b-m)%bLen; int i = a1, l = i-bLen, r = m; - + int mKey = array[t+(m-i)/bLen]; int f = a; boolean frag = true; - + this.blockSelect(a1, b1, t, bLen); - + while(l < m && r < b1) { boolean curr = Reads.compareValues(array[t++], mKey) < 0; - + if(frag != curr) { boolean tmp = frag; - - if(f == i || Reads.compareValues(array[i-1], array[i+bLen-1]) < (frag ? 1 : 0)) + + if(f == i || Reads.compareValues(array[i-1], array[i+bLen-1]) < (frag ? 1 : 0)) frag = curr; - + f = this.inPlaceMergeFW(f, i, i+bLen, tmp); - + if(frag) r += bLen; else l += bLen; } else { f = i; - + if(frag) l += bLen; else r += bLen; } i += bLen; } - + if(l < m) this.inPlaceMergeBW(f, b1, b, true); } - + private void redistFW(int a, int m, int b) { this.binaryInsertion(a, m); this.inPlaceMergeFW(a, m, b, true); @@ -359,106 +359,106 @@ private void redistBW(int a, int m, int b) { this.binaryInsertion(m, b); this.inPlaceMergeBW(a, m, b, false); } - + private void lazyStableSort(int a, int b) { if(b-a <= 16) { this.binaryInsertion(a, b); return; } - + int m = (a+b)/2; - + LazyStableSort left = new LazyStableSort(a, m); LazyStableSort right = new LazyStableSort(m, b); left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - + this.inPlaceMergeFW(a, m, b, true); } - + private void grailCommonSort(int a, int b, int nKeys) { int len = b-a; - + if(len <= 16) { this.binaryInsertion(a, b); return; } - + int bLen = sqrt(len); int tLen = len/bLen; - + int idl = bLen+tLen; boolean strat1 = nKeys >= idl; if(!strat1) idl = nKeys; - + int keys = this.findKeys(a, b, idl); int a1 = a+keys; int m = (a1+b)/2; - + if(strat1 && keys == idl) { GrailCommonSort left = new GrailCommonSort(a1, m, keys); GrailCommonSort right = new GrailCommonSort(m, b, keys); left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - + this.blockMerge(a, a1, m, b, bLen); - + m = this.leftBinSearch(a+tLen, b-bLen, array[a+tLen-1]); - + RedistFW kBuf = new RedistFW(a, a+tLen, m); RedistBW mBuf = new RedistBW(m, b-bLen, b); kBuf.start(); mBuf.start(); - + try { kBuf.join(); mBuf.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else if(keys > 4) { bLen = (b-a1-1)/(keys-keys%2)+1; - + GrailCommonSort left = new GrailCommonSort(a1, m, keys); GrailCommonSort right = new GrailCommonSort(m, b, keys); left.start(); right.start(); - + try { left.join(); right.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - + this.blockMergeFewKeys(a, a1, m, b, bLen); this.redistFW(a, a1, b); } else if(keys > 1) this.lazyStableSort(a, b); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.grailCommonSort(0, length, 46341); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/RemiSort.java b/src/main/java/sorts/hybrid/RemiSort.java index 5172aa59..30c3fdf6 100644 --- a/src/main/java/sorts/hybrid/RemiSort.java +++ b/src/main/java/sorts/hybrid/RemiSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RemiSort extends MultiWayMergeSorting { +public final class RemiSort extends MultiWayMergeSorting { public RemiSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -268,4 +268,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.deleteExternalArray(buf); Writes.changeAllocAmount(-alloc); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/SqrtSort.java b/src/main/java/sorts/hybrid/SqrtSort.java index 7cbb3333..b8de9ae0 100644 --- a/src/main/java/sorts/hybrid/SqrtSort.java +++ b/src/main/java/sorts/hybrid/SqrtSort.java @@ -5,7 +5,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2014 Andrey Astrelin @@ -45,27 +45,27 @@ this software and associated documentation files (the "Software"), to deal in final class SqrtState { private int leftOverLen; private int leftOverFrag; - + protected SqrtState(int len, int frag) { this.leftOverLen = len; this.leftOverFrag = frag; } - + protected int getLeftOverLen() { return leftOverLen; } - + protected int getLeftOverFrag() { return leftOverFrag; } } -final public class SqrtSort extends Sort { +public final class SqrtSort extends Sort { private InsertionSort insertSorter; - + public SqrtSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Sqrt"); //this.setRunAllID("Square Root Sort (Block Merge Sort)"); //this.setRunAllSortsName("Square Root Sort [Block Merge Sort]"); @@ -79,14 +79,14 @@ public SqrtSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void sqrtSwap(int[] arr, int a, int b, boolean auxwrite) { Writes.swap(arr, a, b, 1, true, auxwrite); Highlights.clearMark(2); } - private void sqrtMultiSwap(int[] arr, int a, int b, int swapsLeft, boolean auxwrite) { - while(swapsLeft != 0) { + private void sqrtMultiSwap(int[] arr, int a, int b, int swapsLeft, boolean auxwrite) { + while(swapsLeft != 0) { this.sqrtSwap(arr, a++, b++, auxwrite); swapsLeft--; } @@ -95,7 +95,7 @@ private void sqrtMultiSwap(int[] arr, int a, int b, int swapsLeft, boolean auxwr private void sqrtInsertSort(int[] arr, int pos, int len, boolean auxwrite) { insertSorter.customInsertSort(arr, pos, len, 0.25, auxwrite); } - + private void sqrtMergeRight(int[] arr, int pos, int leftLen, int rightLen, int dist, boolean auxwrite) { int mergedPos = leftLen + rightLen + dist - 1; int right = leftLen + rightLen - 1; @@ -104,22 +104,22 @@ private void sqrtMergeRight(int[] arr, int pos, int leftLen, int rightLen, int d while(left >= 0) { Highlights.markArray(2, pos + left); Highlights.markArray(3, pos + right); - + if(right < leftLen || Reads.compareValues(arr[pos + left], arr[pos + right]) > 0) { Writes.write(arr, pos + (mergedPos--), arr[pos + (left--)], 1, true, auxwrite); - } + } else Writes.write(arr, pos + (mergedPos--), arr[pos + (right--)], 1, true, auxwrite); } - + Highlights.clearMark(3); - + if(right != mergedPos) { while(right >= leftLen) { Writes.write(arr, pos + (mergedPos--), arr[pos + (right--)], 1, true, auxwrite); Highlights.markArray(2, pos + right); } } - + Highlights.clearMark(2); } @@ -135,20 +135,20 @@ private void sqrtMergeLeftWithXBuf(int[] arr, int pos, int leftEnd, int rightEnd Writes.write(arr, pos + dist++, arr[pos + right++], 1, true, auxwrite); } else Writes.write(arr, pos + dist++, arr[pos + left++], 1, true, auxwrite); - + Highlights.markArray(2, pos + left); Highlights.markArray(3, pos + right); } - + Highlights.clearMark(3); - + if(dist != left) { while(left < leftEnd) { Writes.write(arr, pos + dist++, arr[pos + left++], 1, true, auxwrite); Highlights.markArray(2, pos + left); } } - + Highlights.clearMark(2); } @@ -156,29 +156,29 @@ private void sqrtMergeLeftWithXBuf(int[] arr, int pos, int leftEnd, int rightEnd private void sqrtMergeDown(int[] arr, int arrPos, int[] buffer, int bufPos, int leftLen, int rightLen, boolean auxwrite) { int arrMerge = 0, bufMerge = 0; int dist = 0 - rightLen; - + while(bufMerge < rightLen) { if(arrMerge == leftLen || Reads.compareValues(arr[arrPos + arrMerge], buffer[bufPos + bufMerge]) >= 0) { Writes.write(arr, arrPos + dist++, buffer[bufPos + bufMerge++], 1, true, auxwrite); } else Writes.write(arr, arrPos + dist++, arr[arrPos + arrMerge++], 1, true, auxwrite); - + Highlights.markArray(2, arrPos + arrMerge); Highlights.markArray(3, bufPos + bufMerge); } - + Highlights.clearMark(3); - + if(dist != arrMerge) { while(arrMerge < leftLen) { Writes.write(arr, arrPos + dist++, arr[arrPos + arrMerge++], 1, true, auxwrite); Highlights.markArray(2, arrPos + arrMerge); } } - + Highlights.clearMark(2); } - + //returns the leftover length, then the leftover fragment private SqrtState sqrtSmartMergeWithXBuf(int[] arr, int pos, int leftOverLen, int leftOverFrag, int blockLen, boolean auxwrite) { int dist = 0 - blockLen, left = 0, right = leftOverLen, leftEnd = right, rightEnd = right + blockLen; @@ -189,30 +189,30 @@ private SqrtState sqrtSmartMergeWithXBuf(int[] arr, int pos, int leftOverLen, in Writes.write(arr, pos + dist++, arr[pos + left++], 1, true, auxwrite); } else Writes.write(arr, pos + dist++, arr[pos + right++], 1, true, auxwrite); - + Highlights.markArray(2, pos + left); Highlights.markArray(3, pos + right); } - + Highlights.clearMark(3); - + int length, fragment = leftOverFrag; - + if(left < leftEnd) { length = leftEnd - left; - + while(left < leftEnd) { Writes.write(arr, pos + --rightEnd, arr[pos + --leftEnd], 1, true, auxwrite); Highlights.markArray(2, pos + leftEnd); } - } + } else { length = rightEnd - right; fragment = typeFrag; } - + Highlights.clearMark(2); - + return new SqrtState(length, fragment); } @@ -236,90 +236,90 @@ private void sqrtMergeBuffersLeftWithXBuf(int[] keys, int midkey, int[] arr, int int processIndex = regBlockLen; int restToProcess; - + for(int keyIndex = 1; keyIndex < blockCount; keyIndex++, processIndex += regBlockLen) { restToProcess = processIndex - leftOverLen; int nextFrag = Reads.compareOriginalValues(keys[keyIndex], midkey) < 0 ? 0 : 1; if(nextFrag == leftOverFrag) { Writes.arraycopy(arr, pos + restToProcess, arr, pos + restToProcess - regBlockLen, leftOverLen, 1, true, auxwrite); - + restToProcess = processIndex; leftOverLen = regBlockLen; } else { SqrtState results = this.sqrtSmartMergeWithXBuf(arr, pos + restToProcess, leftOverLen, leftOverFrag, regBlockLen, auxwrite); - + leftOverLen = results.getLeftOverLen(); leftOverFrag = results.getLeftOverFrag(); } } - + restToProcess = processIndex - leftOverLen; if(lastLen != 0) { if(leftOverFrag != 0) { Writes.arraycopy(arr, pos + restToProcess, arr, pos + restToProcess - regBlockLen, leftOverLen, 1, true, auxwrite); - + restToProcess = processIndex; leftOverLen = regBlockLen * aBlockCount; leftOverFrag = 0; - } + } else { leftOverLen += regBlockLen * aBlockCount; } this.sqrtMergeLeftWithXBuf(arr, pos + restToProcess, leftOverLen, lastLen, 0 - regBlockLen, auxwrite); - } + } else { Writes.arraycopy(arr, pos + restToProcess, arr, pos + restToProcess - regBlockLen, leftOverLen, 1, true, auxwrite); } } - + // build blocks of length buildLen // input: [-buildLen,-1] elements are buffer // output: first buildLen elements are buffer, blocks 2 * buildLen and last subblock sorted private void sqrtBuildBlocks(int[] arr, int pos, int len, int buildLen, boolean auxwrite) { int extraDist, part; - + for(int dist = 1; dist < len; dist += 2) { extraDist = 0; if(Reads.compareValues(arr[pos + (dist - 1)], arr[pos + dist]) > 0) extraDist = 1; - + Writes.write(arr, pos + dist - 3, arr[pos + dist - 1 + extraDist], 1, true, auxwrite); Writes.write(arr, pos + dist - 2, arr[pos + dist - extraDist], 1, true, auxwrite); } if(len % 2 != 0) Writes.write(arr, pos + len - 3, arr[pos + len - 1], 1, true, auxwrite); - + pos -= 2; - + for(part = 2; part < buildLen; part *= 2) { int left = 0; int right = len - 2 * part; - + while(left <= right) { this.sqrtMergeLeftWithXBuf(arr, pos + left, part, part, 0 - part, auxwrite); left += 2 * part; } - + int rest = len - left; if(rest > part) { this.sqrtMergeLeftWithXBuf(arr, pos + left, part, rest - part, 0 - part, auxwrite); - } + } else { while(left < len) Writes.write(arr, pos + left - part, arr[pos + left++], 1, true, auxwrite); } - + pos -= part; } int restToBuild = len % (2 * buildLen); int leftOverPos = len - restToBuild; - + if(restToBuild <= buildLen) { Writes.arraycopy(arr, pos + leftOverPos, arr, pos + leftOverPos + buildLen, restToBuild, 1, true, auxwrite); } else this.sqrtMergeRight(arr, pos + leftOverPos, buildLen, restToBuild - buildLen, buildLen, auxwrite); - + while(leftOverPos > 0) { leftOverPos -= 2 * buildLen; this.sqrtMergeRight(arr, pos + leftOverPos, buildLen, buildLen, buildLen, auxwrite); @@ -336,29 +336,29 @@ private void sqrtCombineBlocks(int[] arr, int pos, int len, int buildLen, int re len -= leftOver; leftOver = 0; } - + int leftIndex = 0; - + for(int i = 0; i <= combineLen; i++) { if(i == combineLen && leftOver == 0) break; - + int blockPos = pos + i * 2 * buildLen; int blockCount = (i == combineLen ? leftOver : 2 * buildLen) / regBlockLen; - + int tagIndex = blockCount + (i == combineLen ? 1 : 0); for(int j = 0; j <= tagIndex; j++) Writes.write(tags, j, j, 1, true, true); - + int midkey = buildLen / regBlockLen; - + for(tagIndex = 1; tagIndex < blockCount; tagIndex++) { leftIndex = tagIndex - 1; - + for(int rightIndex = tagIndex; rightIndex < blockCount; rightIndex++) { int rightComp = Reads.compareValues(arr[blockPos + leftIndex * regBlockLen], arr[blockPos + rightIndex * regBlockLen]); if(rightComp > 0 || (rightComp == 0 && tags[leftIndex] > tags[rightIndex])) leftIndex = rightIndex; } - + if(leftIndex != tagIndex - 1) { this.sqrtMultiSwap(arr, blockPos + (tagIndex - 1) * regBlockLen, blockPos + leftIndex * regBlockLen, regBlockLen, auxwrite); this.sqrtSwap(tags, tagIndex - 1, leftIndex, true); @@ -366,9 +366,9 @@ private void sqrtCombineBlocks(int[] arr, int pos, int len, int buildLen, int re } int aBlockCount = 0; int lastLen = 0; - + if(i == combineLen) lastLen = leftOver % regBlockLen; - + if(lastLen != 0) { while(aBlockCount < blockCount && Reads.compareValues(arr[blockPos + blockCount * regBlockLen], arr[blockPos + (blockCount - aBlockCount - 1) * regBlockLen]) < 0) { @@ -382,29 +382,29 @@ private void sqrtCombineBlocks(int[] arr, int pos, int len, int buildLen, int re private void sqrtCommonSort(int[] arr, int pos, int len, int[] extBuf, int extBufPos, int[] tags, boolean auxwrite) { insertSorter = new InsertionSort(this.arrayVisualizer); - + if(len <= 16) { this.sqrtInsertSort(arr, pos, len, auxwrite); Highlights.clearAllMarks(); return; } - + int blockLen = 1; while((blockLen * blockLen) < len) blockLen *= 2; - + Writes.arraycopy(arr, pos, extBuf, extBufPos, blockLen, 1, true, auxwrite); - + this.sqrtCommonSort(extBuf, extBufPos, blockLen, arr, pos, tags, !auxwrite); - + this.sqrtBuildBlocks(arr, pos + blockLen, len - blockLen, blockLen, auxwrite); - + int buildLen = blockLen; - + while(len > (buildLen *= 2)) { this.sqrtCombineBlocks(arr, pos + blockLen, len - blockLen, buildLen, blockLen, tags, auxwrite); } this.sqrtMergeDown(arr, pos + blockLen, extBuf, extBufPos, len - blockLen, blockLen, auxwrite); - + Highlights.clearAllMarks(); } @@ -414,7 +414,7 @@ public void runSort(int[] array, int len, int bucketCount) { while(bufferLen * bufferLen < len) bufferLen *= 2; int numKeys = (len - 1) / bufferLen + 2; - + int[] extBuf = Writes.createExternalArray(bufferLen); int[] tags = Writes.createExternalArray(numKeys); @@ -423,4 +423,4 @@ public void runSort(int[] array, int len, int bucketCount) { Writes.deleteExternalArray(extBuf); Writes.deleteExternalArray(tags); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/StacklessDualPivotQuickSort.java b/src/main/java/sorts/hybrid/StacklessDualPivotQuickSort.java index 7f2b4d4c..2ca91fdc 100644 --- a/src/main/java/sorts/hybrid/StacklessDualPivotQuickSort.java +++ b/src/main/java/sorts/hybrid/StacklessDualPivotQuickSort.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StacklessDualPivotQuickSort extends Sort { +public final class StacklessDualPivotQuickSort extends Sort { public StacklessDualPivotQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -154,4 +154,4 @@ private void quickSort(int[] array, int a, int b) { public void runSort(int[] array, int length, int bucketCount) { this.quickSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/StacklessHybridQuickSort.java b/src/main/java/sorts/hybrid/StacklessHybridQuickSort.java index c4e909b0..0380a4d7 100644 --- a/src/main/java/sorts/hybrid/StacklessHybridQuickSort.java +++ b/src/main/java/sorts/hybrid/StacklessHybridQuickSort.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StacklessHybridQuickSort extends Sort { +public final class StacklessHybridQuickSort extends Sort { public StacklessHybridQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -161,4 +161,4 @@ private void quickSort(int[] array, int a, int b) { public void runSort(int[] array, int length, int bucketCount) { this.quickSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/SynchronousSqrtSort.java b/src/main/java/sorts/hybrid/SynchronousSqrtSort.java index 6e030a0d..6235ff4e 100644 --- a/src/main/java/sorts/hybrid/SynchronousSqrtSort.java +++ b/src/main/java/sorts/hybrid/SynchronousSqrtSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2021 The Holy Grail Sort Project, implemented by aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SynchronousSqrtSort extends BlockMergeSorting { +public final class SynchronousSqrtSort extends BlockMergeSorting { public SynchronousSqrtSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Synchronous Sqrt"); this.setRunAllSortsName("Synchronous Sqrt Sort"); this.setRunSortName("Synchronous Sqrtsort"); @@ -44,14 +44,14 @@ public SynchronousSqrtSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int smartMergeBW(int[] array, int a, int m, int b, int p, boolean rev) { int i = m-1, j = b-1; int cmp = rev ? -1 : 0; - + while(i >= a && j >= m) { Highlights.markArray(2, i); - + if(Reads.compareValues(array[i], array[j]) > cmp) Writes.write(array, --p, array[i--], 1, true, false); else @@ -59,18 +59,18 @@ private int smartMergeBW(int[] array, int a, int m, int b, int p, boolean rev) { } return i+1; } - + private void blockSelection(int[] array, int[] tags, int a, int b, int bLen, int t, int tj) { for(int i = 0; i < Math.min(tj+1, tags.length-t); i++) Writes.write(tags, t+i, i + (i <= tj/2 ? 0 : tags.length), 1, true, true); - + for(int j = a, p = a; j < b-bLen; j+=bLen) { int min = p == j ? j+bLen : j; - + for(int i = min+bLen; i < b; i+=bLen) { if(i != p) { int cmp = Reads.compareValues(array[i], array[min]); - + if(cmp == -1 || (cmp == 0 && Reads.compareOriginalValues(tags[t + (i-a)/bLen], tags[t + (min-a)/bLen]) == -1)) min = i; } @@ -79,7 +79,7 @@ private void blockSelection(int[] array, int[] tags, int a, int b, int bLen, int if(p == j) { Writes.arraycopy(array, min, array, j, bLen, 1, true, false); Writes.write(tags, t + (j-a)/bLen, tags[t + (min-a)/bLen], 1, true, false); - + p = min; } else { @@ -93,7 +93,7 @@ private void mergeBlocksBW(int[] array, int[] tags, int a, int b, int ti, int tb int tj = tb-1, mkv = tags.length; int f = b, a1 = f-bLen; boolean rev = Reads.compareOriginalValues(tags[tj], mkv) < 0; - + while(true) { do { tj--; @@ -109,73 +109,73 @@ private void mergeBlocksBW(int[] array, int[] tags, int a, int b, int ti, int tb rev = !rev; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { if(length <= 16) { this.binaryInsertion(array, 0, length); return; } - + int bLen = 1; while(bLen*bLen < length) bLen *= 2; int mod = length%bLen; - + int a = bLen+mod, b = length, len = b-a; int i, j = 1; - + int[] temp = Writes.createExternalArray(bLen+mod); int[] tags = Writes.createExternalArray((length-1)/bLen+1); - + this.binaryInsertion(array, 0, a); Writes.arraycopy(array, 0, temp, 0, a, 1, true, true); - + for(; j < bLen; j *= 2) { int p = Math.max(2, j); - + for(i = a; i+2*j < b; i += 2*j) this.mergeWithBufFWExt(array, i, i+j, i+2*j, i-p); - + if(i+j < b) this.mergeWithBufFWExt(array, i, i+j, b, i-p); else this.shiftFWExt(array, i-p, i, b); - + a -= p; b -= p; } - + int p = len%(2*j); i = b-p; - + if(i+j < b) this.mergeWithBufBWExt(array, i, i+j, b, b+j); else this.shiftBWExt(array, i, b, b+j); - + for(i -= 2*j; i >= a; i -= 2*j) this.mergeWithBufBWExt(array, i, i+j, i+2*j, i+3*j); - + a += j; b += j; j *= 2; - + for(int ti, tj = 4; j < len; j *= 2, tj *= 2) { for(i = a, ti = 0; i+2*j < b; i += 2*j, ti += tj) this.blockSelection(array, tags, i-bLen, i+2*j, bLen, ti, tj); - + boolean noFrag = i+j < b; p = (b-i)/bLen; - + if(noFrag) this.blockSelection(array, tags, i-bLen, b, bLen, ti, tj); - + a -= bLen; b -= bLen; i -= bLen; - + if(noFrag) this.mergeBlocksBW(array, tags, i, b, ti, ti+p, bLen); - + for(i -= 2*j, ti -= tj; i >= a; i -= 2*j, ti -= tj) this.mergeBlocksBW(array, tags, i, i+2*j, ti, ti+tj, bLen); - + a += bLen; b += bLen; } p = 0; i = 0; j = a; - + while(i < a && j < b) { Highlights.markArray(2, i); - + if(Reads.compareValues(temp[i], array[j]) <= 0) Writes.write(array, p++, temp[i++], 1, true, false); else @@ -188,4 +188,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.deleteExternalArray(temp); Writes.deleteExternalArray(tags); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/TimSort.java b/src/main/java/sorts/hybrid/TimSort.java index 9206f2a4..4a5323b8 100644 --- a/src/main/java/sorts/hybrid/TimSort.java +++ b/src/main/java/sorts/hybrid/TimSort.java @@ -48,4 +48,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { TimSorting.sort(this.timSortInstance, array, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/UnstableGrailSort.java b/src/main/java/sorts/hybrid/UnstableGrailSort.java index b0c6f9c8..c6637781 100644 --- a/src/main/java/sorts/hybrid/UnstableGrailSort.java +++ b/src/main/java/sorts/hybrid/UnstableGrailSort.java @@ -4,7 +4,7 @@ import sorts.templates.UnstableGrailSorting; /* - * + * The MIT License (MIT) Copyright (c) 2013 Andrey Astrelin @@ -47,10 +47,10 @@ this software and associated documentation files (the "Software"), to deal in /* */ /*********************************************************/ -final public class UnstableGrailSort extends UnstableGrailSorting { +public final class UnstableGrailSort extends UnstableGrailSorting { public UnstableGrailSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Unstable Grail"); //this.setRunAllID("Grail Sort (Block Merge Sort)"); //this.setRunAllSortsName("Grail Sort [Block Merge Sort]"); @@ -64,9 +64,9 @@ public UnstableGrailSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.grailCommonSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/WeaveMergeSort.java b/src/main/java/sorts/hybrid/WeaveMergeSort.java index efbc61c9..95403be9 100644 --- a/src/main/java/sorts/hybrid/WeaveMergeSort.java +++ b/src/main/java/sorts/hybrid/WeaveMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class WeaveMergeSort extends Sort { +public final class WeaveMergeSort extends Sort { public WeaveMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Weave Merge"); this.setRunAllSortsName("Weave Merge Sort"); this.setRunSortName("Weave Mergesort"); @@ -47,29 +47,29 @@ public WeaveMergeSort(ArrayVisualizer arrayVisualizer) { private void weaveInsert(int[] arr, int start, int end) { int pos; - + for(int j = start; j < end; j++){ pos = j; - + Highlights.markArray(1, j); Highlights.clearMark(2); - + while(pos > start && Reads.compareValues(arr[pos], arr[pos - 1]) < 1) { Writes.swap(arr, pos, pos - 1, 0.2, true, false); pos--; } } } - + private void weaveMerge(int[] arr, int min, int max, int mid) { int i = 1; int target = (mid - min); - + while(i <= target) { Writes.multiSwap(arr, mid + i, min + (i * 2) - 1, 0.05, true, false); i++; } - + this.weaveInsert(arr, min, max + 1); } @@ -95,4 +95,4 @@ else if(max - min == 1) { //only two elements and swaps them public void runSort(int[] array, int currentLength, int bucketCount) { this.weaveMergeSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/WikiSort.java b/src/main/java/sorts/hybrid/WikiSort.java index 8d761ac7..24074c20 100644 --- a/src/main/java/sorts/hybrid/WikiSort.java +++ b/src/main/java/sorts/hybrid/WikiSort.java @@ -79,4 +79,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { WikiSorting.sort(this.wikiSortInstance, array, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/hybrid/YujisBufferedMergeSort2.java b/src/main/java/sorts/hybrid/YujisBufferedMergeSort2.java index da966369..1b29e12e 100644 --- a/src/main/java/sorts/hybrid/YujisBufferedMergeSort2.java +++ b/src/main/java/sorts/hybrid/YujisBufferedMergeSort2.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020 yuji, implemented by aphitorite, edited by dani_dlg @@ -30,12 +30,12 @@ of this software and associated documentation files (the "Software"), to deal */ -final public class YujisBufferedMergeSort2 extends Sort { - - +public final class YujisBufferedMergeSort2 extends Sort { + + public YujisBufferedMergeSort2(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Optimized A/Y Sort"); this.setRunAllSortsName("Optimized Andrey/Yuji Sort"); this.setRunSortName("Optimized Andrey/Yuji Sort"); @@ -53,45 +53,45 @@ public static int ceilLog(int n) { for(i = 0; (1 << i) < n; i++); return i; } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void insertTo(int[] array, int a, int b) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[(a--)-1], 0.5, true, false); Writes.write(array, b, temp, 0.5, true, false); } - + private int binarySearch(int[] array, int start, int end, int value, boolean left) { int a = start, b = end; - + while(a < b) { int m = a+(b-a)/2; boolean comp; - + if(left) comp = Reads.compareValues(value, array[m]) <= 0; else comp = Reads.compareValues(value, array[m]) < 0; - + if(comp) b = m; else a = m+1; } - + return a; } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, i, this.binarySearch(array, a, i, array[i], false)); } - - + + private void mergeWithBufStatic(int[] array, int a, int m, int b, int p, boolean useBinarySearch) { int i = 0, j = m, k = a; - + if(useBinarySearch) { while(i < m-a && j < b) { if(Reads.compareValues(array[j], array[p+i]) == -1) { @@ -118,19 +118,19 @@ private void mergeWithBufStatic(int[] array, int a, int m, int b, int p, boolean } } } - + private int merge(int[] array, int a, int m, int b, int p) { int i = a, j = m; while(i < m && j < b) { if(Reads.compareIndices(array, i, j, 0, false) <= 0) Writes.swap(array, p++, i++, 1, true, false); - else + else Writes.swap(array, p++, j++, 1, true, false); } int leftover = 0; while(i < m) { Writes.swap(array, p++, i++, 1, true, false); - + } while(j < b) { Writes.swap(array, p++, j++, 1, true, false); @@ -138,45 +138,45 @@ private int merge(int[] array, int a, int m, int b, int p) { } return leftover; } - + private void mergeSort(int[] array, int a, int p, int length) { int i, j = 16, pos, ceilLog = ceilLog(length); - + if(length > 16 && (ceilLog & 1) == 1) pos = p; else pos = a; - + for(i = pos; i+16 <= pos+length; i+=16) this.binaryInsertion(array, i, i+16); this.binaryInsertion(array, i, pos+length); - + int next = pos, posNext; while(j < length) { pos = next; next ^= a ^ p; posNext = next; - + for(i = pos; i+2*j <= pos+length; i+=2*j, posNext+=2*j) this.merge(array, i, i+j, i+2*j, posNext); if(i + j < pos+length) this.merge(array, i, i+j, pos+length, posNext); else while(i < pos+length) Writes.swap(array, i++, posNext++, 1, true, false); - + j *= 2; } } - + private void bufferedMerge(int[] array, int a, int b) { if(b-a <= 16) { this.binaryInsertion(array, a, b); return; } - + int m = (a+b+1)/2; this.mergeSort(array, m, 2*m-b, b-m); - + int n = (a+m+1)/2; //The 16 means that it will recursively sort 1/16 of the array. It's interesting to change that number and see how it affects performance int limit = (b-a)/16; @@ -186,18 +186,18 @@ private void bufferedMerge(int[] array, int a, int b) { m = n; n = (a+m+1)/2; } - + // the same as Andreysort's buffer redistribution this.bufferedMerge(array, a, m); this.multiSwap(array, a, b-(m-a), m-a); int s = this.merge(array, m, b-(m-a), b, a); this.bufferedMerge(array, b-(m-a)-s, b); - + } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.bufferedMerge(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/AATreeSort.java b/src/main/java/sorts/insert/AATreeSort.java index 183e085d..676055f1 100644 --- a/src/main/java/sorts/insert/AATreeSort.java +++ b/src/main/java/sorts/insert/AATreeSort.java @@ -5,13 +5,13 @@ /** * An implementation of a tree sort using an AA tree. - * + * * @author Sam Walko (Anonymous0726) */ -final public class AATreeSort extends Sort { +public final class AATreeSort extends Sort { public AATreeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("AA Tree"); this.setRunAllSortsName("Tree Sort (AA Balanced)"); this.setRunSortName("Tree sort (AA Balanced)"); @@ -23,9 +23,9 @@ public AATreeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private Node NULL_NODE = new Node(); // (sub)tree of size 0 - + /** * The fundamental building block of any programming tree. Each node is the * root of its own subtree. In general, a node has the data being stored at @@ -36,12 +36,12 @@ public AATreeSort(ArrayVisualizer arrayVisualizer) { private class Node { // main array (poor encapsulation since it's all the same but w/e) private int[] array; - - private int pointer; // index in main array of the element contained here + + private int pointer; // index in main array of the element contained here private Node left, right; // left and right subtrees - + private int level; - + // Default constructor, and constructor for NULL_NODE private Node() { // Shouldn't point to anything by default @@ -50,7 +50,7 @@ private Node() { this.right = NULL_NODE; this.level = -1; // A NULL_NODE is level -1 } - + // Constructor for a node with a pointer private Node(int[] array, int pointer) { this(); @@ -58,10 +58,10 @@ private Node(int[] array, int pointer) { this.pointer = pointer; this.level = 0; // A new element is always initially placed at level 0 } - + /** * Recursively adds an element to the subtree whose root is this node - * + * * @param addPointer A pointer to the array telling what element is to be inserted * @return the Node at to be located at this point */ @@ -71,33 +71,33 @@ private Node add(int addPointer) { Highlights.clearMark(2); // No longer comparing to previous leaves return new Node(array, addPointer); // Create the node and return it } - + // If there's an element already here, we need to compare them to // determine whether it should be placed in the left or the right subtree. // Thus, mark the element at the pointer for comparison. Highlights.markArray(2, pointer); - + // Case 2: The element is smaller and thus belongs in the left subtree if(Reads.compareValues(array[addPointer], array[pointer]) == -1) { Delays.sleep(0.25); - + // Recursively get the root of the new left subtree Node newLeft = left.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new left subtree as such Writes.changeAuxWrites(1); Writes.startLap(); left = newLeft; Writes.stopLap(); Delays.sleep(0.05); - + // This handles the case where left subtree increased in height if(left.level == level) { - if(level != right.level) // Often, a skew is all that's needed to + if(level != right.level) // Often, a skew is all that's needed to return skew(); // keep AA tree properties - + // Other times, we can skip the case where a skew is immediately // followed by a split (i.e., a right rotation immediately // followed by a left rotation) on the same subtree. @@ -105,32 +105,32 @@ private Node add(int addPointer) { level++; return this; } - + // Else, just return this node return this; } - + // Case 3: The element is equal or larger and thus belongs in the right subtree // Note: As equality also results in right subtree, this sort is stable Delays.sleep(0.25); - + // Recursively get the root of the new right subtree Node newRight = right.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new right subtree as such Writes.changeAuxWrites(1); Writes.startLap(); right = newRight; Writes.stopLap(); Delays.sleep(0.05); - + // This handles the case where right subtree's right subtree increased // in height, requiring a split to keep AA tree properties if(right.right.level == level) return split(); - + // Else, just return this node return this; } @@ -139,60 +139,60 @@ private Node add(int addPointer) { * Since the level of every left child is exactly one less than that of * its parent, performs a skew to maintain this AA tree property. Helper * method for add. Analogous to a single right rotation. - * + * * @return the new root of this subtree */ private Node skew() { Node l = left; - + Highlights.markArray(3, pointer); Highlights.markArray(4, l.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); left = l.right; l.right = this; Writes.stopLap(); Delays.sleep(0.25); - + Highlights.clearMark(3); Highlights.clearMark(4); - + return l; } - + /** * Since only one consecutive right children may be on the same level, * performs a skew to maintain this AA tree property. Helper method for * add and skew. Analogous to a single left rotation. - * + * * @return the new root of this subtree */ private Node split() { Node r = right; - + Highlights.markArray(3, pointer); Highlights.markArray(4, r.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); right = r.left; r.left = this; Writes.stopLap(); Delays.sleep(0.25); - + r.level++; - + Highlights.clearMark(3); Highlights.clearMark(4); - + return r; } /** * Performs an in-order traversal of the array and writes * the values of the original array to a sorted temporary array - * + * * @param tempArray the temporary array to write the contents of the subtree to * @param location a pointer to the location in the temporary array to which the * contents of the current subtree should be written to @@ -201,38 +201,38 @@ private Node split() { */ private int writeToArray(int[] tempArray, int location) { if(this == NULL_NODE) return 0; - + int leftTreeSize = left.writeToArray(tempArray, location); int newLocation = location + leftTreeSize; - + Highlights.markArray(1, pointer); Writes.write(tempArray, newLocation, array[pointer], 0.1, false, true); - + int rightTreeSize = right.writeToArray(tempArray, newLocation + 1); return leftTreeSize + rightTreeSize + 1; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { // Tells the tree what array is to be sorted NULL_NODE.array = array; // Creates a tree of size 0, to which all elements will be added Node root = NULL_NODE; - + // This loop adds every element of the array to be sorted into the tree for(int i = 0; i < length; i++) { Highlights.markArray(1, i); // Highlights the element being added Node newRoot = root.add(i); - + Highlights.clearMark(2); - + Writes.changeAuxWrites(1); Writes.startLap(); root = newRoot; Writes.stopLap(); Delays.sleep(0.25); - + Highlights.clearAllMarks(); // Clearing all just in case } diff --git a/src/main/java/sorts/insert/AVLTreeSort.java b/src/main/java/sorts/insert/AVLTreeSort.java index b2b6c7fc..018afaed 100644 --- a/src/main/java/sorts/insert/AVLTreeSort.java +++ b/src/main/java/sorts/insert/AVLTreeSort.java @@ -6,13 +6,13 @@ /** * An implementation of a tree sort using an AVL tree, * based on what I learned in my CSSE230 class - * + * * @author Sam Walko (Anonymous0726) */ -final public class AVLTreeSort extends Sort { +public final class AVLTreeSort extends Sort { public AVLTreeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("AVL Tree"); this.setRunAllSortsName("Tree Sort (AVL Balanced)"); this.setRunSortName("Tree sort (AVL Balanced)"); @@ -24,28 +24,28 @@ public AVLTreeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private Node NULL_NODE = new Node(); // (sub)tree of size 0 - + /** * The fundamental building block of any programming tree. Each node is the * root of its own subtree. In general, a node has the data being stored at * that point, up to two children nodes, and (optionally) data for keeping * the tree balanced. In the case of an AVL tree, the data for balancing is * an integer telling it's "balance": the height of the right subtree minus - * the height of the left subtree. + * the height of the left subtree. */ private class Node { // main array (poor encapsulation since it's all the same but w/e) private int[] array; - - private int pointer; // index in main array of the element contained here + + private int pointer; // index in main array of the element contained here private Node left, right; // left and right subtrees - + // height of right subtree minus height of left subtree // by definition of an AVL tree, must be -1, 0, or 1 private int balance; - + // Default constructor, and constructor for NULL_NODE private Node() { // Shouldn't point to anything by default @@ -54,14 +54,14 @@ private Node() { this.right = NULL_NODE; this.balance = 0; // A tree of size 0 is balanced } - + // Constructor for a node with a pointer private Node(int[] array, int pointer) { this(); this.array = array; this.pointer = pointer; } - + /** * A return container for the recursive add method containing a Node * and a boolean telling whether or not the subtree changed height @@ -69,16 +69,16 @@ private Node(int[] array, int pointer) { private class AddContainer { private Node node; private boolean heightChange; - + private AddContainer(Node node, boolean heightChange) { this.node = node; this.heightChange = heightChange; } } - + /** * Recursively adds an element to the subtree whose root is this node - * + * * @param addPointer A pointer to the array telling what element is to be inserted * @return an AddContainer containing the node that is now the root of this * subtree, and the boolean telling whether or not this subtree increased in height @@ -92,72 +92,72 @@ private AddContainer add(int addPointer) { // as there was no subtree here previously return new AddContainer(newNode, true); } - + // If there's an element already here, we need to compare them to // determine whether it should be placed in the left or the right subtree. // Thus, mark the element at the pointer for comparison. Highlights.markArray(2, pointer); - + // Case 2: The element is smaller and thus belongs in the left subtree if(Reads.compareValues(array[addPointer], array[pointer]) == -1) { Delays.sleep(0.25); - + // Recursively get the root of the new left subtree AddContainer container = left.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new left subtree as such Writes.changeAuxWrites(1); Writes.startLap(); left = container.node; Writes.stopLap(); Delays.sleep(0.05); - + // This handles the case where left subtree increased in // height, possibly requiring rotations to keep balance if(container.heightChange) return heightChangeLeft(); - + // In the case where left subtree did not increase in height, // return this node, and the fact that the height obviously // did not change as neither of it subtrees changed in height return new AddContainer(this, false); // This is the boolean that took me about a day to fix, btw. } - + // Case 3: The element is equal or larger and thus belongs in the right subtree // Note: As equality also results in right subtree, this sort is stable Delays.sleep(0.25); - + // Recursively get the root of the new right subtree AddContainer container = right.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new right subtree as such Writes.changeAuxWrites(1); Writes.startLap(); right = container.node; Writes.stopLap(); Delays.sleep(0.05); - + // This handles the case where right subtree increased in // height, possibly requiring rotations to keep balance - if(container.heightChange) + if(container.heightChange) return heightChangeRight(); - + // In the case where right subtree did not increase in height, // return this node, and the fact that the height obviously // did not change as neither of it subtrees changed in height return new AddContainer(this, false); // This is also the boolean that took me about a day to fix. } - + /** * Updates balance codes and rotates if necessary when the left subtree's * height has increased. Helper method for add. - * + * * @return an AddContainer including the node to be placed at the position * and whether or not the left subtree's height has been changed */ @@ -169,17 +169,17 @@ private AddContainer heightChangeLeft() { } // Determine which type of rotation necessary. Note that after // a rotation, the subtree height must not have changed. - if(left.balance == -1) + if(left.balance == -1) // Left-most subtree must be too large, so fix with single rotation return new AddContainer(singleRotateRight(), false); // Middle-left subtree must be too large, so fix with double rotation return new AddContainer(doubleRotateRight(), false); } - + /** * Updates balance codes and rotates if necessary when the right subtree's * height has increased. Helper method for add. - * + * * @return an AddContainer including the node to be placed at the position * and whether or not the right subtree's height has been changed */ @@ -197,127 +197,127 @@ private AddContainer heightChangeRight() { // Middle-right subtree must be too large, so fix with double rotation return new AddContainer(doubleRotateLeft(), false); } - + // These rotations are kinda magic (read: too difficult to explain without // diagrams), so feel free to ask me (Anonymous0726#2452 on Discord) about it. // Alternatively, the animations on Wikipedia are pretty good. - + /** * Singly rotates the subtree right about the current node, - * updating balance codes along the way. Helper method for + * updating balance codes along the way. Helper method for * heightChangeLeft and heightChangeRight. - * + * * @return the subtree's new root */ private Node singleRotateRight() { Node b = left; - + Highlights.markArray(3, pointer); Highlights.markArray(4, b.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); left = b.right; b.right = this; Writes.stopLap(); Delays.sleep(0.25); - + balance = 0; b.balance = 0; - + Highlights.clearMark(3); Highlights.clearMark(4); - + return b; } - + /** * Singly rotates the subtree left about the current node, - * updating balance codes along the way. Helper method for + * updating balance codes along the way. Helper method for * heightChangeLeft and heightChangeRight. - * + * * @return the subtree's new root */ private Node singleRotateLeft() { Node b = right; - + Highlights.markArray(3, pointer); Highlights.markArray(4, b.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); right = b.left; b.left = this; Writes.stopLap(); Delays.sleep(0.25); - + balance = 0; b.balance = 0; - + Highlights.clearMark(3); Highlights.clearMark(4); - + return b; } - + /** * Singly rotates the subtree right about the current node, updating balance - * codes along the way. Also known as a left-right rotation. Helper method for + * codes along the way. Also known as a left-right rotation. Helper method for * heightChangeLeft. - * + * * @return the subtree's new root */ private Node doubleRotateRight() { int oldBBalance = left.right.balance; - + Node newLeft = left.singleRotateLeft(); Writes.changeAuxWrites(1); Writes.startLap(); left = newLeft; Writes.stopLap(); Delays.sleep(0.25); - + Node b = singleRotateRight(); - + if(oldBBalance == -1) b.right.balance = 1; if(oldBBalance == 1) b.left.balance = -1; - + return b; } - + /** * Singly rotates the subtree left about the current node, updating balance - * codes along the way. Also known as a right-left rotation. Helper method for + * codes along the way. Also known as a right-left rotation. Helper method for * heightChangeRight. - * + * * @return the subtree's new root */ private Node doubleRotateLeft() { int oldBBalance = right.left.balance; - + Node newRight = right.singleRotateRight(); Writes.changeAuxWrites(1); Writes.startLap(); right = newRight; Writes.stopLap(); Delays.sleep(0.25); - + Node b = singleRotateLeft(); - + if(oldBBalance == -1) b.right.balance = 1; if(oldBBalance == 1) b.left.balance = -1; - + return b; } /** * Performs an in-order traversal of the array and writes * the values of the original array to a sorted temporary array - * + * * @param tempArray the temporary array to write the contents of the subtree to * @param location a pointer to the location in the temporary array to which the * contents of the current subtree should be written to @@ -326,38 +326,38 @@ private Node doubleRotateLeft() { */ private int writeToArray(int[] tempArray, int location) { if(this == NULL_NODE) return 0; - + int leftTreeSize = left.writeToArray(tempArray, location); int newLocation = location + leftTreeSize; - + Highlights.markArray(1, pointer); Writes.write(tempArray, newLocation, array[pointer], 0.1, false, true); - + int rightTreeSize = right.writeToArray(tempArray, newLocation + 1); return leftTreeSize + rightTreeSize + 1; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { // Tells the tree what array is to be sorted NULL_NODE.array = array; // Creates a tree of size 0, to which all elements will be added Node root = NULL_NODE; - + // This loop adds every element of the array to be sorted into the tree for(int i = 0; i < length; i++) { Highlights.markArray(1, i); // Highlights the element being added Node.AddContainer container = root.add(i); - + Highlights.clearMark(2); - + Writes.changeAuxWrites(1); Writes.startLap(); root = container.node; Writes.stopLap(); Delays.sleep(0.25); - + Highlights.clearAllMarks(); // Clearing all just in case } diff --git a/src/main/java/sorts/insert/BinaryDoubleInsertionSort.java b/src/main/java/sorts/insert/BinaryDoubleInsertionSort.java index 0126fa60..27f8f03a 100644 --- a/src/main/java/sorts/insert/BinaryDoubleInsertionSort.java +++ b/src/main/java/sorts/insert/BinaryDoubleInsertionSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2020-2021 Gaming32 Morewenn and aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BinaryDoubleInsertionSort extends Sort { +public final class BinaryDoubleInsertionSort extends Sort { public BinaryDoubleInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binary Double Insertion"); this.setRunAllSortsName("Binary Double Insertion Sort"); this.setRunSortName("Binary Double Insertsort"); @@ -44,71 +44,71 @@ public BinaryDoubleInsertionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int leftBinarySearch(int[] array, int a, int b, int val, double sleep) { while(a < b) { int m = a+(b-a)/2; - + Highlights.markArray(1, a); Highlights.markArray(2, m); Highlights.markArray(3, b); Delays.sleep(sleep); - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; else a = m+1; } - + return a; } private int rightBinarySearch(int[] array, int a, int b, int val, double sleep) { while(a < b) { int m = a+(b-a)/2; - + Highlights.markArray(1, a); Highlights.markArray(2, m); Highlights.markArray(3, b); Delays.sleep(sleep); - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; else a = m+1; } - + return a; } - + private void insertToLeft(int[] array, int a, int b, int temp, double sleep) { Highlights.clearMark(2); - + while(a > b) Writes.write(array, a, array[--a], sleep, true, false); Writes.write(array, b, temp, sleep, true, false); } private void insertToRight(int[] array, int a, int b, int temp, double sleep) { Highlights.clearMark(2); - + while(a < b) Writes.write(array, a, array[++a], sleep, true, false); Writes.write(array, a, temp, sleep, true, false); } - + public void doubleInsertion(int[] array, int a, int b, double compSleep, double sleep) { if(b-a < 2) return; - + int j = a+(b-a-2)/2+1, i = a+(b-a-1)/2; - + if(j > i && Reads.compareIndices(array, i, j, compSleep, true) == 1) Writes.swap(array, i, j, compSleep, true, false); - + i--; j++; - + while(j < b) { if(Reads.compareIndices(array, i, j, compSleep, true) == 1) { int l = array[j]; int r = array[i]; - + int m = this.rightBinarySearch(array, i+1, j, l, compSleep); this.insertToRight(array, i, m-1, l, sleep); this.insertToLeft(array, j, this.leftBinarySearch(array, m, j, r, compSleep), r, sleep); @@ -116,7 +116,7 @@ public void doubleInsertion(int[] array, int a, int b, double compSleep, double else { int l = array[i]; int r = array[j]; - + int m = this.leftBinarySearch(array, i+1, j, l, compSleep); this.insertToRight(array, i, m-1, l, sleep); this.insertToLeft(array, j, this.rightBinarySearch(array, m, j, r, compSleep), r, sleep); @@ -126,13 +126,13 @@ public void doubleInsertion(int[] array, int a, int b, double compSleep, double } Highlights.clearAllMarks(); } - + public void customDoubleInsert(int[] array, int a, int b, double sleep) { this.doubleInsertion(array, a, b, sleep, sleep); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.doubleInsertion(array, 0, currentLength, 1, 0.05); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/BinaryInsertionSort.java b/src/main/java/sorts/insert/BinaryInsertionSort.java index 00487230..d3f4dfbf 100644 --- a/src/main/java/sorts/insert/BinaryInsertionSort.java +++ b/src/main/java/sorts/insert/BinaryInsertionSort.java @@ -4,7 +4,7 @@ import sorts.templates.BinaryInsertionSorting; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BinaryInsertionSort extends BinaryInsertionSorting { +public final class BinaryInsertionSort extends BinaryInsertionSorting { public BinaryInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -48,9 +48,9 @@ public BinaryInsertionSort(ArrayVisualizer arrayVisualizer) { public void customBinaryInsert(int[] array, int start, int end, double sleep) { this.binaryInsertSort(array, start, end, sleep, sleep); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.binaryInsertSort(array, 0, currentLength, 1, 0.05); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/BlockInsertionSort.java b/src/main/java/sorts/insert/BlockInsertionSort.java index 739603cd..77b0cf4b 100644 --- a/src/main/java/sorts/insert/BlockInsertionSort.java +++ b/src/main/java/sorts/insert/BlockInsertionSort.java @@ -4,10 +4,10 @@ import sorts.templates.GrailSorting; import utils.Rotations; -final public class BlockInsertionSort extends GrailSorting { +public final class BlockInsertionSort extends GrailSorting { public BlockInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Block Insertion"); this.setRunAllSortsName("Block Insertion Sort"); this.setRunSortName("Block Insertsort"); @@ -79,9 +79,9 @@ public void insertionSort(int[] array, int a, int b) { i = j; } } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { insertionSort(array, 0, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/ClassicTreeSort.java b/src/main/java/sorts/insert/ClassicTreeSort.java index 9e1a7c23..67c2f04e 100644 --- a/src/main/java/sorts/insert/ClassicTreeSort.java +++ b/src/main/java/sorts/insert/ClassicTreeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -28,11 +28,11 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. * */ - -final public class ClassicTreeSort extends Sort { + +public final class ClassicTreeSort extends Sort { public ClassicTreeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Classic Tree"); this.setRunAllSortsName("Classic Unbalanced Tree Sort"); this.setRunSortName("Classic Unbalanced Treesort"); @@ -44,19 +44,19 @@ public ClassicTreeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int idx; - + private void traverse(int[] array, int[] temp, int[] lower, int[] upper, int r) { Highlights.markArray(1, r); Delays.sleep(1); - + if(lower[r] != 0) this.traverse(array, temp, lower, upper, lower[r]); - + Writes.write(temp, this.idx++, array[r], 0, false, true); Highlights.markArray(1, r); Delays.sleep(1); - + if(upper[r] != 0) this.traverse(array, temp, lower, upper, upper[r]); } @@ -65,17 +65,17 @@ public void runSort(int[] array, int currentLength, int bucketCount) { int[] lower = Writes.createExternalArray(currentLength); int[] upper = Writes.createExternalArray(currentLength); int[] next; - + for(int i = 1; i < currentLength; i++) { Highlights.markArray(2, i); int c = 0; - + while(true) { Highlights.markArray(1, c); Delays.sleep(0.5); - + next = Reads.compareValues(array[i], array[c]) < 0 ? lower : upper; - + if(next[c] == 0) { Writes.write(next, c, i, 0, false, true); break; @@ -84,14 +84,14 @@ public void runSort(int[] array, int currentLength, int bucketCount) { } } Highlights.clearMark(2); - + int[] temp = Writes.createExternalArray(currentLength); this.idx = 0; this.traverse(array, temp, lower, upper, 0); Writes.arraycopy(temp, 0, array, 0, currentLength, 1, true, false); - + Writes.deleteExternalArray(lower); Writes.deleteExternalArray(upper); Writes.deleteExternalArray(temp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/DoubleInsertionSort.java b/src/main/java/sorts/insert/DoubleInsertionSort.java index 29a3e793..0b65ae11 100644 --- a/src/main/java/sorts/insert/DoubleInsertionSort.java +++ b/src/main/java/sorts/insert/DoubleInsertionSort.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DoubleInsertionSort extends Sort { +public final class DoubleInsertionSort extends Sort { public DoubleInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -115,4 +115,4 @@ public void customInsertSort(int[] array, int start, int end, double sleep, bool public void runSort(int[] array, int currentLength, int bucketCount) { this.insertionSort(array, 0, currentLength, 0.015, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/HanoiSort.java b/src/main/java/sorts/insert/HanoiSort.java index 476a6a7d..6a1d1ff3 100644 --- a/src/main/java/sorts/insert/HanoiSort.java +++ b/src/main/java/sorts/insert/HanoiSort.java @@ -7,7 +7,7 @@ /** * Hanoi Sort, a sort inspired by the classic Tower of Hanoi puzzle.

- * + * * This sort started as a random idea I had, with a vague idea of how I could approach * the problem. And this is, in fact, the first complete implementation that I know of. * However, I was greatly helped by two people before me. The first was Andrei "Mrrl" @@ -20,14 +20,14 @@ * in particular, for the aforementioned iterative hanoi function to work, I had to * ensure all consecutive identical items were moved together. But I made it work, and * thus here I present the first implementation of this sort that I am aware of. - * + * * @author Sam Walko (Anonymous0726) * @see * Mrrl's Hanoi Sort pseudocode * @see * Iterative algorithm for Tower of Hanoi problem */ -final public class HanoiSort extends Sort { +public final class HanoiSort extends Sort { // main array private int[] array; // Length of the array @@ -44,7 +44,7 @@ final public class HanoiSort extends Sort { public HanoiSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Hanoi"); this.setRunAllSortsName("Hanoi Sort"); this.setRunSortName("Hanoi sort"); @@ -64,15 +64,15 @@ public HanoiSort(ArrayVisualizer arrayVisualizer) { private void removeFromMainStack() { Highlights.markArray(2, sp); target = array[sp]; - + int moves = hanoi(2, true, 1); int height = getHeight(moves + 1); targetMoves = moves; boolean evenHeight = height % 2 == 0; - + Highlights.clearAllMarks(); Highlights.markArray(1, sp); - + if(evenHeight) // Move smaller elements to stack3, if necessary hanoi(1, true, 2); unsorted += moveFromMain(stack2, false); // Move next element(s) to stack2 @@ -92,12 +92,12 @@ private void returnToMainStack() { hanoi(3, true, 2); } // In even case, it's already on main stack, and we need not do anything } - + /** * Iteratively moves elements around the stacks according to the * Tower of Hanoi problem. Recursion cannot be used because in many * cases we do not know the initial recursion depth. - * + * * @param startStack Which stack we wish to move a tower from * @param goRight Whether the smallest disk should go right on the pegs * (1->2, 2->3, 3->1) or left (1->3, 2->1, 3->2) @@ -107,7 +107,7 @@ private void returnToMainStack() { private int hanoi(int startStack, boolean goRight, int endCon) { int moves = 0; int minPoleLoc = startStack; - + if(!endConMet(endCon, moves)) { moves++; switch(minPoleLoc) { @@ -137,7 +137,7 @@ private int hanoi(int startStack, boolean goRight, int endCon) { } break; } } - + while(!endConMet(endCon, moves)) { moves += 2; switch(minPoleLoc) { @@ -182,13 +182,13 @@ private int hanoi(int startStack, boolean goRight, int endCon) { } break; } } - + return moves; } - + /** * Determines whether or not the hanoi function should end now - * + * * @param endCon Which ending condition is required for the hanoi function to end * @param moves the moves completed by the hanoi function so far * @return Whether or not the end condition has been met @@ -218,7 +218,7 @@ private boolean validNumberMoves(int moves) { /** * Figures out the height of the pyramid moved based on number of moves - * + * * @param movesPlus1 The number of moves performed, plus one * @return The height of the pyramid moved (equal to log_2(moves + 1)) */ @@ -227,11 +227,11 @@ private int getHeight(int movesPlus1) { return 0; return getHeight(movesPlus1 / 2) + 1; } - + /** * Moves an element from the main array to another stack, * then moves any consecutive duplicates of that element with it - * + * * @param stack The stack to move the element(s) from the main array to * @param checkUnsorted Whether or not it is safe to remove elements in * the unsorted portion of the main array @@ -264,11 +264,11 @@ private int moveFromMain(Stack stack, boolean checkUnsorted) { } return duplicates; } - + /** * Moves an element to the main array from another stack, * then moves any consecutive duplicates of that element with it - * + * * @param stack The stack to move the element(s) to the main array from */ private void moveToMain(Stack stack) { @@ -283,11 +283,11 @@ private void moveToMain(Stack stack) { Writes.write(array, sp, stack.pop(), 0.25, false, false); } } - + /** * Moves an element from one stack to another stack, * then moves any consecutive duplicates of that element with it - * + * * @param from the stack to move the element(s) from * @param to the stack to move the element(s) to */ @@ -307,8 +307,8 @@ private void moveBetweenStacks(Stack from, Stack to) { Delays.sleep(0.25); } } - - + + @Override public void runSort(int[] array, int length, int bucketCount) { // Initialize local variables @@ -318,10 +318,10 @@ public void runSort(int[] array, int length, int bucketCount) { stack3 = new Stack(); sp = 0; unsorted = 0; - + while(unsorted < length) removeFromMainStack(); - + returnToMainStack(); } } diff --git a/src/main/java/sorts/insert/InsertionSort.java b/src/main/java/sorts/insert/InsertionSort.java index db0463ca..3669b097 100644 --- a/src/main/java/sorts/insert/InsertionSort.java +++ b/src/main/java/sorts/insert/InsertionSort.java @@ -4,7 +4,7 @@ import sorts.templates.InsertionSorting; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class InsertionSort extends InsertionSorting { +public final class InsertionSort extends InsertionSorting { public InsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Insertion"); this.setRunAllSortsName("Insertion Sort"); this.setRunSortName("Insertsort"); @@ -48,9 +48,9 @@ public InsertionSort(ArrayVisualizer arrayVisualizer) { public void customInsertSort(int[] array, int start, int end, double sleep, boolean auxwrite) { this.insertionSort(array, start, end, sleep, auxwrite); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.insertionSort(array, 0, currentLength, 0.015, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/LibrarySort.java b/src/main/java/sorts/insert/LibrarySort.java index 6bafbb18..e99a75ce 100644 --- a/src/main/java/sorts/insert/LibrarySort.java +++ b/src/main/java/sorts/insert/LibrarySort.java @@ -7,7 +7,7 @@ import java.util.Random; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -32,10 +32,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class LibrarySort extends Sort { +public final class LibrarySort extends Sort { public LibrarySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Library"); this.setRunAllSortsName("Library Sort"); this.setRunSortName("Library Sort"); @@ -47,170 +47,170 @@ public LibrarySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + //possible implementation of the library sort here https://en.wikipedia.org/wiki/Library_sort //makes O(1) insertions into gaps of constant size on random data using an extra (1+G)*n space - + private final int G = 15; private final int R = 4; - + private int max; - + private void shiftExt(int[] array, int a, int m, int b) { int m1 = a + Math.min(m-a, b-m); while(m > a) Writes.write(array, --b, array[--m], 0.5, false, true); while(a < m1) Writes.write(array, a++, this.max, 0.5, false, true); } - + private int leftBlockSearch(int[] array, int a, int b, int val) { int s = G+1; - + while(a < b) { int m = a+(((b-a)/s)/2)*s; Highlights.markArray(2, m/s); Delays.sleep(0.25); - - if(Reads.compareValues(val, array[m]) <= 0) + + if(Reads.compareValues(val, array[m]) <= 0) b = m; else a = m+s; } - + Highlights.clearMark(2); return a; } private int rightBlockSearch(int[] array, int a, int b, int val) { int s = G+1; - + while(a < b) { int m = a+(((b-a)/s)/2)*s; Highlights.markArray(2, m/s); Delays.sleep(0.25); - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; else a = m+s; } - + Highlights.clearMark(2); return a; } - + private int locSearch(int[] array, int a, int b) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareOriginalValues(this.max, array[m]) <= 0) + + if(Reads.compareOriginalValues(this.max, array[m]) <= 0) b = m; - else + else a = m+1; } - + return a; } private int rightBinSearch(int[] array, int a, int b, int val) { while(a < b) { int m = a+(b-a)/2; - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; else a = m+1; } - + return a; } - + private void insertTo(int[] array, int a, int b, boolean aux) { Highlights.clearMark(2); int temp = array[a]; while(a > b) Writes.write(array, a, array[--a], 0.5, !aux, aux); Writes.write(array, b, temp, 0.5, !aux, aux); } - + private void binaryInsertion(int[] array, int a, int b) { for(int i = a+1; i < b; i++) this.insertTo(array, i, this.rightBinSearch(array, a, i, array[i]), false); } - + private void retrieve(int[] array, int[] tmp, int i, int pEnd) { - int loc = i-1; - + int loc = i-1; + for(int k = pEnd-(G+1); k > G;) { int m = this.locSearch(tmp, k-G, k)-1; k -= G+1; - + while(m >= k) { Writes.write(array, loc--, tmp[m], 0, true, false); Writes.write(tmp, m--, max, 1, false, true); } } - + int m = this.locSearch(tmp, 0, G)-1; while(m >= 0) { Writes.write(array, loc--, tmp[m], 0, true, false); Writes.write(tmp, m--, max, 1, false, true); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.max = length; - + //there is supposed to be a shuffle here between [0, length) //but for the sake of demonstrating O(n^2) worst case it has been removed - + int[] tmp = Writes.createExternalArray(length*(G+1)-1); Arrays.fill(tmp, this.max); //(o_o;) - + Random rng = new Random(); - + int s = length; while(s >= 32) s = (s-1)/R + 1; - + int i = s, j = R*i, pEnd = (s+1)*(G+1)+G; this.binaryInsertion(array, 0, s); - + for(int k = 0; k < s; k++) { Highlights.markArray(1, k); Writes.write(tmp, k*(G+1)+G, array[k], 1, false, true); } - + for(; i < length; i++) { if(i == j) { this.retrieve(array, tmp, i, pEnd); - + s = i; pEnd = (s+1)*(G+1)+G; j *= R; - + for(int k = 0; k < s; k++) { Highlights.markArray(1, k); Writes.write(tmp, k*(G+1)+G, array[k], 1, false, true); } } - + Highlights.markArray(1, i); int bLoc = this.leftBlockSearch(tmp, G, pEnd-(G+1), array[i]); - + if(Reads.compareValues(array[i], tmp[bLoc]) == 0) { int eqEnd = this.rightBlockSearch(tmp, bLoc+(G+1), pEnd-(G+1), array[i]); bLoc += rng.nextInt((eqEnd-bLoc)/(G+1))*(G+1); } int loc = this.locSearch(tmp, bLoc-G, bLoc); - + if(loc == bLoc) { do bLoc += G+1; while(bLoc < pEnd && this.locSearch(tmp, bLoc-G, bLoc) == bLoc); - + if(bLoc == pEnd) { this.retrieve(array, tmp, i, pEnd); - + s = i; pEnd = (s+1)*(G+1)+G; j = R*i; - + for(int k = 0; k < s; k++) { Highlights.markArray(1, k); Writes.write(tmp, k*(G+1)+G, array[k], 1, false, true); @@ -219,7 +219,7 @@ public void runSort(int[] array, int length, int bucketCount) { else { int rotP = this.locSearch(tmp, bLoc-G, bLoc); int rotS = bLoc - Math.max(rotP, bLoc - G/2); - this.shiftExt(tmp, loc-rotS, bLoc-rotS, bLoc); + this.shiftExt(tmp, loc-rotS, bLoc-rotS, bLoc); } i--; } @@ -231,4 +231,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.retrieve(array, tmp, length, pEnd); Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/PatienceSort.java b/src/main/java/sorts/insert/PatienceSort.java index 88b80bdc..696e5af2 100644 --- a/src/main/java/sorts/insert/PatienceSort.java +++ b/src/main/java/sorts/insert/PatienceSort.java @@ -20,10 +20,10 @@ * */ -final public class PatienceSort extends Sort { +public final class PatienceSort extends Sort { public PatienceSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Patience"); this.setRunAllSortsName("Patience Sort"); this.setRunSortName("Patience Sort"); @@ -35,7 +35,7 @@ public PatienceSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + final private class Pile extends Stack implements Comparable { private static final long serialVersionUID = 1L; @@ -48,26 +48,26 @@ public int compareTo(Pile y) { return Reads.compareValues(peek(), y.peek()); } } - + private void binarySearch(ArrayList list, Pile find) { int at = list.size() / 2; int change = list.size() / 4; - + long compsBefore = Reads.getComparisons(); while(list.get(at).compare(find) != 0 && change > 0){ Reads.setComparisons(compsBefore); Highlights.markArray(1, at); Delays.sleep(0.5); - + if(list.get(at).compare(find) < 0) at += change; else at -= change; - + change /= 2; } Reads.setComparisons(compsBefore); - + Highlights.markArray(1, at); Delays.sleep(0.5); } @@ -75,17 +75,17 @@ private void binarySearch(ArrayList list, Pile find) { @Override public void runSort(int[] array, int length, int bucketCount) { ArrayList piles = new ArrayList<>(); - + // sort into piles for (int x = 0; x < length; x++) { Pile newPile = new Pile(); - + Highlights.markArray(2, x); Writes.mockWrite(length, Math.min(newPile.size(), length - 1), array[x], 1); - + newPile.push(array[x]); Writes.changeAllocAmount(1); - + int i = Collections.binarySearch(piles, newPile); if(!piles.isEmpty()) { this.binarySearch(piles, newPile); @@ -97,7 +97,7 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.changeAllocAmount(1); } else { - Writes.mockWrite(length, Math.min(piles.size(), length - 1), newPile.get(0), 0); + Writes.mockWrite(length, Math.min(piles.size(), length - 1), newPile.get(0), 0); piles.add(newPile); Writes.changeAllocAmount(1); } @@ -107,15 +107,15 @@ public void runSort(int[] array, int length, int bucketCount) { // priority queue allows us to retrieve least pile efficiently PriorityQueue heap = new PriorityQueue<>(piles); - + for (int c = 0; c < length; c++) { Writes.mockWrite(length, Math.min(heap.size(), length - 1), 0, 0); Pile smallPile = heap.poll(); - + Writes.mockWrite(length, Math.min(smallPile.size(), length - 1), 0, 0); Writes.write(array, c, smallPile.pop(), 1, true, false); Writes.changeAllocAmount(-1); - + if (!smallPile.isEmpty()) { Writes.mockWrite(length, Math.min(heap.size(), length - 1), smallPile.get(0), 0); heap.offer(smallPile); @@ -125,4 +125,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.clearAllocAmount(); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/RecursiveShellSort.java b/src/main/java/sorts/insert/RecursiveShellSort.java index 0d0a87f5..f7ace0dd 100644 --- a/src/main/java/sorts/insert/RecursiveShellSort.java +++ b/src/main/java/sorts/insert/RecursiveShellSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class RecursiveShellSort extends Sort { +public final class RecursiveShellSort extends Sort { public RecursiveShellSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Recursive Shell"); this.setRunAllSortsName("yuji's Recursive Shell Sort"); this.setRunSortName("yuji's Recursive Shell Sort"); @@ -23,7 +23,7 @@ public void gappedInsertionSort(int[] arr, int a, int b, int gap) { for (int i = a+gap; i < b; i+=gap) { int key = arr[i]; int j = i-gap; - + while (j >= a && Reads.compareValues(key, arr[j]) < 0) { Writes.write(arr, j+gap, arr[j], 0.5, true, false); j-=gap; @@ -46,4 +46,4 @@ public void recursiveShellSort(int[] array, int start, int end, int g) { public void runSort(int[] array, int length, int bucketCount) { this.recursiveShellSort(array, 0, length, 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/RedBlackTreeSort.java b/src/main/java/sorts/insert/RedBlackTreeSort.java index 81989e0d..1a1bf0df 100644 --- a/src/main/java/sorts/insert/RedBlackTreeSort.java +++ b/src/main/java/sorts/insert/RedBlackTreeSort.java @@ -6,13 +6,13 @@ /** * An implementation of a tree sort using an red-black tree, * based on what I learned in my CSSE230 class - * + * * @author Sam Walko (Anonymous0726) */ -final public class RedBlackTreeSort extends Sort { +public final class RedBlackTreeSort extends Sort { public RedBlackTreeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Red-Black Tree"); this.setRunAllSortsName("Tree Sort (Red-Black Balanced)"); this.setRunSortName("Tree sort (Red-Black Balanced)"); @@ -24,9 +24,9 @@ public RedBlackTreeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private Node NULL_NODE = new Node(); // (sub)tree of size 0 - + /** * The fundamental building block of any programming tree. Each node is the * root of its own subtree. In general, a node has the data being stored at @@ -37,11 +37,11 @@ public RedBlackTreeSort(ArrayVisualizer arrayVisualizer) { private class Node { // main array (poor encapsulation since it's all the same but w/e) private int[] array; - - private int pointer; // index in main array of the element contained here + + private int pointer; // index in main array of the element contained here private Node left, right; // left and right subtrees private boolean isRed; // whether this node is red or black - + // Default constructor, and constructor for NULL_NODE private Node() { // Shouldn't point to anything by default @@ -50,7 +50,7 @@ private Node() { this.right = NULL_NODE; this.isRed = false; // All NULL_NODEs being black will not violate black balance } - + // Constructor for a node with a pointer private Node(int[] array, int pointer) { this(); @@ -58,7 +58,7 @@ private Node(int[] array, int pointer) { this.pointer = pointer; this.isRed = true; // A node with data, when first created, is red } - + /** * A return container for the recursive add method containing a Node * and a boolean telling whether or not the subtree needs rebalancing @@ -66,16 +66,16 @@ private Node(int[] array, int pointer) { private class AddContainer { private Node node; private boolean needsFix; - + private AddContainer(Node node, boolean needsFix) { this.node = node; this.needsFix = needsFix; } } - + /** * Recursively adds an element to the subtree whose root is this node - * + * * @param addPointer A pointer to the array telling what element is to be inserted * @return an AddContainer containing the node that is now the root of this * subtree, and the boolean telling whether or not this subtree increased in height @@ -89,12 +89,12 @@ private AddContainer add(int addPointer) { // obviously does not require rebalancing return new AddContainer(newNode, false); } - + // If there's an element already here, we need to compare them to // determine whether it should be placed in the left or the right subtree. // Thus, mark the element at the pointer for comparison. Highlights.markArray(2, pointer); - + // This type of restructuring allows us to prevent future // cascading rotations, without losing black balance. // (Note that this only occurs when there's actually a node here @@ -102,32 +102,32 @@ private AddContainer add(int addPointer) { if(!isRed && left.isRed && right.isRed) { Highlights.markArray(3, left.pointer); Highlights.markArray(4, right.pointer); - + isRed = true; left.isRed = false; right.isRed = false; - + Delays.sleep(0.25); Highlights.clearMark(3); Highlights.clearMark(4); } - + // Case 2: The element is smaller and thus belongs in the left subtree if(Reads.compareValues(array[addPointer], array[pointer]) == -1) { Delays.sleep(0.25); - + // Recursively get the root of the new left subtree AddContainer container = left.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new left subtree as such Writes.changeAuxWrites(1); Writes.startLap(); left = container.node; Writes.stopLap(); Delays.sleep(0.05); - + // If the tree is determined to need rebalancing, then first // determine which type of rotation is necessary, then perform it. if(container.needsFix) { @@ -135,28 +135,28 @@ private AddContainer add(int addPointer) { return new AddContainer(singleRotateRight(), false); return new AddContainer(doubleRotateRight(), false); } - + // If no rebalancing is necessary here, return this node, and determine // whether or not the subtree at the parent will require rebalancing return new AddContainer(this, isRed && left.isRed); } - + // Case 3: The element is equal or larger and thus belongs in the right subtree // Note: As equality also results in right subtree, this sort is stable Delays.sleep(0.25); - + // Recursively get the root of the new right subtree AddContainer container = right.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new right subtree as such Writes.changeAuxWrites(1); Writes.startLap(); right = container.node; Writes.stopLap(); Delays.sleep(0.05); - + // If the tree is determined to need rebalancing, then first // determine which type of rotation is necessary, then perform it. if(container.needsFix) { @@ -164,119 +164,119 @@ private AddContainer add(int addPointer) { return new AddContainer(singleRotateLeft(), false); return new AddContainer(doubleRotateLeft(), false); } - + // If no rebalancing is necessary here, return this node, and determine // whether or not the subtree at the parent will require rebalancing return new AddContainer(this, isRed && right.isRed); } - + // These rotations are kinda magic (read: too difficult to explain without // diagrams), so feel free to ask me (Anonymous0726#2452 on Discord) about it. // Alternatively, the animations on Wikipedia for AVL trees are fairly decent // at showing what happens during any given type of rotation, but don't really // show what triggers a rotation here in a red-black tree, nor do they explain // the recoloring. - + /** * Singly rotates the subtree right about the current node, * recoloring along the way. Helper method for add. - * + * * @return the subtree's new root */ private Node singleRotateRight() { Node b = left; - + Highlights.markArray(3, pointer); Highlights.markArray(4, b.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); left = b.right; b.right = this; Writes.stopLap(); Delays.sleep(0.25); - + b.isRed = false; isRed = true; - + Highlights.clearMark(3); Highlights.clearMark(4); - + return b; } - + /** * Singly rotates the subtree left about the current node, * recoloring along the way. Helper method for add. - * + * * @return the subtree's new root */ private Node singleRotateLeft() { Node b = right; - + Highlights.markArray(3, pointer); Highlights.markArray(4, b.pointer); - + Writes.changeAuxWrites(2); Writes.startLap(); right = b.left; b.left = this; Writes.stopLap(); Delays.sleep(0.25); - + b.isRed = false; isRed = true; - + Highlights.clearMark(3); Highlights.clearMark(4); - + return b; } - + /** * Doubly rotates the subtree right about the current node, recoloring along * the way. Also known as a left-right rotation. Helper method for add. - * + * * @return the subtree's new root */ private Node doubleRotateRight() { - + Node newLeft = left.singleRotateLeft(); Writes.changeAuxWrites(1); Writes.startLap(); left = newLeft; Writes.stopLap(); Delays.sleep(0.25); - + Node b = singleRotateRight(); - + return b; } - + /** * Double rotates the subtree left about the current node, recoloring along * the way. Also known as a right-left rotation. Helper method for add. - * + * * @return the subtree's new root */ private Node doubleRotateLeft() { - + Node newRight = right.singleRotateRight(); Writes.changeAuxWrites(1); Writes.startLap(); right = newRight; Writes.stopLap(); Delays.sleep(0.25); - + Node b = singleRotateLeft(); - + return b; } /** * Performs an in-order traversal of the array and writes * the values of the original array to a sorted temporary array - * + * * @param tempArray the temporary array to write the contents of the subtree to * @param location a pointer to the location in the temporary array to which the * contents of the current subtree should be written to @@ -285,42 +285,42 @@ private Node doubleRotateLeft() { */ private int writeToArray(int[] tempArray, int location) { if(this == NULL_NODE) return 0; - + int leftTreeSize = left.writeToArray(tempArray, location); int newLocation = location + leftTreeSize; - + Highlights.markArray(1, pointer); Writes.write(tempArray, newLocation, array[pointer], 0.1, false, true); - + int rightTreeSize = right.writeToArray(tempArray, newLocation + 1); return leftTreeSize + rightTreeSize + 1; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { // Tells the tree what array is to be sorted NULL_NODE.array = array; // Creates a tree of size 0, to which all elements will be added Node root = NULL_NODE; - + // This loop adds every element of the array to be sorted into the tree for(int i = 0; i < length; i++) { Highlights.markArray(1, i); // Highlights the element being added Node.AddContainer container = root.add(i); - + Highlights.clearMark(2); - + Writes.changeAuxWrites(1); Writes.startLap(); root = container.node; Writes.stopLap(); Delays.sleep(0.25); - + Highlights.markArray(2, root.pointer); root.isRed = false; // Root of a red-black tree must always be black Delays.sleep(0.25); - + Highlights.clearAllMarks(); // Clearing all just in case } diff --git a/src/main/java/sorts/insert/ShellSort.java b/src/main/java/sorts/insert/ShellSort.java index 231ea1c5..0f1ad49f 100644 --- a/src/main/java/sorts/insert/ShellSort.java +++ b/src/main/java/sorts/insert/ShellSort.java @@ -6,10 +6,10 @@ // Shell sort variant retrieved from: // https://www.cs.princeton.edu/~rs/talks/shellsort.ps -final public class ShellSort extends ShellSorting { +public final class ShellSort extends ShellSorting { public ShellSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Shell"); this.setRunAllSortsName("Shell Sort"); this.setRunSortName("Shellsort"); @@ -21,13 +21,13 @@ public ShellSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + public void finishQuickShell(int[] array, int currentLen) { this.quickShellSort(array, 0, currentLen); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.shellSort(array, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/ShellSortParallel.java b/src/main/java/sorts/insert/ShellSortParallel.java index b45a3f3f..8199c9fc 100644 --- a/src/main/java/sorts/insert/ShellSortParallel.java +++ b/src/main/java/sorts/insert/ShellSortParallel.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.ShellSorting; -final public class ShellSortParallel extends ShellSorting { +public final class ShellSortParallel extends ShellSorting { public ShellSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Shell (Parallel)"); this.setRunAllSortsName("Parallel Shell Sort"); this.setRunSortName("Parallel Shellsort"); @@ -18,10 +18,10 @@ public ShellSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; private int[] gaps; - + private class GappedInsertionSort extends Thread { private int a, b, g; GappedInsertionSort(int a, int b, int g) { @@ -33,49 +33,49 @@ public void run() { ShellSortParallel.this.gappedInsertion(a, b, g); } } - + private void gappedInsertion(int a, int b, int g) { for(int i = a+g; i < b; i+=g) { if(Reads.compareIndices(this.array, i-g, i, 1, true) > 0) { int tmp = this.array[i], j = i; Highlights.clearMark(2); - + do { Writes.write(this.array, j, this.array[j-g], 1, true, false); j -= g; } while(j-g >= a && Reads.compareValues(this.array[j-g], tmp) > 0); - + Writes.write(this.array, j, tmp, 1, true, false); } } } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.array = array; this.gaps = this.ExtendedCiuraGaps; - + int k = 0; - + for(; this.gaps[k] >= currentLength; k++); for(; k < this.gaps.length; k++) { int g = this.gaps[k]; int t = Math.min(g, currentLength-g); - + GappedInsertionSort[] ins = new GappedInsertionSort[t]; for(int i = 0; i < t; i++) ins[i] = new GappedInsertionSort(i, currentLength, g); - + for(GappedInsertionSort s : ins) s.start(); for(GappedInsertionSort s : ins) { try { s.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/SimplifiedLibrarySort.java b/src/main/java/sorts/insert/SimplifiedLibrarySort.java index 3c209355..9fddacd8 100644 --- a/src/main/java/sorts/insert/SimplifiedLibrarySort.java +++ b/src/main/java/sorts/insert/SimplifiedLibrarySort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020-2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SimplifiedLibrarySort extends Sort { +public final class SimplifiedLibrarySort extends Sort { public SimplifiedLibrarySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,40 +44,40 @@ public SimplifiedLibrarySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + //simple library sort with O(n) extra memory + counter & pointer array (n size combined) - + private final int R = 4; //rebalancing factor (gaps have dynamic size) - + private BinaryInsertionSort binaryInsert; - + private int getMinLevel(int n) { while(n >= 32) n = (n-1)/R+1; return n; } - + private int binarySearch(int[] array, int a, int b, int val, double sleep) { while(a < b) { int m = a+(b-a)/2; Highlights.markArray(3, m); Delays.sleep(sleep); - - if(Reads.compareValues(val, array[m]) < 0) + + if(Reads.compareValues(val, array[m]) < 0) b = m; else a = m+1; } Highlights.clearMark(3); - + return a; } - + private void rebalance(int[] array, int[] temp, int[] cnts, int[] locs, int m, int b) { //do a partial sum to find locations Highlights.clearMark(2); for(int i = 0; i < m; i++) Writes.write(cnts, i+1, cnts[i+1]+cnts[i]+1, 1, true, true); - + //place books in gaps into their correct locations for(int i = m, j = 0; i < b; i++, j++) { Highlights.markArray(2, i); @@ -90,56 +90,56 @@ private void rebalance(int[] array, int[] temp, int[] cnts, int[] locs, int m, i Writes.write(cnts, i, cnts[i]+1, 0, false, true); } Highlights.clearMark(2); - + //copy back to array & sort the gaps Writes.arraycopy(temp, 0, array, 0, b, 1, true, false); this.binaryInsert.customBinaryInsert(array, 0, cnts[0]-1, 0.5); for(int i = 0; i < m-1; i++) this.binaryInsert.customBinaryInsert(array, cnts[i], cnts[i+1]-1, 0.5); this.binaryInsert.customBinaryInsert(array, cnts[m-1], cnts[m], 0.5); - + //reset count array - for(int i = 0; i < m+2; i++) + for(int i = 0; i < m+2; i++) Writes.write(cnts, i, 0, 0, false, true); } @Override public void runSort(int[] array, int length, int bucketCount) { this.binaryInsert = new BinaryInsertionSort(this.arrayVisualizer); - + if(length < 32) { this.binaryInsert.customBinaryInsert(array, 0, length, 1); return; } - + int j = this.getMinLevel(length); this.binaryInsert.customBinaryInsert(array, 0, j, 1); - + int maxLevel = j; for(; maxLevel*R < length; maxLevel *= R); - - int[] temp = Writes.createExternalArray(length), + + int[] temp = Writes.createExternalArray(length), cnts = Writes.createExternalArray(maxLevel+2), locs = Writes.createExternalArray(length-maxLevel); - + for(int i = j, k = 0; i < length; i++) { if(R*j == i) { this.rebalance(array, temp, cnts, locs, j, i); j = i; k = 0; } - + //search which gap a book goes and save the result Highlights.markArray(2, i); int loc = this.binarySearch(array, 0, j, array[i], 0.5); - + Writes.write(cnts, loc+1, cnts[loc+1]+1, 0, false, true); Writes.write(locs, k++, loc, 0, false, true); } this.rebalance(array, temp, cnts, locs, j, length); - + Writes.deleteExternalArray(temp); Writes.deleteExternalArray(cnts); Writes.deleteExternalArray(locs); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/SplaySort.java b/src/main/java/sorts/insert/SplaySort.java index 3ff88d3f..00f2acbc 100644 --- a/src/main/java/sorts/insert/SplaySort.java +++ b/src/main/java/sorts/insert/SplaySort.java @@ -3,14 +3,14 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class SplaySort extends Sort { - +public final class SplaySort extends Sort { + //Splay sort is an adaptive algorithm based on splay tree data structure //It is O(n log n) in the average/worst case, and O(n) in the best case when the data is mostly sorted - + public SplaySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Splay"); this.setRunAllSortsName("Splay Sort"); this.setRunSortName("Splaysort"); @@ -22,66 +22,66 @@ public SplaySort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // Splay tree code retrieved from https://www.geeksforgeeks.org/splay-tree-set-2-insert-delete/ - - final private class Node { + + final private class Node { int key; Node left, right; - public Node(int item) { + public Node(int item) { key = item; - left = right = null; + left = right = null; } - - } - - private Node root; + + } + + private Node root; private int index, length; private Node treeWrite(Node element, int at) { Node node = new Node(0); - + if(at > 0 && at < this.length) Highlights.markArray(1, at - 1); Writes.changeAuxWrites(1); Writes.startLap(); node = element; Writes.stopLap(); - + Delays.sleep(0.25); - + return node; } - + private Node leftRotate(Node x, int depth) { Node y = x.right; x.right = treeWrite(y.left, depth * 2 + 1); y.left = treeWrite(x, depth * 4 + 2); - return y; + return y; } - + private Node rightRotate(Node x, int depth) { Node y = x.left; x.left = treeWrite(y.right, depth * 2); y.right = treeWrite(x, depth * 4 + 1); - return y; + return y; } private void insert(int key) { this.root = this.treeWrite(insertRec(this.root, key, 1), 1); Writes.changeAllocAmount(1); - } - + } + private Node splay(Node root, int key, int depth) { if (root == null) { return root; } - + if (Reads.compareValues(root.key, key) == 1) { if (root.left == null) { return root; } - + if (Reads.compareValues(root.left.key, key) == 1) { root.left.left = treeWrite(splay(root.left.left, key, depth * 4), depth * 4); root = treeWrite(rightRotate(root, depth), depth); @@ -96,7 +96,7 @@ private Node splay(Node root, int key, int depth) { if (root.right == null) { return root; } - + if (Reads.compareValues(root.right.key, key) == 1) { root.right.left = treeWrite(splay(root.right.left, key, depth * 4 + 2), depth * 4 + 2); if (root.right.left != null) { @@ -109,16 +109,16 @@ private Node splay(Node root, int key, int depth) { return (root.right == null) ? root : leftRotate(root, depth); } } - - private Node insertRec(Node root, int key, int depth) { - if (root == null) { + + private Node insertRec(Node root, int key, int depth) { + if (root == null) { root = treeWrite(new Node(key), 1); - return root; - } - + return root; + } + root = splay(root, key, depth); Node n = new Node(key); - + if (Reads.compareValues(root.key, key) == 1) { n.right = treeWrite(root, 3); n.left = treeWrite(root.left, 2); @@ -129,30 +129,30 @@ private Node insertRec(Node root, int key, int depth) { root.right = treeWrite(null, depth * 2 + 1); } return n; - } + } - private void traverseRec(Node root, int[] array) { - if (root != null) { - this.traverseRec(root.left, array); - Writes.write(array, this.index++, root.key, 1, true, false); - this.traverseRec(root.right, array); - } - } + private void traverseRec(Node root, int[] array) { + if (root != null) { + this.traverseRec(root.left, array); + Writes.write(array, this.index++, root.key, 1, true, false); + this.traverseRec(root.right, array); + } + } private void treeIns(int arr[]) { for(int i = 0; i < this.length; i++) { Highlights.markArray(2, i); this.insert(arr[i]); - } + } Highlights.clearMark(2); } @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.length = currentLength; - this.treeIns(array); + this.treeIns(array); this.index = 0; - this.traverseRec(this.root, array); + this.traverseRec(this.root, array); Writes.changeAllocAmount(-length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/insert/TreeSort.java b/src/main/java/sorts/insert/TreeSort.java index 29c2d43b..8284eebd 100644 --- a/src/main/java/sorts/insert/TreeSort.java +++ b/src/main/java/sorts/insert/TreeSort.java @@ -6,13 +6,13 @@ /** * An implementation of a tree sort using an unbalanced * (aka naive) binary search tree (BST) - * + * * @author Sam Walko (Anonymous0726) */ -final public class TreeSort extends Sort { +public final class TreeSort extends Sort { public TreeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Tree"); this.setRunAllSortsName("Tree Sort (Unbalanced)"); this.setRunSortName("Tree sort (Unbalanced)"); @@ -24,9 +24,9 @@ public TreeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private Node NULL_NODE = new Node(); - + /** * The fundamental building block of any programming tree. Each node is the * root of its own subtree. In general, a node has the data being stored at @@ -36,10 +36,10 @@ public TreeSort(ArrayVisualizer arrayVisualizer) { private class Node { // main array (poor encapsulation since it's all the same but w/e) private int[] array; - + private int pointer; // index in main array of the element contained here private Node left, right; // left and right subtrees - + // Default constructor, and constructor for NULL_NODE private Node() { // Shouldn't point to anything by default @@ -47,17 +47,17 @@ private Node() { this.left = NULL_NODE; this.right = NULL_NODE; } - + // Constructor for a node with a pointer private Node(int[] array, int pointer) { this(); this.array = array; this.pointer = pointer; } - + /** * Recursively adds an element to the subtree whose root is this node - * + * * @param addPointer A pointer to the array telling what element is to be inserted * @return the Node to be added at this point */ @@ -67,44 +67,44 @@ private Node add(int addPointer) { Highlights.clearMark(1); // No longer comparing to previous leaves return new Node(array, addPointer); // Create the new node and return it } - + // If there's an element already here, we need to compare them to // determine whether it should be placed in the left or the right subtree. // Thus, mark the element at the pointer for comparison. Highlights.markArray(2, pointer); - + // Case 2: The element is smaller and thus belongs in the left subtree if(Reads.compareValues(array[addPointer], array[pointer]) == -1) { Delays.sleep(0.25); - + // Recursively get the root of the new left subtree Node newLeaf = left.add(addPointer); - + Highlights.markArray(2, pointer); - + // Set the root of the new left subtree as such Writes.changeAuxWrites(1); Writes.startLap(); left = newLeaf; Writes.stopLap(); - + Delays.sleep(0.05); return this; // return this node } - + // Case 3: The element is equal or larger and thus belongs in the right subtree // Note: As equality also results in right subtree, this sort is stable Delays.sleep(0.25); - + // Recursively get the root of the new right subtree Node newLeaf = right.add(addPointer); - + // Set the root of the new right subtree as such Writes.changeAuxWrites(1); Writes.startLap(); right = newLeaf; Writes.stopLap(); - + Delays.sleep(0.05); return this; // return this node } @@ -112,7 +112,7 @@ private Node add(int addPointer) { /** * Performs an in-order traversal of the array and writes * the values of the original array to a sorted temporary array - * + * * @param tempArray the temporary array to write the contents of the subtree to * @param location a pointer to the location in the temporary array to which the * contents of the current subtree should be written to @@ -121,41 +121,41 @@ private Node add(int addPointer) { */ private int writeToArray(int[] tempArray, int location) { if(this == NULL_NODE) return 0; - + int leftTreeSize = left.writeToArray(tempArray, location); int newLocation = location + leftTreeSize; - + Highlights.markArray(1, pointer); Writes.write(tempArray, newLocation, array[pointer], 0.1, false, true); - + int rightTreeSize = right.writeToArray(tempArray, newLocation + 1); return leftTreeSize + rightTreeSize + 1; } } - + @Override public void runSort(int[] array, int length, int bucketCount) { // Tells the tree what array is to be sorted NULL_NODE.array = array; // Creates a tree of size 0, to which all elements will be added Node root = NULL_NODE; - + // This loop adds every element of the array to be sorted into the tree for(int i = 0; i < length; i++) { Highlights.markArray(1, i); Node newRoot = root.add(i); - + Highlights.clearMark(2); - + Writes.changeAuxWrites(1); Writes.startLap(); root = newRoot; Writes.stopLap(); Delays.sleep(0.25); - + Highlights.clearAllMarks(); // Clearing all just in case } - + // Write the contents of the tree to a temporary array int[] tempArray = new int[length]; root.writeToArray(tempArray, 0); diff --git a/src/main/java/sorts/merge/AndreySort.java b/src/main/java/sorts/merge/AndreySort.java index 529e3720..d2f390ae 100644 --- a/src/main/java/sorts/merge/AndreySort.java +++ b/src/main/java/sorts/merge/AndreySort.java @@ -156,4 +156,4 @@ private void msort(int[] arr, int a, int len) { public void runSort(int[] array, int sortLength, int bucketCount) throws Exception { this.msort(array, 0, sortLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/BlockSwapMergeSort.java b/src/main/java/sorts/merge/BlockSwapMergeSort.java index 30b22b20..045088ec 100644 --- a/src/main/java/sorts/merge/BlockSwapMergeSort.java +++ b/src/main/java/sorts/merge/BlockSwapMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2019 Piotr Grochowski @@ -30,10 +30,10 @@ this software and associated documentation files (the "Software"), to deal in */ //refactored version of original implementation by @Piotr Grochowski (in place merge 2) -final public class BlockSwapMergeSort extends Sort { +public final class BlockSwapMergeSort extends Sort { public BlockSwapMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Block-Swap Merge"); this.setRunAllSortsName("Block-Swap Merge Sort"); this.setRunSortName("Block-Swap Mergesort"); @@ -45,55 +45,55 @@ public BlockSwapMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private int binarySearchMid(int[] array, int start, int mid, int end) { int a = 0, b = Math.min(mid-start, end-mid), m = a+(b-a)/2; - + while(b > a) { if(Reads.compareValues(array[mid-m-1], array[mid+m]) == 1) a = m+1; else - b = m; - + b = m; + m = a+(b-a)/2; } - + return m; } - + public void multiSwapMerge(int[] array, int start, int mid, int end) { int m = this.binarySearchMid(array, start, mid, end); - + while(m > 0) { this.multiSwap(array, mid-m, mid, m); this.multiSwapMerge(array, mid, mid+m, end); - + end = mid; mid -= m; - + m = this.binarySearchMid(array, start, mid, end); } } - + public void multiSwapMergeSort(int[] array, int a, int b) { int len = b-a, i; - + for(int j = 1; j < len; j *= 2) { for(i = a; i + 2*j <= b; i += 2*j) this.multiSwapMerge(array, i, i+j, i+2*j); - + if(i + j < b) this.multiSwapMerge(array, i, i+j, b); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.multiSwapMergeSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/BottomUpMergeSort.java b/src/main/java/sorts/merge/BottomUpMergeSort.java index 1b9e9f6e..9249386f 100644 --- a/src/main/java/sorts/merge/BottomUpMergeSort.java +++ b/src/main/java/sorts/merge/BottomUpMergeSort.java @@ -97,4 +97,4 @@ public void runSort(int[] array, int currentLength, int bucketCount) { Writes.deleteExternalArray(this.scratchArray); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/ImprovedInPlaceMergeSort.java b/src/main/java/sorts/merge/ImprovedInPlaceMergeSort.java index 9c20bdd6..e6b43fd0 100644 --- a/src/main/java/sorts/merge/ImprovedInPlaceMergeSort.java +++ b/src/main/java/sorts/merge/ImprovedInPlaceMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ImprovedInPlaceMergeSort extends Sort { +public final class ImprovedInPlaceMergeSort extends Sort { public ImprovedInPlaceMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Improved In-Place Merge"); this.setRunAllSortsName("Improved In-Place Merge Sort"); this.setRunSortName("Improved In-Place Mergesort"); @@ -47,42 +47,42 @@ public ImprovedInPlaceMergeSort(ArrayVisualizer arrayVisualizer) { private void push(int[] array, int p, int a, int b, double sleep) { if(a == b) return; - + int temp = array[p]; Writes.write(array, p, array[a], sleep, true, false); - + for(int i = a+1; i < b; i++) Writes.write(array, i-1, array[i], sleep, true, false); - + Writes.write(array, b-1, temp, sleep, true, false); } - + private void merge(int[] array, int a, int m, int b, double sleep) { int i = a, j = m; - + Highlights.clearMark(1); while(i < m && j < b) { Highlights.markArray(2, i); Highlights.markArray(3, j); Delays.sleep(1); - + if(Reads.compareValues(array[i], array[j]) == 1) j++; else this.push(array, i++, m, j, sleep); } - + Highlights.clearAllMarks(); while(i < m) this.push(array, i++, m, b, sleep); } private void mergeSort(int[] array, int a, int b, double sleep) { int m = a+(b-a)/2; - + if(b-a > 2) { if(b-a > 3) this.mergeSort(array, a, m, 2*sleep); this.mergeSort(array, m, b, 2*sleep); } - + this.merge(array, a, m, b, sleep); } @@ -90,4 +90,4 @@ private void mergeSort(int[] array, int a, int b, double sleep) { public void runSort(int[] array, int currentLength, int bucketCount) { this.mergeSort(array, 0, currentLength, Math.max(1.0 / currentLength, 0.001)); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/InPlaceMergeSort.java b/src/main/java/sorts/merge/InPlaceMergeSort.java index 0947f7b5..0f08119a 100644 --- a/src/main/java/sorts/merge/InPlaceMergeSort.java +++ b/src/main/java/sorts/merge/InPlaceMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class InPlaceMergeSort extends Sort { +public final class InPlaceMergeSort extends Sort { public InPlaceMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("In-Place Merge"); this.setRunAllSortsName("In-Place Merge Sort"); this.setRunSortName("In-Place Mergesort"); @@ -52,7 +52,7 @@ private void push(int[] array, int low, int high) { } } } - + private void merge(int[] array, int min, int max, int mid) { int i = min; while(i <= mid) { @@ -87,4 +87,4 @@ else if(max - min == 1) { //only two elements and swaps them public void runSort(int[] array, int currentLength, int bucketCount) { this.mergeSort(array, 0, currentLength - 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/LazyStableSort.java b/src/main/java/sorts/merge/LazyStableSort.java index 465adfcb..ab965ca5 100644 --- a/src/main/java/sorts/merge/LazyStableSort.java +++ b/src/main/java/sorts/merge/LazyStableSort.java @@ -4,7 +4,7 @@ import sorts.templates.GrailSorting; /* - * + * The MIT License (MIT) Copyright (c) 2013 Andrey Astrelin @@ -38,10 +38,10 @@ this software and associated documentation files (the "Software"), to deal in /* */ /*****************************************/ -final public class LazyStableSort extends GrailSorting { +public final class LazyStableSort extends GrailSorting { public LazyStableSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Lazy Stable"); this.setRunAllSortsName("Lazy Stable Sort"); this.setRunSortName("Lazy Stable Sort"); @@ -53,9 +53,9 @@ public LazyStableSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override - public void runSort(int[] array, int length, int bucketCount) { + public void runSort(int[] array, int length, int bucketCount) { this.grailLazyStableSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/MergeSort.java b/src/main/java/sorts/merge/MergeSort.java index 9204ebf3..c7dce071 100644 --- a/src/main/java/sorts/merge/MergeSort.java +++ b/src/main/java/sorts/merge/MergeSort.java @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class MergeSort extends MergeSorting { +public final class MergeSort extends MergeSorting { public MergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Merge"); this.setRunAllSortsName("Merge Sort"); this.setRunSortName("Mergesort"); @@ -44,9 +44,9 @@ public MergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.mergeSort(array, length, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/MergeSortParallel.java b/src/main/java/sorts/merge/MergeSortParallel.java index ff9cf06b..74146c55 100644 --- a/src/main/java/sorts/merge/MergeSortParallel.java +++ b/src/main/java/sorts/merge/MergeSortParallel.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2021 aphitorite @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class MergeSortParallel extends Sort { +public final class MergeSortParallel extends Sort { public MergeSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Merge (Parallel)"); this.setRunAllSortsName("Parallel Merge Sort"); this.setRunSortName("Parallel Mergesort"); @@ -43,10 +43,10 @@ public MergeSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; private int[] tmp; - + private class MergeSort extends Thread { private int a, b; MergeSort(int a, int b) { @@ -57,10 +57,10 @@ public void run() { MergeSortParallel.this.mergeSort(a, b); } } - + private void merge(int a, int m, int b) { int i = a, j = m, k = a; - + while(i < m && j < b) { if(Reads.compareValues(array[i], array[j]) <= 0) { Highlights.markArray(1, i); @@ -79,33 +79,33 @@ private void merge(int a, int m, int b) { Highlights.markArray(2, j); Writes.write(tmp, k++, array[j++], 1, false, true); } - + Highlights.clearMark(2); while(a < b) Writes.write(array, a, tmp[a++], 1, true, false); } - + private void mergeSort(int a, int b) { int len = b-a; - + if(len < 2) return; - + int m = (a+b)/2; - + MergeSort left = new MergeSort(a, m); MergeSort right = new MergeSort(m, b); left.start(); right.start(); - + try { left.join(); right.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - + this.merge(a, m, b); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; @@ -113,4 +113,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.mergeSort(0, length); Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/PDMergeSort.java b/src/main/java/sorts/merge/PDMergeSort.java index 278022f0..19fecbfe 100644 --- a/src/main/java/sorts/merge/PDMergeSort.java +++ b/src/main/java/sorts/merge/PDMergeSort.java @@ -181,4 +181,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.deleteExternalArray(runs); Writes.deleteExternalArray(copied); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/QuadSort.java b/src/main/java/sorts/merge/QuadSort.java index 5d112994..474f5ab2 100644 --- a/src/main/java/sorts/merge/QuadSort.java +++ b/src/main/java/sorts/merge/QuadSort.java @@ -29,10 +29,10 @@ a copy of this software and associated documentation files (the // re-implemented to Java, ArrayV by mg-2018, 2021 // 210704 ~ 210816 -final public class QuadSort extends QuadSorting { +public final class QuadSort extends QuadSorting { public QuadSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Quad"); this.setRunAllSortsName("Quad Sort"); this.setRunSortName("Quadsort"); @@ -44,9 +44,9 @@ public QuadSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.quadSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/RotateMergeSort.java b/src/main/java/sorts/merge/RotateMergeSort.java index da3c8832..15667cd7 100644 --- a/src/main/java/sorts/merge/RotateMergeSort.java +++ b/src/main/java/sorts/merge/RotateMergeSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2020 aphitorite @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class RotateMergeSort extends Sort { +public final class RotateMergeSort extends Sort { public RotateMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Rotate Merge"); this.setRunAllSortsName("Rotate Merge Sort"); //this.setRunAllID("In-Place Merge Sort with Rotations"); @@ -44,15 +44,15 @@ public RotateMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void multiSwap(int[] array, int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(array, a+i, b+i, 1, true, false); } - + private void rotate(int[] array, int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(array, m-r, m, r); @@ -68,24 +68,24 @@ private void rotate(int[] array, int a, int m, int b) { } } } - + private int binarySearch(int[] array, int a, int b, int value, boolean left) { while(a < b) { int m = a+(b-a)/2; - + boolean comp = left ? Reads.compareValues(value, array[m]) <= 0 : Reads.compareValues(value, array[m]) < 0; - + if(comp) b = m; else a = m+1; } - + return a; } - + private void rotateMerge(int[] array, int a, int m, int b) { int m1, m2, m3; - + if(m-a >= b-m) { m1 = a+(m-a)/2; m2 = this.binarySearch(array, m, b, array[m1], true); @@ -97,25 +97,25 @@ private void rotateMerge(int[] array, int a, int m, int b) { m3 = (m2++)-(m-m1); } this.rotate(array, m1, m, m2); - + if(m2-(m3+1) > 0 && b-m2 > 0) this.rotateMerge(array, m3+1, m2, b); if(m1-a > 0 && m3-m1 > 0) this.rotateMerge(array, a, m1, m3); } - + protected void rotateMergeSort(int[] array, int a, int b) { int len = b-a, i; - + for(int j = 1; j < len; j *= 2) { for(i = a; i + 2*j <= b; i += 2*j) this.rotateMerge(array, i, i+j, i+2*j); - + if(i + j < b) this.rotateMerge(array, i, i+j, b); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.rotateMergeSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/RotateMergeSortParallel.java b/src/main/java/sorts/merge/RotateMergeSortParallel.java index 3f45acf7..6f894680 100644 --- a/src/main/java/sorts/merge/RotateMergeSortParallel.java +++ b/src/main/java/sorts/merge/RotateMergeSortParallel.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * The MIT License (MIT) Copyright (c) 2020 aphitorite @@ -28,10 +28,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class RotateMergeSortParallel extends Sort { +public final class RotateMergeSortParallel extends Sort { public RotateMergeSortParallel(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Rotate Merge (Parallel)"); this.setRunAllSortsName("Parallel Rotate Merge Sort"); //this.setRunAllID("In-Place Merge Sort with Rotations"); @@ -44,9 +44,9 @@ public RotateMergeSortParallel(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(4096); this.setBogoSort(false); } - + private int[] array; - + private class RotateMergeSort extends Thread { private int a, b; RotateMergeSort(int a, int b) { @@ -69,15 +69,15 @@ public void run() { RotateMergeSortParallel.this.rotateMerge(a, m, b); } } - + private void multiSwap(int a, int b, int len) { for(int i = 0; i < len; i++) Writes.swap(this.array, a+i, b+i, 1, true, false); } - + private void rotate(int a, int m, int b) { int l = m-a, r = b-m; - + while(l > 0 && r > 0) { if(r < l) { this.multiSwap(m-r, m, r); @@ -93,26 +93,26 @@ private void rotate(int a, int m, int b) { } } } - + private int binarySearch(int a, int b, int value, boolean left) { while(a < b) { int m = a+(b-a)/2; - + boolean comp = left ? Reads.compareValues(value, this.array[m]) <= 0 : Reads.compareValues(value, this.array[m]) < 0; - + if(comp) b = m; else a = m+1; } - + return a; } - + private void rotateMerge(int a, int m, int b) { if(m-a < 1 || b-m < 1) return; - + int m1, m2, m3; - + if(m-a >= b-m) { m1 = a+(m-a)/2; m2 = this.binarySearch(m, b, this.array[m1], true); @@ -124,7 +124,7 @@ private void rotateMerge(int a, int m, int b) { m3 = (m2++)-(m-m1); } this.rotate(m1, m, m2); - + RotateMerge l = new RotateMerge(a, m1, m3); RotateMerge r = new RotateMerge(m3+1, m2, b); l.start(); @@ -136,10 +136,10 @@ private void rotateMerge(int a, int m, int b) { Thread.currentThread().interrupt(); } } - + protected void rotateMergeSort(int a, int b) { if(b-a < 2) return; - + int m = (a+b)/2; RotateMergeSort l = new RotateMergeSort(a, m); RotateMergeSort r = new RotateMergeSort(m, b); @@ -148,16 +148,16 @@ protected void rotateMergeSort(int a, int b) { try { l.join(); r.join(); - } + } catch (InterruptedException e) { Thread.currentThread().interrupt(); } this.rotateMerge(a, m, b); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; this.rotateMergeSort(0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/StacklessRotateMergeSort.java b/src/main/java/sorts/merge/StacklessRotateMergeSort.java index ec767532..74697226 100644 --- a/src/main/java/sorts/merge/StacklessRotateMergeSort.java +++ b/src/main/java/sorts/merge/StacklessRotateMergeSort.java @@ -5,7 +5,7 @@ import utils.IndexedRotations; /* - * + * The MIT License (MIT) Copyright (c) 2021 aphitorite @@ -29,10 +29,10 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class StacklessRotateMergeSort extends Sort { +public final class StacklessRotateMergeSort extends Sort { public StacklessRotateMergeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stackless Rotate Merge"); this.setRunAllSortsName("Stackless Rotate Merge Sort"); this.setRunSortName("Stackless Rotate Mergesort"); @@ -44,24 +44,24 @@ public StacklessRotateMergeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void rotate(int[] array, int a, int m, int b) { IndexedRotations.griesMills(array, a, m, b, 0.5, true, false); } - + //@param c - select c smallest elements private void partitionMerge(int[] array, int a, int m, int b, int c) { int lenA = m-a, lenB = b-m; - + if(lenA < 1 || lenB < 1) return; - + if(lenB < lenA) { c = (lenA+lenB)-c; int r1 = 0, r2 = Math.min(c, lenB); - + while(r1 < r2) { int ml = (r1+r2)/2; - + if(Reads.compareValues(array[m-(c-ml)], array[b-ml-1]) > 0) r2 = ml; else @@ -73,10 +73,10 @@ private void partitionMerge(int[] array, int a, int m, int b, int c) { } else { int r1 = 0, r2 = Math.min(c, lenA); - + while(r1 < r2) { int ml = (r1+r2)/2; - + if(Reads.compareValues(array[a+ml], array[m+(c-ml)-1]) > 0) r2 = ml; else @@ -87,40 +87,40 @@ private void partitionMerge(int[] array, int a, int m, int b, int c) { this.rotate(array, a+r1, m, m+(c-r1)); } } - + private void rotateMerge(int[] array, int a, int b, int c) { int i; for(i = a+1; i < b && Reads.compareIndices(array, i-1, i, 0.25, true) <= 0; i++); if(i < b) this.partitionMerge(array, a, i, b, c); } - + public void rotatePartitionMergeSort(int[] array, int a, int b) { int len = b-a; - + for(int i = a+1; i < b; i += 2) if(Reads.compareIndices(array, i-1, i, 0.5, true) > 0) Writes.swap(array, i-1, i, 0.5, true, false); - + for(int j = 2; j < len; j *= 2) { int b1 = 0; - + for(int i = a; i+j < b; i += 2*j) { b1 = Math.min(i+2*j, b); this.partitionMerge(array, i, i+j, b1, j); } - + for(int k = j/2; k > 1; k /= 2) for(int i = a; i+k < b1; i += 2*k) this.rotateMerge(array, i, Math.min(i+2*k, b), k); - + for(int i = a+1; i < b1; i += 2) if(Reads.compareIndices(array, i-1, i, 0.5, true) > 0) Writes.swap(array, i-1, i, 0.5, true, false); } } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.rotatePartitionMergeSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/StrandSort.java b/src/main/java/sorts/merge/StrandSort.java index 0b7a79c3..1de069a9 100644 --- a/src/main/java/sorts/merge/StrandSort.java +++ b/src/main/java/sorts/merge/StrandSort.java @@ -87,4 +87,4 @@ public void runSort(int[] array, int length, int bucketCount) { } Writes.deleteExternalArray(subList); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/merge/TwinSort.java b/src/main/java/sorts/merge/TwinSort.java index f1ddc74c..351e8cb0 100644 --- a/src/main/java/sorts/merge/TwinSort.java +++ b/src/main/java/sorts/merge/TwinSort.java @@ -35,7 +35,7 @@ a copy of this software and associated documentation files (the twinsort 1.1.3.3 */ -final public class TwinSort extends TwinSorting { +public final class TwinSort extends TwinSorting { public TwinSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -55,4 +55,4 @@ public TwinSort(ArrayVisualizer arrayVisualizer) { public void runSort(int[] array, int currentLength, int bucketCount) { this.twinsort(array, currentLength); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/misc/BurntPancakeSort.java b/src/main/java/sorts/misc/BurntPancakeSort.java index 7544389c..44a6ff09 100644 --- a/src/main/java/sorts/misc/BurntPancakeSort.java +++ b/src/main/java/sorts/misc/BurntPancakeSort.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BurntPancakeSort extends Sort { +public final class BurntPancakeSort extends Sort { public BurntPancakeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -36,4 +36,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/misc/PancakeInsertionSort.java b/src/main/java/sorts/misc/PancakeInsertionSort.java index 4b0af84b..3a85baae 100644 --- a/src/main/java/sorts/misc/PancakeInsertionSort.java +++ b/src/main/java/sorts/misc/PancakeInsertionSort.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class PancakeInsertionSort extends Sort { +public final class PancakeInsertionSort extends Sort { public PancakeInsertionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -26,7 +26,7 @@ private int monoboundFw(int[] array, int start, int end, int value) { while (top > 1) { mid = top / 2; - + if (Reads.compareValueIndex(array, value, end - mid, 0.5, true) <= 0) { end -= mid; } diff --git a/src/main/java/sorts/misc/PancakeSort.java b/src/main/java/sorts/misc/PancakeSort.java index 4b5223cf..dc11eb2c 100644 --- a/src/main/java/sorts/misc/PancakeSort.java +++ b/src/main/java/sorts/misc/PancakeSort.java @@ -7,15 +7,15 @@ * IDeserve
* https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve * Given an array, sort the array using Pancake sort. - * + * * @author Saurabh * https://www.ideserve.co.in/learn/pancake-sorting */ -final public class PancakeSort extends Sort { +public final class PancakeSort extends Sort { public PancakeSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Pancake"); this.setRunAllSortsName("Pancake Sorting"); this.setRunSortName("Pancake Sort"); @@ -27,34 +27,34 @@ public PancakeSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private boolean sorted(int[] array, int length) { for(int i = 0; i < length; i++) { Highlights.markArray(1, i); Delays.sleep(0.025); - + if(Reads.compareValues(array[i], array[i + 1]) > 0) return false; } return true; } - + private int findMax(int[] arr, int end) { int index = 0, max = Integer.MIN_VALUE; for (int i = 0; i <= end; i++) { Highlights.markArray(1, i); - + if (Reads.compareValues(arr[i], max) == 1) { max = arr[i]; index = i; Highlights.markArray(2, i); } - + Delays.sleep(0.025); Highlights.clearMark(1); } return index; } - + @Override public void runSort(int[] array, int length, int bucketCount) { for (int i = length - 1; i >= 0; i--) { @@ -72,4 +72,4 @@ else if(index != i) { else break; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/misc/StalinSort.java b/src/main/java/sorts/misc/StalinSort.java index fec1af19..556ff324 100644 --- a/src/main/java/sorts/misc/StalinSort.java +++ b/src/main/java/sorts/misc/StalinSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class StalinSort extends Sort { +public final class StalinSort extends Sort { public StalinSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stalin"); this.setRunAllSortsName("Stalin Sort"); this.setRunSortName("Stalinsort"); @@ -18,7 +18,7 @@ public StalinSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { for (int i = 1; i < currentLength; i++) { diff --git a/src/main/java/sorts/quick/TernaryLLQuickSort.java b/src/main/java/sorts/quick/TernaryLLQuickSort.java index 941d5209..35d1585a 100644 --- a/src/main/java/sorts/quick/TernaryLLQuickSort.java +++ b/src/main/java/sorts/quick/TernaryLLQuickSort.java @@ -8,7 +8,7 @@ * Implemented in ArrayV by Gaming32 * https://github.com/bingmann/sound-of-sorting/blob/master/src/SortAlgo.cpp#L536-L595 */ -final public class TernaryLLQuickSort extends Sort { +public final class TernaryLLQuickSort extends Sort { class PivotPair { int first, second; @@ -20,7 +20,7 @@ public PivotPair(int first, int second) { public TernaryLLQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Ternary LL Quick"); this.setRunAllSortsName("Quick Sort (ternary, LL ptrs)"); this.setRunSortName("Quicksort (ternary, LL ptrs)"); @@ -91,7 +91,7 @@ private void quickSortTernaryLL(int[] A, int lo, int hi) { quickSortTernaryLL(A, mid.second, hi); } } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { quickSortTernaryLL(array, 0, currentLength); diff --git a/src/main/java/sorts/quick/TernaryLRQuickSort.java b/src/main/java/sorts/quick/TernaryLRQuickSort.java index 010b16c3..bf29e675 100644 --- a/src/main/java/sorts/quick/TernaryLRQuickSort.java +++ b/src/main/java/sorts/quick/TernaryLRQuickSort.java @@ -8,10 +8,10 @@ * Implemented in ArrayV by Gaming32 * https://github.com/bingmann/sound-of-sorting/blob/master/src/SortAlgo.cpp#L449-L534 */ -final public class TernaryLRQuickSort extends Sort { +public final class TernaryLRQuickSort extends Sort { public TernaryLRQuickSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Ternary LR Quick"); this.setRunAllSortsName("Quick Sort (ternary, LR ptrs)"); this.setRunSortName("Quicksort (ternary, LR ptrs)"); @@ -107,7 +107,7 @@ private void quickSortTernaryLR(int[] A, int lo, int hi) { quickSortTernaryLR(A, lo, lo + num_less - 1); quickSortTernaryLR(A, hi - num_greater + 1, hi); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { quickSortTernaryLR(array, 0, currentLength - 1); diff --git a/src/main/java/sorts/select/AsynchronousSort.java b/src/main/java/sorts/select/AsynchronousSort.java index e88ded6c..5a80c9e3 100644 --- a/src/main/java/sorts/select/AsynchronousSort.java +++ b/src/main/java/sorts/select/AsynchronousSort.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class AsynchronousSort extends Sort { +public final class AsynchronousSort extends Sort { public AsynchronousSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -83,4 +83,4 @@ public void runSort(int[] array, int length, int bucketCount) { Highlights.clearMark(2); insertSort.customInsertSort(array, 0, cur, 0.2, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BadSort.java b/src/main/java/sorts/select/BadSort.java index e527fa1a..d710d6ad 100644 --- a/src/main/java/sorts/select/BadSort.java +++ b/src/main/java/sorts/select/BadSort.java @@ -7,11 +7,11 @@ * This example of an O(n^3) sorting algorithm may be found here, written by James Jensen (StriplingWarrayior on StackOverflow): * https://stackoverflow.com/questions/27389344/is-there-a-sorting-algorithm-with-a-worst-case-time-complexity-of-n3 */ - -final public class BadSort extends Sort { + +public final class BadSort extends Sort { public BadSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bad"); this.setRunAllSortsName("Bad Sort"); this.setRunSortName("Badsort"); @@ -29,16 +29,16 @@ public void runSort(int[] array, int currentLen, int bucketCount) { for (int i = 0; i < currentLen; i++) { int shortest = i; Delays.sleep(0.05); - + for (int j = i; j < currentLen; j++) { Highlights.markArray(1, j); Delays.sleep(0.05); - + boolean isShortest = true; for (int k = j + 1; k < currentLen; k++) { Highlights.markArray(2, k); Delays.sleep(0.05); - + if (Reads.compareValues(array[j], array[k]) == 1) { isShortest = false; break; @@ -52,4 +52,4 @@ public void runSort(int[] array, int currentLen, int bucketCount) { Writes.swap(array, i, shortest, 0.05, true, false); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BaseNMaxHeapSort.java b/src/main/java/sorts/select/BaseNMaxHeapSort.java index a99cf657..99c79d1f 100644 --- a/src/main/java/sorts/select/BaseNMaxHeapSort.java +++ b/src/main/java/sorts/select/BaseNMaxHeapSort.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BaseNMaxHeapSort extends Sort { +public final class BaseNMaxHeapSort extends Sort { public BaseNMaxHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); this.setSortListName("Base-N Max Heap"); @@ -50,4 +50,4 @@ public void runSort(int[] arr, int length, int base) { this.siftDown(arr, base, 0, i, 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BingoSort.java b/src/main/java/sorts/select/BingoSort.java index 00c70c81..156683ff 100644 --- a/src/main/java/sorts/select/BingoSort.java +++ b/src/main/java/sorts/select/BingoSort.java @@ -6,23 +6,23 @@ /* * Bingo Sort is a variant of Selection Sort which looks through all elements, using the * element with the maximum VALUE instead of the item to be swapped instead of the item. - * + * * This is best suited to use when there are duplicate values in the array because the * sort will run quicker (similar to Counting Sort) - running on O(n+m^2) best case scenario, * otherwise it will run at O(n*m) time complexity, * where "m" is the amount of unique values in the array. - * + * * >> Original source(s): * >> https://en.wikipedia.org/wiki/Selection_sort#Variants * >> https://xlinux.nist.gov/dads/HTML/bingosort.html - * + * * >> Imported and Translated (from Pascal) by Joel "McDude73" Zaleschuk */ -final public class BingoSort extends Sort { +public final class BingoSort extends Sort { public BingoSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Bingo"); this.setRunAllSortsName("Bingo Sort"); this.setRunSortName("Bingosort"); @@ -34,14 +34,14 @@ public BingoSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { double sleep = Math.min(Math.max(10d/this.arrayVisualizer.getUniqueItems(), 0.001), 1); - + int maximum = length - 1; int next = array[maximum]; - + for(int i=maximum-1;i>=0;i--) { if(array[i] > next) { next = array[i]; @@ -53,16 +53,16 @@ public void runSort(int[] array, int length, int bucketCount) { while(maximum > 0) { int val = next; next = array[maximum]; - + for(int j=maximum-1;j>=0;j--) { - + Highlights.markArray(1, array[j]); Highlights.markArray(2, val); - + if(Reads.compareValues(array[j], val) == 0) { Writes.swap(array, j, maximum, 2*sleep, true, false); maximum--; - + } else { if(array[j] > next) { @@ -76,4 +76,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BinomialHeapSort.java b/src/main/java/sorts/select/BinomialHeapSort.java index 0ff8de14..f6e60582 100644 --- a/src/main/java/sorts/select/BinomialHeapSort.java +++ b/src/main/java/sorts/select/BinomialHeapSort.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BinomialHeapSort extends Sort { +public final class BinomialHeapSort extends Sort { public BinomialHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -58,4 +58,4 @@ public void runSort(int[] array, int length, int bucketCount) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BinomialSmoothSort.java b/src/main/java/sorts/select/BinomialSmoothSort.java index aa576185..3082bb79 100644 --- a/src/main/java/sorts/select/BinomialSmoothSort.java +++ b/src/main/java/sorts/select/BinomialSmoothSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class BinomialSmoothSort extends Sort { +public final class BinomialSmoothSort extends Sort { public BinomialSmoothSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Binomial Smooth"); this.setRunAllSortsName("Binomial Smooth Sort"); this.setRunSortName("Binomial Smoothsort"); @@ -25,7 +25,7 @@ private int height(int node){ count++; return count; } - + private void thrift(int[] array, int node, boolean parent, boolean root){ root = root && (node >= (1 << height(node))); if (!root && !parent) @@ -40,7 +40,7 @@ private void thrift(int[] array, int node, boolean parent, boolean root){ Writes.swap(array, node, node - (1 << choice), .65, true, false); thrift(array, node - (1 << choice), (node - (1 << choice)) % 2 ==1, choice == height(node)); } - + @Override public void runSort(int[] array, int length, int bucketCount){ //heapify from 0 to length - 1 @@ -52,4 +52,4 @@ public void runSort(int[] array, int length, int bucketCount){ for (int Child = height(Node) - 1; Child >= 0; Child --) thrift(array, Node - (1 << Child), false, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/BottomUpHeapSort.java b/src/main/java/sorts/select/BottomUpHeapSort.java index ad9f44fd..682082ad 100644 --- a/src/main/java/sorts/select/BottomUpHeapSort.java +++ b/src/main/java/sorts/select/BottomUpHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class BottomUpHeapSort extends Sort { +public final class BottomUpHeapSort extends Sort { public BottomUpHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,24 +44,24 @@ public BottomUpHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + //source: https://en.wikipedia.org/wiki/Heapsort#Bottom-up_heapsort - + private void siftDown(int[] array, int i, int b) { int j = i; for(; 2*j + 1 < b; j = 2*j + 2 < b ? (Reads.compareValues(array[2*j + 2], array[2*j + 1]) > 0 ? 2*j + 2 : 2*j + 1) : 2*j + 1); for(; Reads.compareValues(array[i], array[j]) > 0; j = (j-1)/2); for(; j > i; j = (j-1)/2) Writes.swap(array, i, j, 1, true, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int i = (length-1)/2; i >= 0; i--) this.siftDown(array, i, length); - + for(int i = length-1; i > 0; i--) { Writes.swap(array, 0, i, 1, true, false); this.siftDown(array, 0, i); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/ClassicTournamentSort.java b/src/main/java/sorts/select/ClassicTournamentSort.java index ec2ebe07..14e73e1a 100644 --- a/src/main/java/sorts/select/ClassicTournamentSort.java +++ b/src/main/java/sorts/select/ClassicTournamentSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class ClassicTournamentSort extends Sort { +public final class ClassicTournamentSort extends Sort { public ClassicTournamentSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,98 +44,98 @@ public ClassicTournamentSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int[] array; private int[] tmp; private int[] tree; - + private int size; - + private int ceilPow2(int n) { int r = 1; while(r < n) r *= 2; return r; } - + private boolean treeCompare(int a, int b) { return Reads.compareIndices(this.array, this.tree[a], this.tree[b], 1, true) <= 0; } - + private void buildTree(int n) { this.size = this.ceilPow2(n)-1; int mod = n&1; int treeSize = n+this.size+mod; - + this.tree = Writes.createExternalArray(treeSize); - + for(int i = 0; i < treeSize; i++) Writes.write(this.tree, i, -1, 0, false, true); - + for(int i = this.size; i < treeSize-mod; i++) { Highlights.markArray(1, i-this.size); Writes.write(this.tree, i, i-this.size, 0.5, false, true); } - + for(int i, j = this.size, k = treeSize-mod; j > 0; j /= 2, k /= 2) { for(i = j; i+1 < k; i += 2) { int val = this.treeCompare(i, i+1) ? this.tree[i] : this.tree[i+1]; - + Writes.write(this.tree, i/2, val, 0, false, true); } if(i < k) Writes.write(this.tree, i/2, this.tree[i], 0, false, true); } } - + private int peek() { return this.array[this.tree[0]]; } - + private int findNext() { int root = this.tree[0]+this.size; - + for(int i = root; i > 0; i = (i-1)/2) Writes.write(this.tree, i, -1, 0, false, true); - + for(int i = root; i > 0; ) { int j = i + ((i&1)<<1)-1; - + int c1 = this.tree[i] >> 31; int c2 = this.tree[j] >> 31; - + int nVal = (c1 & ((c2 & -1) + (~c2 & this.tree[j]))) + (~c1 & ((c2 & this.tree[i]) + (~c2 & -2))); - + if(nVal == -2) { if(i < j) nVal = this.treeCompare(i, j) ? this.tree[i] : this.tree[j]; else nVal = this.treeCompare(j, i) ? this.tree[j] : this.tree[i]; } - + i = (i-1)/2; if(nVal != -1) Writes.write(this.tree, i, nVal, 0, false, true); } - + return this.peek(); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.array = array; - + this.buildTree(length); this.tmp = Writes.createExternalArray(length); - + Highlights.markArray(3, 0); Writes.write(this.tmp, 0, this.peek(), 1, false, true); - + for(int i = 1; i < length; i++) { int val = this.findNext(); - + Highlights.markArray(3, i); Writes.write(this.tmp, i, val, 1, false, true); } Highlights.clearAllMarks(); Writes.arraycopy(this.tmp, 0, array, 0, length, 1, true, false); - + Writes.deleteExternalArray(this.tree); Writes.deleteExternalArray(this.tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/CycleSort.java b/src/main/java/sorts/select/CycleSort.java index 24937ab6..b044be8a 100644 --- a/src/main/java/sorts/select/CycleSort.java +++ b/src/main/java/sorts/select/CycleSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class CycleSort extends Sort { +public final class CycleSort extends Sort { public CycleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Cycle"); this.setRunAllSortsName("Cycle Sort"); this.setRunSortName("Cyclesort"); @@ -44,15 +44,15 @@ public CycleSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private int countLesser(int[] array, int a, int b, int t) { int r = a; - + for(int i = a+1; i < b; i++) { Highlights.markArray(1, r); Highlights.markArray(2, i); Delays.sleep(0.01); - + r += Reads.compareValues(array[i], t) < 0 ? 1 : 0; } Highlights.clearMark(2); @@ -63,24 +63,24 @@ private int countLesser(int[] array, int a, int b, int t) { public void runSort(int[] array, int length, int bucketCount) { for(int i = 0; i < length-1; i++) { Highlights.markArray(3, i); - + int t = array[i]; int r = this.countLesser(array, i, length, t); - + if(r != i) { do { while(Reads.compareIndexValue(array, r, t, 0.01, true) == 0) r++; - + int t1 = array[r]; Writes.write(array, r, t, 0.02, false, false); t = t1; - + r = this.countLesser(array, i, length, t); } while(r != i); - + Writes.write(array, i, t, 0.02, false, false); } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/DoubleSelectionSort.java b/src/main/java/sorts/select/DoubleSelectionSort.java index cfc9b069..f7b0d8fd 100644 --- a/src/main/java/sorts/select/DoubleSelectionSort.java +++ b/src/main/java/sorts/select/DoubleSelectionSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class DoubleSelectionSort extends Sort { +public final class DoubleSelectionSort extends Sort { public DoubleSelectionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Double Selection"); this.setRunAllSortsName("Double Selection Sort"); this.setRunSortName("Double Selection Sort"); @@ -44,18 +44,18 @@ public DoubleSelectionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int left = 0; int right = length - 1; int smallest = 0; int biggest = 0; - + while(left <= right) { for(int i = left; i <= right; i++) { Highlights.markArray(3, i); - + if(Reads.compareValues(array[i], array[biggest]) == 1) { biggest = i; Highlights.markArray(1, biggest); @@ -66,20 +66,20 @@ public void runSort(int[] array, int length, int bucketCount) { Highlights.markArray(2, smallest); Delays.sleep(0.01); } - + Delays.sleep(0.01); } if(biggest == left) biggest = smallest; - + Writes.swap(array, left, smallest, 0.02, true, false); Writes.swap(array, right, biggest, 0.02, true, false); - + left++; right--; - + smallest = left; biggest = right; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/FlippedMinHeapSort.java b/src/main/java/sorts/select/FlippedMinHeapSort.java index 5b317a52..eb138575 100644 --- a/src/main/java/sorts/select/FlippedMinHeapSort.java +++ b/src/main/java/sorts/select/FlippedMinHeapSort.java @@ -22,10 +22,10 @@ the only real changes are subtracting every array access from (length - 1) the rest is just compacting the code a bit */ -final public class FlippedMinHeapSort extends Sort { +public final class FlippedMinHeapSort extends Sort { public FlippedMinHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Flipped Min Heap"); this.setRunAllSortsName("Flipped Min Heap Sort"); this.setRunSortName("Flipped Reverse Heapsort"); @@ -62,4 +62,4 @@ public void runSort(int[] array, int length, int bucketCount) { siftDown(array, length, 1, i - 1); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/LazyHeapSort.java b/src/main/java/sorts/select/LazyHeapSort.java index c10b25eb..62ca69fa 100644 --- a/src/main/java/sorts/select/LazyHeapSort.java +++ b/src/main/java/sorts/select/LazyHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class LazyHeapSort extends Sort { +public final class LazyHeapSort extends Sort { public LazyHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,33 +44,33 @@ public LazyHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private void maxToFront(int[] array, int a, int b) { int max = a; - + for(int i = a+1; i < b; i++) if(Reads.compareIndices(array, i, max, 0.1, true) > 0) max = i; - + Writes.swap(array, max, a, 1, true, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int s = (int)Math.sqrt(length-1)+1; - + for(int i = 0; i < length; i += s) this.maxToFront(array, i, Math.min(i+s, length)); - + for(int j = length; j > 0;) { int max = 0; - + for(int i = max+s; i < j; i += s) if(Reads.compareIndices(array, i, max, 0.1, true) >= 0) max = i; - + Writes.swap(array, max, --j, 1, true, false); this.maxToFront(array, max, Math.min(max+s, j)); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/MaxHeapSort.java b/src/main/java/sorts/select/MaxHeapSort.java index b2624596..13c53eac 100644 --- a/src/main/java/sorts/select/MaxHeapSort.java +++ b/src/main/java/sorts/select/MaxHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.HeapSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -15,10 +15,10 @@ * */ -final public class MaxHeapSort extends HeapSorting { +public final class MaxHeapSort extends HeapSorting { public MaxHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Max Heap"); this.setRunAllSortsName("Max Heap Sort"); this.setRunSortName("Heapsort"); @@ -34,7 +34,7 @@ public MaxHeapSort(ArrayVisualizer arrayVisualizer) { public void makeHeap(int[] array, int start, int length, double sleep) { this.heapify(array, start, length, sleep, true); } - + public void customHeapSort(int[] array, int start, int length, double sleep) { this.heapSort(array, start, length, sleep, true); } @@ -43,4 +43,4 @@ public void customHeapSort(int[] array, int start, int length, double sleep) { public void runSort(int[] array, int length, int bucketCount) { this.heapSort(array, 0, length, 1, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/MinHeapSort.java b/src/main/java/sorts/select/MinHeapSort.java index 6dad04f4..9ecf081c 100644 --- a/src/main/java/sorts/select/MinHeapSort.java +++ b/src/main/java/sorts/select/MinHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.HeapSorting; /* - * + * Copyright (c) rosettacode.org. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 @@ -15,10 +15,10 @@ * */ -final public class MinHeapSort extends HeapSorting { +public final class MinHeapSort extends HeapSorting { public MinHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Min Heap"); this.setRunAllSortsName("Min Heap Sort"); this.setRunSortName("Reverse Heapsort"); @@ -30,9 +30,9 @@ public MinHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.heapSort(array, 0, length, 1, false); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/MinMaxHeapSort.java b/src/main/java/sorts/select/MinMaxHeapSort.java index 80010f6b..e7fb6717 100644 --- a/src/main/java/sorts/select/MinMaxHeapSort.java +++ b/src/main/java/sorts/select/MinMaxHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; // Min-Max Heaps translated from https://github.com/BartMassey/minmaxheap -final public class MinMaxHeapSort extends Sort { +public final class MinMaxHeapSort extends Sort { int[] a; int start, end; diff --git a/src/main/java/sorts/select/OutOfPlaceHeapSort.java b/src/main/java/sorts/select/OutOfPlaceHeapSort.java index 62c538f7..06c644b5 100644 --- a/src/main/java/sorts/select/OutOfPlaceHeapSort.java +++ b/src/main/java/sorts/select/OutOfPlaceHeapSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class OutOfPlaceHeapSort extends Sort { +public final class OutOfPlaceHeapSort extends Sort { public OutOfPlaceHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,21 +44,21 @@ public OutOfPlaceHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + //source: https://en.wikipedia.org/wiki/Heapsort#Bottom-up_heapsort - + private void siftDown(int[] array, int i, int b) { int j = i; for(; 2*j + 1 < b; j = 2*j + 2 < b ? (Reads.compareValues(array[2*j + 2], array[2*j + 1]) > 0 ? 2*j + 2 : 2*j + 1) : 2*j + 1); for(; Reads.compareValues(array[i], array[j]) > 0; j = (j-1)/2); for(; j > i; j = (j-1)/2) Writes.swap(array, i, j, 1, true, false); } - + private void findNext(int[] array, int b) { int i = 0; int l = 1; int r = 2; - + while(r < b && !(array[l] == -1 && array[r] == -1)) { if(array[l] == -1) { Writes.swap(array, i, r, 1, true, false); @@ -82,22 +82,22 @@ else if(Reads.compareValues(array[r], array[l]) > 0) { if(l < b && array[l] != -1) Writes.swap(array, i, l, 1, true, false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for(int i = (length-1)/2; i >= 0; i--) this.siftDown(array, i, length); - + int[] tmp = Writes.createExternalArray(length); - + for(int i = length-1; i >= 0; i--) { Writes.write(tmp, i, array[0], 0, true, true); Writes.write(array, 0, -1, 1, false, false); - + this.findNext(array, length); } Highlights.clearMark(2); Writes.arraycopy(tmp, 0, array, 0, length, 1, true, false); Writes.deleteExternalArray(tmp); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/PoplarHeapSort.java b/src/main/java/sorts/select/PoplarHeapSort.java index c07e4c27..bc6f54ac 100644 --- a/src/main/java/sorts/select/PoplarHeapSort.java +++ b/src/main/java/sorts/select/PoplarHeapSort.java @@ -207,4 +207,4 @@ public void heapSort(int[] array, int start, int end) { public void runSort(int[] array, int length, int bucketCount) { this.heapSort(array, 0, length); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/SelectionSort.java b/src/main/java/sorts/select/SelectionSort.java index b50438dd..f2201da4 100644 --- a/src/main/java/sorts/select/SelectionSort.java +++ b/src/main/java/sorts/select/SelectionSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2019 w0rthy @@ -29,10 +29,10 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class SelectionSort extends Sort { +public final class SelectionSort extends Sort { public SelectionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Selection"); this.setRunAllSortsName("Selection Sort"); this.setRunSortName("Selection Sort"); @@ -44,16 +44,16 @@ public SelectionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for (int i = 0; i < length - 1; i++) { int lowestindex = i; - + for (int j = i + 1; j < length; j++) { Highlights.markArray(2, j); Delays.sleep(0.01); - + if (Reads.compareValues(array[j], array[lowestindex]) == -1){ lowestindex = j; Highlights.markArray(1, lowestindex); @@ -63,4 +63,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.swap(array, i, lowestindex, 0.02, true, false); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/SmoothSort.java b/src/main/java/sorts/select/SmoothSort.java index 51ec2fc5..d8937796 100644 --- a/src/main/java/sorts/select/SmoothSort.java +++ b/src/main/java/sorts/select/SmoothSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class SmoothSort extends Sort { +public final class SmoothSort extends Sort { public SmoothSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Smooth"); this.setRunAllSortsName("Smooth Sort"); this.setRunSortName("Smoothsort"); @@ -43,12 +43,12 @@ private void sift(int[] A, int pshift, int head) { int rt = head - 1; int lf = head - 1 - LP[pshift - 2]; - + Highlights.markArray(2, rt); Highlights.markArray(3, lf); - + Delays.sleep(0.325); - + if (Reads.compareValues(val, A[lf]) >= 0 && Reads.compareValues(val, A[rt]) >= 0) break; @@ -64,7 +64,7 @@ private void sift(int[] A, int pshift, int head) } } Writes.write(A, head, val, 0.65, true, false); - + Highlights.clearMark(2); Highlights.clearMark(3); } @@ -76,7 +76,7 @@ private void trinkle(int[] A, int p, int pshift, int head, boolean isTrusty) while (p != 1) { int stepson = head - LP[pshift]; - + if (Reads.compareValues(A[stepson], val) <= 0) break; // current node is greater than head. sift. @@ -88,18 +88,18 @@ private void trinkle(int[] A, int p, int pshift, int head, boolean isTrusty) int lf = head - 1 - LP[pshift - 2]; Highlights.markArray(2, rt); Highlights.markArray(3, lf); - + Delays.sleep(0.325); - + if (Reads.compareValues(A[rt], A[stepson]) >= 0 || Reads.compareValues(A[lf], A[stepson]) >= 0) break; } Writes.write(A, head, A[stepson], 0.65, true, false); - + Highlights.clearMark(2); Highlights.clearMark(3); - + head = stepson; //int trail = Integer.numberOfTrailingZeros(p & ~1); int trail = Integer.numberOfTrailingZeros(p & ~1); @@ -107,7 +107,7 @@ private void trinkle(int[] A, int p, int pshift, int head, boolean isTrusty) pshift += trail; isTrusty = false; } - + if (!isTrusty) { Writes.write(A, head, val, 0.65, true, false); this.sift(A, pshift, head); @@ -198,9 +198,9 @@ private void smoothSort(int[] A, int lo, int hi, boolean fullSort) public void smoothHeapify(int[] array, int length) { this.smoothSort(array, 0, length - 1, false); } - + @Override public void runSort(int[] array, int currentLength, int bucketCount) { this.smoothSort(array, 0, currentLength - 1, true); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/StableCycleSort.java b/src/main/java/sorts/select/StableCycleSort.java index 78ff3759..687c51a1 100644 --- a/src/main/java/sorts/select/StableCycleSort.java +++ b/src/main/java/sorts/select/StableCycleSort.java @@ -4,7 +4,7 @@ import sorts.templates.Sort; /* - * + * MIT License Copyright (c) 2021 aphitorite @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StableCycleSort extends Sort { +public final class StableCycleSort extends Sort { public StableCycleSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); @@ -44,51 +44,51 @@ public StableCycleSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private final int WLEN = 3; - + private boolean getBit(int[] bits, int idx) { int b = (bits[idx >> WLEN]) >> (idx & ((1 << WLEN) - 1)) & 1; return b == 1; } - + private void flag(int[] bits, int idx) { Highlights.markArray(4, idx >> WLEN); Writes.write(bits, idx >> WLEN, bits[idx >> WLEN] | (1 << (idx & ((1 << WLEN) - 1))), 0.02, false, true); } - + private int destination1(int[] array, int[] bits, int a, int b1, int b) { int d = a, e = 0; - + for(int i = a+1; i < b; i++) { Highlights.markArray(2, i); int cmp = Reads.compareValues(array[i], array[a]); - + if(cmp < 0) d++; else if(i < b1 && !this.getBit(bits, i) && cmp == 0) e++; - + Highlights.markArray(3, d); Delays.sleep(0.01); } while(this.getBit(bits, d) || e-- > 0) { d++; - + Highlights.markArray(3, d); Delays.sleep(0.01); } - + return d; } - + @Override public void runSort(int[] array, int length, int bucketCount) { int[] bits = Writes.createExternalArray(((length-1) >> WLEN) + 1); - + for(int i = 0; i < length-1; i++) { if(!this.getBit(bits, i)) { Highlights.markArray(1, i); int j = i; - + do { int k = this.destination1(array, bits, i, j, length); Writes.swap(array, i, k, 0.02, true, false); @@ -100,4 +100,4 @@ public void runSort(int[] array, int length, int bucketCount) { } Writes.deleteExternalArray(bits); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/StableSelectionSort.java b/src/main/java/sorts/select/StableSelectionSort.java index 227563a5..6a5c48ab 100644 --- a/src/main/java/sorts/select/StableSelectionSort.java +++ b/src/main/java/sorts/select/StableSelectionSort.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; /* - * + * MIT License Copyright (c) 2020 fungamer2 Permission is hereby granted, free of charge, to any person obtaining a copy @@ -25,11 +25,11 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class StableSelectionSort extends Sort { - +public final class StableSelectionSort extends Sort { + public StableSelectionSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Stable Selection"); this.setRunAllSortsName("Stable Selection Sort"); this.setRunSortName("Stable Selection Sort"); @@ -41,7 +41,7 @@ public StableSelectionSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { for (int i = 0; i < length - 1; i++) { @@ -64,4 +64,4 @@ public void runSort(int[] array, int length, int bucketCount) { Writes.write(array, pos, tmp, 0.5, true, false); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/TernaryHeapSort.java b/src/main/java/sorts/select/TernaryHeapSort.java index 8314f728..b857f39d 100644 --- a/src/main/java/sorts/select/TernaryHeapSort.java +++ b/src/main/java/sorts/select/TernaryHeapSort.java @@ -3,10 +3,10 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class TernaryHeapSort extends Sort { +public final class TernaryHeapSort extends Sort { public TernaryHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Ternary Heap"); this.setRunAllSortsName("Ternary Heap Sort"); this.setRunSortName("Ternary Heapsort"); @@ -18,12 +18,12 @@ public TernaryHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + // TERNARY HEAP SORT - written by qbit // https://codereview.stackexchange.com/questions/63384/binary-heapsort-and-ternary-heapsort-implementation - + private int heapSize; - + private static int leftBranch(int i) { return 3 * i + 1; } @@ -59,13 +59,13 @@ private void maxHeapify(int[] array, int i) { this.maxHeapify(array, largest); } } - + private void buildMaxTernaryHeap(int[] array, int length) { heapSize = length - 1; for(int i = length - 1 / 3; i >= 0; i--) this.maxHeapify(array, i); } - + @Override public void runSort(int[] array, int length, int bucketCount) { this.buildMaxTernaryHeap(array, length); @@ -77,4 +77,4 @@ public void runSort(int[] array, int length, int bucketCount) { this.maxHeapify(array, 0); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/TournamentSort.java b/src/main/java/sorts/select/TournamentSort.java index b1417422..6e05d458 100644 --- a/src/main/java/sorts/select/TournamentSort.java +++ b/src/main/java/sorts/select/TournamentSort.java @@ -21,13 +21,13 @@ along with this program. If not, see . */ -final public class TournamentSort extends Sort { +public final class TournamentSort extends Sort { private int[] matches; private int tourney; - + public TournamentSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Tournament"); this.setRunAllSortsName("Tournament Sort"); this.setRunSortName("Tournament Sort"); @@ -43,16 +43,16 @@ public TournamentSort(ArrayVisualizer arrayVisualizer) { private int tourneyCompare(int a, int b) { Highlights.markArray(2, a); Highlights.markArray(3, b); - + Delays.sleep(1); - + return Reads.compareValues(a, b); } - + private static boolean isPlayer(int i) { return i <= 0; } - + private void setWinner(int root, int winner) { Writes.write(matches, root, winner, 0, false, true); } @@ -76,11 +76,11 @@ private void setMatch(int root, int winner, int winners, int losers) { this.setWinners(root, winners); this.setLosers(root, losers); } - + private int getPlayer(int i) { return i <= 0 ? Math.abs(i) : this.getWinner(i); } - + private int pop(int[] arr) { int result = arr[this.getPlayer(tourney)]; tourney = TournamentSort.isPlayer(tourney) ? 0 : this.rebuild(arr, tourney); @@ -94,18 +94,18 @@ private static int makePlayer(int i) { private int makeMatch(int[] arr, int top, int bot, int root) { int top_w = this.getPlayer(top); int bot_w = this.getPlayer(bot); - + if (tourneyCompare(arr[top_w], arr[bot_w]) <= 0) this.setMatch(root, top_w, top, bot); else this.setMatch(root, bot_w, bot, top); - + return root; } - + private int knockout(int[] arr, int i, int k, int root) { if (i == k) return TournamentSort.makePlayer(i); - + int j = (i + k) / 2; return this.makeMatch(arr, this.knockout(arr, i, j, 2 * root), this.knockout(arr,j + 1, k, (2 * root) + 3), root); } @@ -113,27 +113,27 @@ private int knockout(int[] arr, int i, int k, int root) { private int rebuild(int[] arr, int root) { if (TournamentSort.isPlayer(this.getWinners(root))) return this.getLosers(root); - + this.setWinners(root, this.rebuild(arr, this.getWinners(root))); - + if (tourneyCompare(arr[this.getPlayer(this.getLosers(root))], arr[this.getPlayer(this.getWinners(root))]) < 0) { this.setWinner(root, this.getPlayer(this.getLosers(root))); - + int temp = this.getLosers(root); - + this.setLosers(root, this.getWinners(root)); this.setWinners(root, temp); } else { this.setWinner(root, this.getPlayer(this.getWinners(root))); } - + return root; } - + private void sort(int[] arr, int currentLen) { int[] copy = Writes.createExternalArray(currentLen); - + for (int i = 0; i < currentLen; i++) { int result = this.pop(arr); Writes.write(copy, i, result, 1, true, true); @@ -141,7 +141,7 @@ private void sort(int[] arr, int currentLen) { Highlights.clearAllMarks(); Writes.arraycopy(copy, 0, arr, 0, currentLen, 1, true, false); - + Writes.deleteExternalArray(copy); } @@ -149,9 +149,9 @@ private void sort(int[] arr, int currentLen) { public void runSort(int[] array, int currentLength, int bucketCount) { this.matches = Writes.createExternalArray(6 * currentLength); this.tourney = this.knockout(array, 0, currentLength - 1, 3); - + this.sort(array, currentLength); Writes.deleteExternalArray(this.matches); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/select/WeakHeapSort.java b/src/main/java/sorts/select/WeakHeapSort.java index b60b82e0..ac8560e6 100644 --- a/src/main/java/sorts/select/WeakHeapSort.java +++ b/src/main/java/sorts/select/WeakHeapSort.java @@ -5,10 +5,10 @@ // Refactored from C++ code written by Manish Bhojasia, found here: // https://www.sanfoundry.com/cpp-program-implement-weak-heap/ -final public class WeakHeapSort extends Sort { +public final class WeakHeapSort extends Sort { public WeakHeapSort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Weak Heap"); this.setRunAllSortsName("Weak Heap Sort"); this.setRunSortName("Weak Heapsort"); @@ -20,15 +20,15 @@ public WeakHeapSort(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + private static int getBitwiseFlag(int[] bits, int x) { return ((bits[(x) >> 3] >> ((x) & 7)) & 1); } - - private void toggleBitwiseFlag(int[] bits, int x) { - int flag = bits[(x) >> 3]; + + private void toggleBitwiseFlag(int[] bits, int x) { + int flag = bits[(x) >> 3]; flag ^= 1 << ((x) & 7); - + Writes.write(bits, (x) >> 3, flag, 0, true, true); } @@ -47,39 +47,39 @@ private void weakHeapMerge(int[] array, int[] bits, int i, int j) { public void runSort(int[] array, int length, int bucketCount) { int n = length; int i, j, x, y, Gparent; - + int bitsLength = (n + 7) / 8; int[] bits = Writes.createExternalArray(bitsLength); - + for (i = 0; i < n / 8; ++i) { Writes.write(bits, i, 0, 0, false, true); } - + for (i = n - 1; i > 0; --i) { j = i; - + while ((j & 1) == WeakHeapSort.getBitwiseFlag(bits, j >> 1)) j >>= 1; Gparent = j >> 1; - + this.weakHeapMerge(array, bits, Gparent, i); } - + for (i = n - 1; i >= 2; --i) { Writes.swap(array, 0, i, 1, true, false); - + x = 1; - + while ((y = 2 * x + WeakHeapSort.getBitwiseFlag(bits, x)) < i) x = y; - + while (x > 0) { this.weakHeapMerge(array, bits, 0, x); x >>= 1; } } - Writes.swap(array, 0, 1, 1, true, false); + Writes.swap(array, 0, 1, 1, true, false); Writes.deleteExternalArray(bits); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/BinaryInsertionSorting.java b/src/main/java/sorts/templates/BinaryInsertionSorting.java index 0e14d6ac..97a704f8 100644 --- a/src/main/java/sorts/templates/BinaryInsertionSorting.java +++ b/src/main/java/sorts/templates/BinaryInsertionSorting.java @@ -70,4 +70,4 @@ protected void binaryInsertSort(int[] array, int start, int end, double compSlee Highlights.clearAllMarks(); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/BinaryQuickSorting.java b/src/main/java/sorts/templates/BinaryQuickSorting.java index be3da144..7fb4f45f 100644 --- a/src/main/java/sorts/templates/BinaryQuickSorting.java +++ b/src/main/java/sorts/templates/BinaryQuickSorting.java @@ -98,4 +98,4 @@ public int partition(int[] array, int p, int r, int bit) return j; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/CircleSorting.java b/src/main/java/sorts/templates/CircleSorting.java index 1209117a..406cd088 100644 --- a/src/main/java/sorts/templates/CircleSorting.java +++ b/src/main/java/sorts/templates/CircleSorting.java @@ -45,4 +45,4 @@ protected int circleSortRoutine(int[] array, int lo, int hi, int swapCount, doub return swapCount; } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/CombSorting.java b/src/main/java/sorts/templates/CombSorting.java index 4df6011d..61df2b5c 100644 --- a/src/main/java/sorts/templates/CombSorting.java +++ b/src/main/java/sorts/templates/CombSorting.java @@ -73,4 +73,4 @@ protected void combSort(int[] array, int length, double shrink, boolean hybrid) } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/GrailSorting.java b/src/main/java/sorts/templates/GrailSorting.java index df6d5006..fb957e92 100644 --- a/src/main/java/sorts/templates/GrailSorting.java +++ b/src/main/java/sorts/templates/GrailSorting.java @@ -777,4 +777,4 @@ protected void grailInPlaceMergeSort(int[] arr, int start, int len) { if(rest > part) this.grailInPlaceMerge(arr, left, part, rest - part); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/HeapSorting.java b/src/main/java/sorts/templates/HeapSorting.java index 594faf82..a60fd3dd 100644 --- a/src/main/java/sorts/templates/HeapSorting.java +++ b/src/main/java/sorts/templates/HeapSorting.java @@ -62,4 +62,4 @@ protected void heapSort(int[] arr, int start, int length, double sleep, boolean Writes.reversal(arr, start, start + length - 1, 1, true, false); } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/IterativeCircleSorting.java b/src/main/java/sorts/templates/IterativeCircleSorting.java index 6dff09c6..c48c8200 100644 --- a/src/main/java/sorts/templates/IterativeCircleSorting.java +++ b/src/main/java/sorts/templates/IterativeCircleSorting.java @@ -41,4 +41,4 @@ protected int circleSortRoutine(int[] array, int length, double sleep) { } return swapCount; } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/KotaSorting.java b/src/main/java/sorts/templates/KotaSorting.java index 2653b8ec..37d9c794 100644 --- a/src/main/java/sorts/templates/KotaSorting.java +++ b/src/main/java/sorts/templates/KotaSorting.java @@ -1139,4 +1139,4 @@ else if(this.bufLen <= 16) { Writes.deleteExternalArray(this.cache); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/PDQSorting.java b/src/main/java/sorts/templates/PDQSorting.java index bc5d7701..5ae7877f 100644 --- a/src/main/java/sorts/templates/PDQSorting.java +++ b/src/main/java/sorts/templates/PDQSorting.java @@ -567,4 +567,4 @@ && pdqPartialInsertSort(array, pivotPos + 1, end)) leftmost = false; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/ShatterSorting.java b/src/main/java/sorts/templates/ShatterSorting.java index 3d35f857..da3595de 100644 --- a/src/main/java/sorts/templates/ShatterSorting.java +++ b/src/main/java/sorts/templates/ShatterSorting.java @@ -99,4 +99,4 @@ protected void simpleShatterSort(int[] array, int length, int num, int rate) { } shatterPartition(array, length, 1); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/ShellSorting.java b/src/main/java/sorts/templates/ShellSorting.java index 74529835..23744f1b 100644 --- a/src/main/java/sorts/templates/ShellSorting.java +++ b/src/main/java/sorts/templates/ShellSorting.java @@ -112,4 +112,4 @@ protected void quickShellSort(int[] array, int lo, int hi) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/Sort.java b/src/main/java/sorts/templates/Sort.java index 9444f601..a1aedeb7 100644 --- a/src/main/java/sorts/templates/Sort.java +++ b/src/main/java/sorts/templates/Sort.java @@ -181,4 +181,4 @@ public int validateAnswer(int answer) { } public abstract void runSort(int[] array, int sortLength, int bucketCount) throws Exception; //bucketCount will be zero for comparison-based sorts -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/TimSorting.java b/src/main/java/sorts/templates/TimSorting.java index 39a64e56..fd6108cd 100644 --- a/src/main/java/sorts/templates/TimSorting.java +++ b/src/main/java/sorts/templates/TimSorting.java @@ -52,18 +52,18 @@ * (privately) instantiable; a TimSort instance holds the state of an ongoing * sort, assuming the input array is large enough to warrant the full-blown * TimSort. Small arrays are sorted in place, using a binary insertion sort. - * + * * @author Josh Bloch - * + * * Tailored to ArrayVisualizer by MusicTheorist */ -final public class TimSorting { +public final class TimSorting { private Delays Delays; private Highlights Highlights; private Reads Reads; private Writes Writes; - + /** * This is the minimum sized sequence that will be merged. Shorter * sequences will be lengthened by calling binarySort. If the entire @@ -126,18 +126,18 @@ final public class TimSorting { private int stackSize = 0; // Number of pending runs on stack private final int[] runBase; private final int[] runLen; - + public static int getMinRun() { return TimSorting.MIN_MERGE / 2; } - - // Easy patch for preventing gallop and merge methods from going out-of-bounds + + // Easy patch for preventing gallop and merge methods from going out-of-bounds private void markArray(int marker, int markPosition) { if(markPosition >= 0 && markPosition < this.len) { this.Highlights.markArray(marker, markPosition); } } - + /** * Creates a TimSort instance to maintain the state of an ongoing sort. * @@ -146,12 +146,12 @@ private void markArray(int marker, int markPosition) { public TimSorting(int[] a, int currentLen, ArrayVisualizer arrayVisualizer) { this.a = a; this.len = currentLen; - + this.Delays = arrayVisualizer.getDelays(); this.Highlights = arrayVisualizer.getHighlights(); this.Reads = arrayVisualizer.getReads(); this.Writes = arrayVisualizer.getWrites(); - + // Allocate temp storage (which may be increased later if necessary) int[] newArray = Writes.createExternalArray(this.len < 2 * INITIAL_TMP_STORAGE_LENGTH ? this.len >>> 1 : INITIAL_TMP_STORAGE_LENGTH); @@ -172,7 +172,7 @@ public TimSorting(int[] a, int currentLen, ArrayVisualizer arrayVisualizer) { this.runBase = Writes.createExternalArray(stackLen); this.runLen = Writes.createExternalArray(stackLen); } - + public void customSort(int[] a, int start, int length) { TimSorting.sort(this, a, start, length); this.Writes.deleteExternalArray(this.tmp); @@ -193,7 +193,7 @@ public static void sort(TimSorting timSort, int[] a, int length) { } static void sort(TimSorting timSort, int[] a, int lo, int hi) { TimSorting ts = timSort; - + int nRemaining = hi - lo; // If array is small, do a "mini-TimSort" with no merges if (nRemaining < MIN_MERGE) { @@ -210,26 +210,26 @@ static void sort(TimSorting timSort, int[] a, int lo, int hi) { do { // Identify next run int runLen = countRunAndMakeAscending(ts, a, lo, hi); - + // If run is short, extend to min(minRun, nRemaining) if (runLen < minRun) { int force = nRemaining <= minRun ? nRemaining : minRun; - binarySort(ts, a, lo, lo + force, lo + runLen); + binarySort(ts, a, lo, lo + force, lo + runLen); runLen = force; } // Push run onto pending-run stack, and maybe merge ts.pushRun(lo, runLen); ts.mergeCollapse(); - + // Advance to find next run lo += runLen; nRemaining -= runLen; } while (nRemaining != 0); - + // Merge all remaining runs to complete sort ts.mergeForceCollapse(); } - + /** * Sorts the specified portion of the specified array using a binary * insertion sort. This is the best method for sorting small numbers @@ -248,22 +248,22 @@ static void sort(TimSorting timSort, int[] a, int lo, int hi) { * not already known to be sorted (@code lo <= start <= hi} * @param c comparator to used for the sort */ - + // Here, we do not use the Binary Insertion Sort included in ArrayVisualizer, as TimSort // outfits it with a start index and uses the arraycopy method - + @SuppressWarnings("fallthrough") private static void binarySort(TimSorting ts, int[] a, int lo, int hi, int start) { if (start == lo) start++; - + for ( ; start < hi; start++) { int pivot = a[start]; - + // Set left (and right) to the index where a[start] (pivot) belongs int left = lo; int right = start; - + /* * Invariants: * pivot >= all in [lo, left). @@ -272,13 +272,13 @@ private static void binarySort(TimSorting ts, int[] a, int lo, int hi, int start while (left < right) { // Another good way to prevent integer overflow with left + right! int mid = (left + right) >>> 1; - + if (ts.Reads.compareValues(pivot, a[mid]) < 0) right = mid; else left = mid + 1; } - + /* * The invariants still hold: pivot >= all in [lo, left) and * pivot < all in [left, start), so pivot belongs at left. Note @@ -289,7 +289,7 @@ private static void binarySort(TimSorting ts, int[] a, int lo, int hi, int start int n = start - left; // The number of elements to move // Switch is just an optimization for arraycopy in default case switch(n) { - case 2: ts.Writes.write(a, left + 2, a[left + 1], 1, true, false); + case 2: ts.Writes.write(a, left + 2, a[left + 1], 1, true, false); case 1: ts.Writes.write(a, left + 1, a[left], 1, true, false); break; default: ts.Writes.arraycopy(a, left, a, left + 1, n, 1, true, false); @@ -297,7 +297,7 @@ private static void binarySort(TimSorting ts, int[] a, int lo, int hi, int start ts.Writes.write(a, left, pivot, 1, true, false); } } - + /** * Returns the length of the run beginning at the specified position in * the specified array and reverses the run if it is descending (ensuring @@ -327,7 +327,7 @@ private static int countRunAndMakeAscending(TimSorting ts, int[] a, int lo, int int runHi = lo + 1; if (runHi == hi) return 1; - + // Find end of run, and reverse range if descending if (ts.Reads.compareValues(a[runHi++], a[lo]) < 0) { // Descending while(runHi < hi && ts.Reads.compareValues(a[runHi], a[runHi - 1]) < 0) { @@ -345,7 +345,7 @@ private static int countRunAndMakeAscending(TimSorting ts, int[] a, int lo, int } return runHi - lo; } - + /** * Reverse the specified range of the specified array. * @@ -357,7 +357,7 @@ private static void reverseRange(TimSorting ts, int[] a, int lo, int hi) { ts.Writes.reversal(a, lo, hi - 1, 1, true, false); ts.Highlights.clearMark(2); } - + /** * Returns the minimum acceptable run length for an array of the specified * length. Natural runs shorter than this will be extended with @@ -383,7 +383,7 @@ public static int minRunLength(int n) { } return n + r; } - + /** * Pushes the specified run onto the pending-run stack. * @@ -395,7 +395,7 @@ private void pushRun(int runBase, int runLen) { this.runLen[this.stackSize] = runLen; this.stackSize++; } - + /** * Examines the stack of runs waiting to be merged and merges adjacent runs * until the stack invariants are reestablished: @@ -406,7 +406,7 @@ private void pushRun(int runBase, int runLen) { * This method is called each time a new run is pushed onto the stack, * so the invariants are guaranteed to hold for i < stackSize upon * entry to the method. - * + * * Thanks to Stijn de Gouw, Jurriaan Rot, Frank S. de Boer, * Richard Bubel and Reiner Hahnle, this is fixed with respect to * the analysis in "On the Worst-Case Complexity of TimSort" by @@ -425,7 +425,7 @@ private void mergeCollapse() { mergeAt(n); } } - + /** * Merges all runs on the stack until only one remains. This method is * called once, to complete the sort. @@ -438,7 +438,7 @@ private void mergeForceCollapse() { mergeAt(n); } } - + /** * Merges the two runs at stack indices i and i+1. Run i must be * the penultimate or antepenultimate run on the stack. In other words, @@ -449,12 +449,12 @@ private void mergeForceCollapse() { private void mergeAt(int i) { this.Highlights.clearMark(1); this.Highlights.clearMark(2); - + int base1 = this.runBase[i]; int len1 = this.runLen[i]; int base2 = this.runBase[i + 1]; int len2 = this.runLen[i + 1]; - + /* * Record the length of the combined runs; if i is the 3rd-last * run now, also slide over the last run (which isn't involved @@ -466,7 +466,7 @@ private void mergeAt(int i) { this.runLen[i + 1] = this.runLen[i + 2]; } this.stackSize--; - + /* * Find where the first element of run2 goes in run1. Prior elements * in run1 can be ignored (because they're already in place). @@ -476,7 +476,7 @@ private void mergeAt(int i) { len1 -= k; if (len1 == 0) return; - + /* * Find where the last element of run1 goes in run2. Subsequent elements * in run2 can be ignored (because they're already in place). @@ -484,17 +484,17 @@ private void mergeAt(int i) { len2 = gallopLeft(this, this.a[base1 + len1 - 1], this.a, base2, len2, len2 - 1); if (len2 == 0) return; - + // Merge remaining runs, using tmp array with min(len1, len2) elements if (len1 <= len2) mergeLo(this, base1, len1, base2, len2); else mergeHi(this, base1, len1, base2, len2); - + this.Highlights.clearMark(1); this.Highlights.clearMark(2); } - + /** * Locates the position at which to insert the specified key into the * specified sorted range; if the range contains an element equal to key, @@ -516,57 +516,57 @@ private void mergeAt(int i) { private static int gallopLeft(TimSorting ts, int key, int[] a, int base, int len, int hint) { int lastOfs = 0; int ofs = 1; - + ts.markArray(3, base + hint); ts.Delays.sleep(1); - + if (ts.Reads.compareValues(key, a[base + hint]) > 0) { // Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs] int maxOfs = len - hint; - + ts.markArray(3, base + hint + ofs); ts.Delays.sleep(1); - - while (ofs < maxOfs && ts.Reads.compareValues(key, a[base + hint + ofs]) > 0) { + + while (ofs < maxOfs && ts.Reads.compareValues(key, a[base + hint + ofs]) > 0) { lastOfs = ofs; ofs = (ofs * 2) + 1; if (ofs <= 0) // int overflow ofs = maxOfs; - + ts.markArray(3, base + hint + ofs); ts.Delays.sleep(1); } if (ofs > maxOfs) ofs = maxOfs; - + // Make offsets relative to base lastOfs += hint; ofs += hint; } else { // key <= a[base + hint] // Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs] final int maxOfs = hint + 1; - + ts.markArray(3, base + hint - ofs); ts.Delays.sleep(1); - + while (ofs < maxOfs && ts.Reads.compareValues(key, a[base + hint - ofs]) <= 0) { lastOfs = ofs; ofs = (ofs * 2) + 1; if (ofs <= 0) // int overflow ofs = maxOfs; - + ts.markArray(3, base + hint - ofs); ts.Delays.sleep(1); } if (ofs > maxOfs) ofs = maxOfs; - + // Make offsets relative to base int tmp = lastOfs; lastOfs = hint - ofs; ofs = hint - tmp; } - + /* * Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere * to the right of lastOfs but no farther right than ofs. Do a binary @@ -575,10 +575,10 @@ private static int gallopLeft(TimSorting ts, int key, int[] a, int base, int len lastOfs++; while (lastOfs < ofs) { int m = lastOfs + ((ofs - lastOfs) >>> 1); - + ts.markArray(3, base + m); ts.Delays.sleep(1); - + if (ts.Reads.compareValues(key, a[base + m]) > 0) lastOfs = m + 1; // a[base + m] < key else @@ -603,29 +603,29 @@ private static int gallopLeft(TimSorting ts, int key, int[] a, int base, int len private static int gallopRight(TimSorting ts, int key, int[] a, int base, int len, int hint) { int ofs = 1; int lastOfs = 0; - + ts.markArray(3, base + hint); ts.Delays.sleep(1); - + if (ts.Reads.compareValues(key, a[base + hint]) < 0) { // Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs] int maxOfs = hint + 1; - + ts.markArray(3, base + hint - ofs); ts.Delays.sleep(1); - + while (ofs < maxOfs && ts.Reads.compareValues(key, a[base + hint - ofs]) < 0) { lastOfs = ofs; ofs = (ofs * 2) + 1; if (ofs <= 0) // int overflow ofs = maxOfs; - + ts.markArray(3, base + hint - ofs); ts.Delays.sleep(1); } if (ofs > maxOfs) ofs = maxOfs; - + // Make offsets relative to b int tmp = lastOfs; lastOfs = hint - ofs; @@ -633,27 +633,27 @@ private static int gallopRight(TimSorting ts, int key, int[] a, int base, int le } else { // a[b + hint] <= key // Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs] int maxOfs = len - hint; - + ts.markArray(3, base + hint + ofs); ts.Delays.sleep(1); - + while (ofs < maxOfs && ts.Reads.compareValues(key, a[base + hint + ofs]) >= 0) { lastOfs = ofs; ofs = (ofs * 2) + 1; if (ofs <= 0) // int overflow ofs = maxOfs; - + ts.markArray(3, base + hint + ofs); ts.Delays.sleep(1); } if (ofs > maxOfs) ofs = maxOfs; - + // Make offsets relative to b lastOfs += hint; ofs += hint; } - + /* * Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to * the right of lastOfs but no farther right than ofs. Do a binary @@ -662,10 +662,10 @@ private static int gallopRight(TimSorting ts, int key, int[] a, int base, int le lastOfs++; while (lastOfs < ofs) { int m = lastOfs + ((ofs - lastOfs) >>> 1); - + ts.markArray(3, base + m); ts.Delays.sleep(1); - + if (ts.Reads.compareValues(key, a[base + m]) < 0) ofs = m; // key < a[b + m] else @@ -695,11 +695,11 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { int[] a = ts.a; // For performance int[] tmp = ensureCapacity(len1); ts.Writes.arraycopy(a, base1, tmp, 0, len1, 1, true, true); - + int cursor1 = 0; // Indexes into tmp array int cursor2 = base2; // Indexes int a int dest = base1; // Indexes int a - + // Move first element of second run and deal with degenerate cases ts.Writes.write(a, dest++, a[cursor2++], 1, false, false); ts.markArray(1, dest); @@ -714,7 +714,7 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { ts.markArray(1, dest + len2); return; } - + int minGallop = ts.minGallop; // " " " " " outer: while (true) { @@ -742,7 +742,7 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { break outer; } } while ((count1 | count2) < minGallop); - + /* * One run is winning so consistently that galloping may be a * huge win. So try that, and continue galloping until (if ever) @@ -763,7 +763,7 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { ts.markArray(2, cursor2); if (--len2 == 0) break outer; - + count2 = gallopLeft(ts, tmp[cursor1], a, cursor2, len2, 0); if (count2 != 0) { ts.Writes.arraycopy(a, cursor2, a, dest, count2, 1, true, false); @@ -784,7 +784,7 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { minGallop += 2; // Penalize for leaving gallop mode } // End of "outer" loop ts.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field - + if (len1 == 1) { ts.Writes.arraycopy(a, cursor2, a, dest, len2, 1, true, false); ts.Writes.write(a, dest + len2, tmp[cursor1], 1, false, false); // Last elt of run 1 to end of merge @@ -796,7 +796,7 @@ private void mergeLo(TimSorting ts, int base1, int len1, int base2, int len2) { ts.Writes.arraycopy(tmp, cursor1, a, dest, len1, 1, true, false); } } - + /** * Like mergeLo, except that this method should be called only if * len1 >= len2; mergeLo should be called if len1 <= len2. (Either method @@ -813,11 +813,11 @@ private void mergeHi(TimSorting ts, int base1, int len1, int base2, int len2) { int[] a = ts.a; // For performance int[] tmp = ensureCapacity(len2); ts.Writes.arraycopy(a, base2, tmp, 0, len2, 1, true, true); - + int cursor1 = base1 + len1 - 1; // Indexes into a int cursor2 = len2 - 1; // Indexes into tmp array int dest = base2 + len2 - 1; // Indexes into a - + // Move last element of first run and deal with degenerate cases ts.Writes.write(a, dest--, a[cursor1--], 1, false, false); ts.markArray(1, dest); @@ -834,13 +834,13 @@ private void mergeHi(TimSorting ts, int base1, int len1, int base2, int len2) { ts.markArray(1, dest); return; } - + int minGallop = ts.minGallop; // " " " " " outer: while (true) { int count1 = 0; // Number of times in a row that first run won int count2 = 0; // Number of times in a row that second run won - + /* * Do the straightforward thing until (if ever) one run * appears to win consistently. @@ -863,7 +863,7 @@ private void mergeHi(TimSorting ts, int base1, int len1, int base2, int len2) { break outer; } } while ((count1 | count2) < minGallop); - + /* * One run is winning so consistently that galloping may be a * huge win. So try that, and continue galloping until (if ever) @@ -883,7 +883,7 @@ private void mergeHi(TimSorting ts, int base1, int len1, int base2, int len2) { ts.markArray(1, dest); if (--len2 == 1) break outer; - + count2 = len2 - gallopLeft(ts, a[cursor1], tmp, 0, len2, len2 - 1); if (count2 != 0) { dest -= count2; @@ -905,7 +905,7 @@ private void mergeHi(TimSorting ts, int base1, int len1, int base2, int len2) { minGallop += 2; // Penalize for leaving gallop mode } // End of "outer" loop ts.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field - + if (len2 == 1) { dest -= len1; cursor1 -= len1; @@ -947,4 +947,4 @@ private int[] ensureCapacity(int minCapacity) { } return this.tmp; } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/TwinSorting.java b/src/main/java/sorts/templates/TwinSorting.java index 17bfa764..4b0527eb 100644 --- a/src/main/java/sorts/templates/TwinSorting.java +++ b/src/main/java/sorts/templates/TwinSorting.java @@ -214,4 +214,4 @@ public void tailsort(int[] array, int nmemb) Writes.deleteExternalArray(swap); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/UnstableGrailSorting.java b/src/main/java/sorts/templates/UnstableGrailSorting.java index 5e91f2d3..8932f0b8 100644 --- a/src/main/java/sorts/templates/UnstableGrailSorting.java +++ b/src/main/java/sorts/templates/UnstableGrailSorting.java @@ -352,4 +352,4 @@ protected void grailCommonSort(int[] arr, int pos, int len) { this.grailInsertSort(arr, pos, blockLen); this.grailMergeWithoutBuffer(arr, pos, blockLen, len - blockLen); } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/templates/WikiSorting.java b/src/main/java/sorts/templates/WikiSorting.java index ce3a959d..198fc254 100644 --- a/src/main/java/sorts/templates/WikiSorting.java +++ b/src/main/java/sorts/templates/WikiSorting.java @@ -143,7 +143,7 @@ int length() { } } -final public class WikiSorting { +public final class WikiSorting { // use a small cache to speed up some of the operations // since the cache size is fixed, it's still O(1) memory! // just keep in mind that making it too small ruins the point (nothing will fit into it), @@ -1065,4 +1065,4 @@ else if (buffer2.length() > 0) if (!iterator.nextLevel()) break; } } -} \ No newline at end of file +} diff --git a/src/main/java/sorts/tests/RotationTest.java b/src/main/java/sorts/tests/RotationTest.java index e7c0b07a..863f2e66 100644 --- a/src/main/java/sorts/tests/RotationTest.java +++ b/src/main/java/sorts/tests/RotationTest.java @@ -5,7 +5,7 @@ import utils.Rotations; /* - * + * The MIT License (MIT) Copyright (c) 2021 ArrayV 4.0 Team @@ -29,12 +29,12 @@ this software and associated documentation files (the "Software"), to deal in * */ -final public class RotationTest extends Sort { +public final class RotationTest extends Sort { final double BLOCK_DIV = 6.98; public RotationTest(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); - + this.setSortListName("Rotations"); this.setRunAllSortsName("Rotation Test"); this.setRunSortName("Rotation Test"); @@ -46,11 +46,11 @@ public RotationTest(ArrayVisualizer arrayVisualizer) { this.setUnreasonableLimit(0); this.setBogoSort(false); } - + @Override public void runSort(int[] array, int length, int bucketCount) { int blockSize = (int)(length / BLOCK_DIV); - + for (int i = 0; i < blockSize; i++) { Writes.write(array, i, i + length - blockSize, 0.05, false, false); } diff --git a/src/main/java/threads/MultipleSortThread.java b/src/main/java/threads/MultipleSortThread.java index bc673b6e..5a756b7b 100644 --- a/src/main/java/threads/MultipleSortThread.java +++ b/src/main/java/threads/MultipleSortThread.java @@ -115,4 +115,4 @@ public int getSortCount() { public int getCategoryCount() { return this.categoryCount; } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunAllSorts.java b/src/main/java/threads/RunAllSorts.java index 9ed55748..cbc06f65 100644 --- a/src/main/java/threads/RunAllSorts.java +++ b/src/main/java/threads/RunAllSorts.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunAllSorts { +public final class RunAllSorts { private ArrayVisualizer arrayVisualizer; private ArrayList allSortThreads; @@ -71,4 +71,4 @@ public void reportAllSorts(int[] array) { this.arrayVisualizer.setHeading("Finished!!"); this.arrayVisualizer.updateNow(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunConcurrentSorts.java b/src/main/java/threads/RunConcurrentSorts.java index cde8cf1d..55eaa3f7 100644 --- a/src/main/java/threads/RunConcurrentSorts.java +++ b/src/main/java/threads/RunConcurrentSorts.java @@ -53,7 +53,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunConcurrentSorts extends MultipleSortThread { +public final class RunConcurrentSorts extends MultipleSortThread { private Sort FoldSort; private Sort CreaseSort; private Sort MatrixSort; @@ -178,4 +178,4 @@ public void run() { arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunDistributionSorts.java b/src/main/java/threads/RunDistributionSorts.java index 05bdc632..d417a343 100644 --- a/src/main/java/threads/RunDistributionSorts.java +++ b/src/main/java/threads/RunDistributionSorts.java @@ -50,7 +50,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunDistributionSorts extends MultipleSortThread { +public final class RunDistributionSorts extends MultipleSortThread { private Sort CountingSort; private Sort PigeonholeSort; private Sort OptimizedPigeonholeSort; @@ -162,4 +162,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunExchangeSorts.java b/src/main/java/threads/RunExchangeSorts.java index 36d0b0fa..246dea14 100644 --- a/src/main/java/threads/RunExchangeSorts.java +++ b/src/main/java/threads/RunExchangeSorts.java @@ -61,7 +61,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunExchangeSorts extends MultipleSortThread { +public final class RunExchangeSorts extends MultipleSortThread { private Sort UnoptimizedBubbleSort; private Sort BubbleSort; private Sort UnoptimizedCocktailShakerSort; diff --git a/src/main/java/threads/RunHybridSorts.java b/src/main/java/threads/RunHybridSorts.java index de2588ce..9387f0bf 100644 --- a/src/main/java/threads/RunHybridSorts.java +++ b/src/main/java/threads/RunHybridSorts.java @@ -64,7 +64,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunHybridSorts extends MultipleSortThread { +public final class RunHybridSorts extends MultipleSortThread { private Sort HybridCombSort; private Sort IntroCircleSortRecursive; private Sort IntroCircleSortIterative; @@ -212,4 +212,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunImpracticalSorts.java b/src/main/java/threads/RunImpracticalSorts.java index c72730d6..94a5ee35 100644 --- a/src/main/java/threads/RunImpracticalSorts.java +++ b/src/main/java/threads/RunImpracticalSorts.java @@ -55,7 +55,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunImpracticalSorts extends MultipleSortThread { +public final class RunImpracticalSorts extends MultipleSortThread { private Sort BadSort; private Sort StoogeSort; private Sort SillySort; @@ -188,4 +188,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunInsertionSorts.java b/src/main/java/threads/RunInsertionSorts.java index 5e7c7807..731f3d0a 100644 --- a/src/main/java/threads/RunInsertionSorts.java +++ b/src/main/java/threads/RunInsertionSorts.java @@ -47,7 +47,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunInsertionSorts extends MultipleSortThread { +public final class RunInsertionSorts extends MultipleSortThread { private Sort InsertionSort; private Sort DoubleInsertionSort; private Sort BinaryInsertionSort; @@ -140,4 +140,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunMergeSorts.java b/src/main/java/threads/RunMergeSorts.java index 668dcd36..3d5ca669 100644 --- a/src/main/java/threads/RunMergeSorts.java +++ b/src/main/java/threads/RunMergeSorts.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunMergeSorts extends MultipleSortThread { +public final class RunMergeSorts extends MultipleSortThread { private Sort MergeSort; private Sort BottomUpMergeSort; private Sort TwinSort; @@ -135,4 +135,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunMiscellaneousSorts.java b/src/main/java/threads/RunMiscellaneousSorts.java index bea143e6..87d68b2b 100644 --- a/src/main/java/threads/RunMiscellaneousSorts.java +++ b/src/main/java/threads/RunMiscellaneousSorts.java @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunMiscellaneousSorts extends MultipleSortThread { +public final class RunMiscellaneousSorts extends MultipleSortThread { private Sort PancakeSort; private Sort BurntPancakeSort; @@ -91,4 +91,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunQuickSorts.java b/src/main/java/threads/RunQuickSorts.java index 65f35e85..40a11db9 100644 --- a/src/main/java/threads/RunQuickSorts.java +++ b/src/main/java/threads/RunQuickSorts.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunQuickSorts extends MultipleSortThread { +public final class RunQuickSorts extends MultipleSortThread { public RunQuickSorts(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer); diff --git a/src/main/java/threads/RunSelectionSorts.java b/src/main/java/threads/RunSelectionSorts.java index 8c83f183..bef384bd 100644 --- a/src/main/java/threads/RunSelectionSorts.java +++ b/src/main/java/threads/RunSelectionSorts.java @@ -50,7 +50,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunSelectionSorts extends MultipleSortThread { +public final class RunSelectionSorts extends MultipleSortThread { private Sort SelectionSort; private Sort DoubleSelectionSort; private Sort CycleSort; @@ -155,4 +155,4 @@ public void run() { }); arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/threads/RunSort.java b/src/main/java/threads/RunSort.java index de33480f..d46d3032 100644 --- a/src/main/java/threads/RunSort.java +++ b/src/main/java/threads/RunSort.java @@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class RunSort { +public final class RunSort { private ArrayManager arrayManager; private ArrayVisualizer arrayVisualizer; private Delays delayOps; @@ -252,4 +252,4 @@ else if (networks) { arrayVisualizer.runSortingThread(); } -} \ No newline at end of file +} diff --git a/src/main/java/utils/Delays.java b/src/main/java/utils/Delays.java index 109246f5..97102944 100644 --- a/src/main/java/utils/Delays.java +++ b/src/main/java/utils/Delays.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Delays { +public final class Delays { private volatile double SLEEPRATIO; private volatile boolean SKIPPED; @@ -146,4 +146,4 @@ public void sleep(double millis) { this.currentDelay = 0; } -} \ No newline at end of file +} diff --git a/src/main/java/utils/Highlights.java b/src/main/java/utils/Highlights.java index f68a5121..356e18d5 100644 --- a/src/main/java/utils/Highlights.java +++ b/src/main/java/utils/Highlights.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Highlights { +public final class Highlights { private volatile int[] highlights; private volatile byte[] markCounts; @@ -200,4 +200,4 @@ public synchronized void clearAllMarks() { this.markCount = 0; ArrayVisualizer.updateNow(); } -} \ No newline at end of file +} diff --git a/src/main/java/utils/MultipleScript.java b/src/main/java/utils/MultipleScript.java index 64470a64..c1729061 100644 --- a/src/main/java/utils/MultipleScript.java +++ b/src/main/java/utils/MultipleScript.java @@ -11,7 +11,7 @@ import panes.JErrorPane; import sorts.templates.Sort; -final public class MultipleScript { +public final class MultipleScript { public static class ScriptCommand { public static enum CommandType { SortCall, diff --git a/src/main/java/utils/PerlinNoise.java b/src/main/java/utils/PerlinNoise.java index a10d68c3..8a1f87ab 100644 --- a/src/main/java/utils/PerlinNoise.java +++ b/src/main/java/utils/PerlinNoise.java @@ -53,4 +53,4 @@ public static float returnFracBrownNoise(float x, int octave) } return f; } -} \ No newline at end of file +} diff --git a/src/main/java/utils/Reads.java b/src/main/java/utils/Reads.java index dee8dbe3..e0b44d9b 100644 --- a/src/main/java/utils/Reads.java +++ b/src/main/java/utils/Reads.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Reads { +public final class Reads { private volatile long comparisons; public volatile ArrayList networkIndices; diff --git a/src/main/java/utils/Renderer.java b/src/main/java/utils/Renderer.java index 61360bd5..a19cc3e4 100644 --- a/src/main/java/utils/Renderer.java +++ b/src/main/java/utils/Renderer.java @@ -49,7 +49,7 @@ public boolean resized() { } } -final public class Renderer { +public final class Renderer { private volatile double xScale; private volatile double yScale; @@ -224,4 +224,4 @@ public void drawVisual(VisualStyles VisualStyles, int[][] arrays, ArrayVisualize this.updateVisualsPerArray(ArrayVisualizer, arrays[0], ArrayVisualizer.getCurrentLength()); VisualStyles.drawVisual(arrays[0], ArrayVisualizer, this, Highlights); } -} \ No newline at end of file +} diff --git a/src/main/java/utils/ShuffleInfo.java b/src/main/java/utils/ShuffleInfo.java index e62019c1..0ef97922 100644 --- a/src/main/java/utils/ShuffleInfo.java +++ b/src/main/java/utils/ShuffleInfo.java @@ -5,7 +5,7 @@ import main.ArrayVisualizer; -final public class ShuffleInfo { +public final class ShuffleInfo { final boolean isDistribution; final Distributions distribution; final Shuffles shuffle; diff --git a/src/main/java/utils/Shuffles.java b/src/main/java/utils/Shuffles.java index bfb5945b..8f50a884 100644 --- a/src/main/java/utils/Shuffles.java +++ b/src/main/java/utils/Shuffles.java @@ -1484,4 +1484,4 @@ public void shuffle(int[] array, int start, int end, double sleep, Writes Writes public abstract String getName(); public abstract void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes); -} \ No newline at end of file +} diff --git a/src/main/java/utils/Statistics.java b/src/main/java/utils/Statistics.java index fa9e0f71..cee06169 100644 --- a/src/main/java/utils/Statistics.java +++ b/src/main/java/utils/Statistics.java @@ -4,7 +4,7 @@ import main.ArrayVisualizer; -final public class Statistics { +public final class Statistics { public long frameTimeMillis; private String sortCategory; @@ -120,4 +120,4 @@ public String getAuxAllocAmount() { public String getSegments() { return this.segments; } -} \ No newline at end of file +} diff --git a/src/main/java/utils/Timer.java b/src/main/java/utils/Timer.java index e8adbfa7..95fbea79 100644 --- a/src/main/java/utils/Timer.java +++ b/src/main/java/utils/Timer.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal * */ -final public class Timer { +public final class Timer { private DecimalFormat formatter; private volatile String minuteFormat; @@ -141,4 +141,4 @@ void manualAddTime(long milliseconds) { public void manualSetTime(long milliseconds) { this.realTimer = milliseconds; } -} \ No newline at end of file +} diff --git a/src/main/java/utils/Writes.java b/src/main/java/utils/Writes.java index 7153989b..8391706d 100644 --- a/src/main/java/utils/Writes.java +++ b/src/main/java/utils/Writes.java @@ -37,7 +37,7 @@ of this software and associated documentation files (the "Software"), to deal * * @author S630690 */ -final public class Writes { +public final class Writes { private volatile long reversals; private volatile long swaps; private volatile long auxWrites; @@ -496,4 +496,4 @@ public void startLap() { public void stopLap() { if (Timer.timerEnabled()) Timer.stopLap(); } -} \ No newline at end of file +} diff --git a/template.java b/template.java index 9b00345e..1c015ad4 100644 --- a/template.java +++ b/template.java @@ -3,7 +3,7 @@ import main.ArrayVisualizer; import sorts.templates.Sort; -final public class MySort extends Sort { +public final class MySort extends Sort { public MySort(ArrayVisualizer arrayVisualizer) { super(arrayVisualizer);