diff --git a/src/main/ArrayManager.java b/src/main/ArrayManager.java index 0fcb71cf..42f63b6a 100644 --- a/src/main/ArrayManager.java +++ b/src/main/ArrayManager.java @@ -13,7 +13,6 @@ MIT License Copyright (c) 2019 w0rthy -Copyright (c) 2020 ArrayV 4.0 Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -39,23 +38,23 @@ final public class ArrayManager { private int[] presortedArray; private utils.Shuffles[] shuffleTypes; private utils.Distributions[] distributionTypes; - private String[] shuffleIDs = { + private String[] shuffleIDs/* = { "Randomly", "Backwards", "Slight Shuffle", "No Shuffle", "Sorted", "Reverse Sorted", - "Scrambled Tail", "Scrambled Head", "Noisy", "Scrambled Odds", + "Scrambled Tail", "Scrambled Head", "Noisy", "Shuffled Odds", "Final Merge Pass", "Sawtooth", "Reversed Final Merge", "Reversed Sawtooth", "Pipe Organ", "Final Bitonic Pass", "Interlaced", "Double Layered", "Final Radix", "Recursive Final Radix", "Half Rotation", "Half Reversed", "BST Traversal", "Logarithmic Slopes", - "Heapified", "Reversed Poplarified", "First Circle Pass", "Final Pairwise Pass", + "Heapified", "Revered Poplarified", "First Circle Pass", "Final Pairwise Pass", "Recursive Reversal", "Gray Code Fractal", "Sierpinski Triangle", "Triangular" - }; - private String[] distributionIDs = { + }*/; + private String[] distributionIDs/* = { "Linear", "Few Unique", "Random", "Quadratic", "Square Root", "Centered Cubic", "Centered Quintic", "Perlin Noise", "Perlin Noise Curve", "Bell Curve", "Ruler", "Blancmange Curve", "Cantor Function", "Sum of Divisors", "Fly Straight, Damnit!", "Decreasing Random" - }; + }*/; private volatile boolean MUTABLE; @@ -78,6 +77,14 @@ public ArrayManager(ArrayVisualizer arrayVisualizer) { this.Delays = ArrayVisualizer.getDelays(); this.Highlights = ArrayVisualizer.getHighlights(); this.Writes = ArrayVisualizer.getWrites(); + + this.shuffleIDs = new String[this.shuffleTypes.length]; + for (int i = 0; i < this.shuffleTypes.length; i++) + this.shuffleIDs[i] = this.shuffleTypes[i].getName(); + + this.distributionIDs = new String[this.distributionTypes.length]; + for (int i = 0; i < this.distributionTypes.length; i++) + this.distributionIDs[i] = this.distributionTypes[i].getName(); this.MUTABLE = true; } @@ -93,13 +100,11 @@ public void toggleMutableLength(boolean Bool) { public void initializeArray(int[] array) { int currentLen = ArrayVisualizer.getCurrentLength(); - int[] temp = new int[currentLen]; double uniqueFactor = (double)currentLen/ArrayVisualizer.getUniqueItems(); for(int i = 0; i < currentLen; i++) - temp[i] = (int)(uniqueFactor*(int)(i/uniqueFactor))+(int)uniqueFactor/2; + array[i] = (int)(uniqueFactor*(int)(i/uniqueFactor))+(int)uniqueFactor/2; - Distributions.initializeArray(temp, this.ArrayVisualizer); - System.arraycopy(temp, 0, array, 0, currentLen); + Distributions.initializeArray(array, this.ArrayVisualizer); } public void initializePresortedArray() { @@ -184,4 +189,4 @@ public void refreshArray(int[] array, int currentLen, ArrayVisualizer ArrayVisua ArrayVisualizer.resetAllStatistics(); } -} \ No newline at end of file +} diff --git a/src/utils/Distributions.java b/src/utils/Distributions.java index 95cc4021..78a1ca5a 100644 --- a/src/utils/Distributions.java +++ b/src/utils/Distributions.java @@ -1,8 +1,5 @@ package utils; -import java.util.Arrays; -import java.util.Random; - import main.ArrayVisualizer; /* @@ -33,39 +30,50 @@ of this software and associated documentation files (the "Software"), to deal public enum Distributions { LINEAR { + public String getName() { + return "Linear"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, SIMILAR { + public String getName() { + return "Few Unique"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); - int l = 0, r, t = Math.min(currentLen, 8); - for(int i = 0; i < t; i++) - if(random.nextDouble() < 0.5) l++; - r = currentLen-(t-l); - - int i = 0; - for(; i < l; i++) array[i] = (int) (currentLen * 0.25); - for(; i < r; i++) array[i] = currentLen / 2; - for(; i < currentLen; i++) array[i] = (int) (currentLen * 0.75); + int i; + for(i = 0; i < currentLen - 8; i++) { + array[i] = currentLen / 2; + } + for(; i < currentLen; i++) { + array[i] = (int) (Math.random() < 0.5 ? currentLen * 0.75 : currentLen * 0.25); + } } }, RANDOM { + public String getName() { + return "Random"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); + int[] temp = new int[currentLen]; + + for(int i = 0; i < currentLen; i++) + temp[i] = array[(int)(Math.random()*currentLen)]; - int[] temp = Arrays.copyOf(array, currentLen); for(int i = 0; i < currentLen; i++) - array[i] = temp[random.nextInt(currentLen)]; + array[i] = temp[i]; } }, SQUARE { + public String getName() { + return "Quadratic"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -75,6 +83,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, SQRT { + public String getName() { + return "Square Root"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -84,6 +95,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, CUBIC { + public String getName() { + return "Centered Cubic"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -95,6 +109,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, QUINTIC { + public String getName() { + return "Centered Quintic"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -106,15 +123,17 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, PERLIN_NOISE { + public String getName() { + return "Perlin Noise"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); - int[] perlinNoise = new int[currentLen]; + int[] temp = new int[currentLen]; float step = 1f / currentLen; - float randomStart = (float) (random.nextInt(currentLen)); + float randomStart = (float) (Math.random() * currentLen); int octave = (int) (Math.log(currentLen) / Math.log(2)); for(int i = 0; i < currentLen; i++) { @@ -147,18 +166,27 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } } - int[] temp = Arrays.copyOf(array, currentLen); + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } for(int i = 0; i < currentLen; i++) { array[i] = temp[Math.min(perlinNoise[i], currentLen-1)]; } } }, PERLIN_NOISE_CURVE { + public String getName() { + return "Perlin Noise Curve"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - int[] temp = Arrays.copyOf(array, currentLen); + int[] temp = new int[currentLen]; + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } + for(int i = 0; i < currentLen; i++) { int value = 0 - (int) (PerlinNoise.returnNoise((float) array[i] / currentLen) * currentLen); array[i] = temp[Math.min(value, currentLen-1)]; @@ -166,6 +194,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, BELL_CURVE { + public String getName() { + return "Bell Curve"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -174,7 +205,11 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int constant = 1264; double factor = currentLen / 512d; - int[] temp = Arrays.copyOf(array, currentLen); + int[] temp = new int[currentLen]; + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } + for(int i = 0; i < currentLen; i++) { double square = Math.pow(position, 2); double negativeSquare = 0 - square; @@ -190,6 +225,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, RULER { + public String getName() { + return "Ruler"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -221,12 +259,19 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { } }, BLANCMANGE { + public String getName() { + return "Blancmange Curve"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); int floorLog2 = (int)(Math.log(currentLen)/Math.log(2)); - int[] temp = Arrays.copyOf(array, currentLen); + int[] temp = new int[currentLen]; + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } + for(int i = 0; i < currentLen; i++) { int value = (int)(currentLen * curveSum(floorLog2, (double)i/currentLen)); array[i] = temp[value]; @@ -249,11 +294,17 @@ public double triangleWave(double x) { } }, CANTOR { + public String getName() { + return "Cantor Function"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - int[] temp = Arrays.copyOf(array, currentLen); + int[] temp = new int[currentLen]; + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } cantor(array, temp, 0, currentLen, 0, currentLen-1); } @@ -276,10 +327,14 @@ public void cantor(int[] array, int[] temp, int a, int b, int min, int max) { } }, DIVISORS {//O(n^1.5) + public String getName() { + return "Sum of Divisors"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); int[] n = new int[currentLen]; + int[] temp = new int[currentLen]; n[0] = 0; n[1] = 1; @@ -290,7 +345,10 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { if(n[i] > max) max = n[i]; } - int[] temp = Arrays.copyOf(array, currentLen); + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } + double scale = Math.min((currentLen-1)/max, 1); for(int i = 0; i < currentLen; i++) { array[i] = temp[(int)(n[i]*scale)]; @@ -309,10 +367,14 @@ public int sumDivisors(int n) { } }, FSD {// fly straight dangit (OEIS A133058) + public String getName() { + return "Fly Straight, Damnit!"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); int[] fsd = new int[currentLen]; + int[] temp = new int[currentLen]; double max; max = fsd[0] = fsd[1] = 1; @@ -322,7 +384,9 @@ public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { if(fsd[i] > max) max = fsd[i]; } - int[] temp = Arrays.copyOf(array, currentLen); + for(int i = 0; i < currentLen; i++) + temp[i] = array[i]; + double scale = Math.min((currentLen-1)/max, 1); for(int i = 0; i < currentLen; i++) array[i] = temp[(int)(fsd[i]*scale)]; @@ -334,18 +398,25 @@ public int gcd(int a, int b) { } }, REVLOG { + public String getName() { + return "Decreasing Random"; + } @Override public void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); - int[] temp = Arrays.copyOf(array, currentLen); + int[] temp = new int[currentLen]; + for(int i = 0; i < currentLen; i++) { + temp[i] = array[i]; + } + for(int i = 0; i < currentLen; i++){ - int r = random.nextInt(currentLen - i) + i; - array[i] = temp[r]; + int random = (int) (Math.random() * (currentLen - i)) + i; + array[i] = temp[random]; } } }; + public abstract String getName(); public abstract void initializeArray(int[] array, ArrayVisualizer ArrayVisualizer); -} \ No newline at end of file +} diff --git a/src/utils/Shuffles.java b/src/utils/Shuffles.java index 37221ab7..2db32bc1 100644 --- a/src/utils/Shuffles.java +++ b/src/utils/Shuffles.java @@ -1,9 +1,7 @@ package utils; -import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; -import java.util.Random; import main.ArrayVisualizer; @@ -41,7 +39,9 @@ public enum Shuffles { // If you want to learn why the random shuffle was changed, // I highly encourage you read this. It's quite fascinating: // http://datagenetics.com/blog/november42014/index.html - + public String getName() { + return "Randomly"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -50,6 +50,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REVERSE { + public String getName() { + return "Backwards"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -58,15 +61,16 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, ALMOST { + public String getName() { + return "Slight Shuffle"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); for(int i = 0; i < Math.max(currentLen / 20, 1); i++){ - Writes.swap(array, random.nextInt(currentLen), random.nextInt(currentLen), 0, true, false); - - if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(20); + Writes.swap(array, (int)(Math.random()*currentLen), (int)(Math.random()*currentLen), 0, true, false); + if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(2); } /* @@ -85,6 +89,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, ALREADY { + public String getName() { + return "No Shuffle"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -95,34 +102,41 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, SORTED { + public String getName() { + return "Sorted"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - this.sort(array, 0, currentLen, delay ? 1 : 0, Writes); + this.sort(array, 0, currentLen, delay, Writes); } }, REV_SORTED { + public String getName() { + return "Reverse Sorted"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - this.sort(array, 0, currentLen, delay ? 1 : 0, Writes); + this.sort(array, 0, currentLen, delay, Writes); Writes.reversal(array, 0, currentLen-1, delay ? 1 : 0, true, false); } }, SHUFFLED_TAIL { + public String getName() { + return "Scrambled Tail"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - Random random = new Random(); - int j = 0, k = currentLen; int[] temp = new int[currentLen]; for(int i = 0; j < k; i++) { - if(random.nextDouble() < 1/8d) + if(Math.random() < 1/8d) Writes.write(temp, --k, array[i], 0, false, true); else Writes.write(temp, j++, array[i], 0, false, true); @@ -135,17 +149,18 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, SHUFFLED_HEAD { + public String getName() { + return "Scrambled Head"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - Random random = new Random(); - int j = currentLen, k = 0; int[] temp = new int[currentLen]; for(int i = currentLen-1; j > k; i--) { - if(random.nextDouble() < 1/8d) + if(Math.random() < 1/8d) Writes.write(temp, k++, array[i], 0, false, true); else Writes.write(temp, --j, array[i], 0, false, true); @@ -158,26 +173,29 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, NOISY { + public String getName() { + return "Noisy"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - Random random = new Random(); - int i, size = Math.max(4, (int)(Math.sqrt(currentLen)/2)); - for(i = 0; i+size <= currentLen; i += random.nextInt(size-1)+1) - shuffle(array, i, i+size, delay ? 0.5 : 0, Writes); - shuffle(array, i, currentLen, delay ? 0.5 : 0, Writes); + for(i = 0; i+size <= currentLen; i+=(int) (Math.random() * size + 1)) + shuffle(array, i, i+size, delay ? 1 : 0, Writes); + shuffle(array, i, currentLen, delay ? 1 : 0, Writes); } }, SHUFFLED_ODDS { + public String getName() { + return "Shuffled Odds"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); - Random random = new Random(); for(int i = 1; i < currentLen; i += 2){ - int randomIndex = (((random.nextInt(currentLen - i) / 2)) * 2) + i; + int randomIndex = (((int) ((Math.random() * (currentLen - i)) / 2)) * 2) + i; Writes.swap(array, i, randomIndex, 0, true, false); if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(2); @@ -185,6 +203,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, FINAL_MERGE { + public String getName() { + return "Final Merge Pass"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -204,6 +225,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, SAWTOOTH { + public String getName() { + return "Sawtooth"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -223,6 +247,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REV_FINAL_MERGE { + public String getName() { + return "Reversed Final Merge"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -244,6 +271,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REV_SAWTOOTH { + public String getName() { + return "Reversed Sawtooth"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -265,6 +295,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, ORGAN { + public String getName() { + return "Pipe Organ"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -283,6 +316,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, FINAL_BITONIC { + public String getName() { + return "Final Bitonic Pass"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -303,6 +339,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, INTERLACED { + public String getName() { + return "Interlaced"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -327,6 +366,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, DOUBLE_LAYERED { + public String getName() { + return "Double Layered"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -338,6 +380,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, FINAL_RADIX { + public String getName() { + return "Final Radix"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -357,12 +402,15 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REC_RADIX { + public String getName() { + return "Recursive Final Radix"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); boolean delay = ArrayVisualizer.shuffleEnabled(); - weaveRec(array, 0, currentLen, 1, delay ? 0.5 : 0, Writes); + weaveRec(array, 0, currentLen, 1, delay ? 1 : 0, Writes); } public void weaveRec(int[] array, int pos, int length, int gap, double delay, Writes Writes) { @@ -386,6 +434,9 @@ public void weaveRec(int[] array, int pos, int length, int gap, double delay, Wr } }, HALF_ROTATION { + public String getName() { + return "Half Rotation"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -408,6 +459,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, PARTIAL_REVERSE { + public String getName() { + return "Half Reversed"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -418,10 +472,12 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, BST_TRAVERSAL { + public String getName() { + return "BST Traversal"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); - int[] temp = Arrays.copyOf(array, currentLen); // credit to sam walko/anon @@ -442,7 +498,7 @@ class Subarray { Subarray sub = q.poll(); if(sub.start != sub.end) { int mid = (sub.start + sub.end)/2; - Writes.write(array, i, temp[mid], 0, true, false); + Writes.write(array, i, mid, 0, true, false); if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(1); i++; q.add(new Subarray(sub.start, mid)); @@ -452,6 +508,9 @@ class Subarray { } }, LOG_SLOPES { + public String getName() { + return "Logarithmic Slopes"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -471,6 +530,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, HEAPIFIED { + public String getName() { + return "Heapified"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -481,6 +543,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REV_POPLAR { + public String getName() { + return "Reversed Poplarified"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -492,6 +557,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, CIRCLE { + public String getName() { + return "First Circle Pass"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -525,6 +593,9 @@ public void circleSortRoutine(int[] array, int lo, int hi, int end, double sleep } }, PAIRWISE { + public String getName() { + return "Final Pairwise Pass"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -561,6 +632,9 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } }, REC_REV { + public String getName() { + return "Recursive Reversal"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -580,6 +654,9 @@ public void reversalRec(int[] array, int a, int b, double sleep, Writes Writes) } }, GRAY_CODE { + public String getName() { + return "Gray Code Fractal"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -601,13 +678,19 @@ public void reversalRec(int[] array, int a, int b, boolean bw, double sleep, Wri } }, SIERPINSKI { + public String getName() { + return "Sierpinski Triangle"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); + int[] temp = new int[currentLen]; int[] triangle = new int[currentLen]; triangleRec(triangle, 0, currentLen); - int[] temp = Arrays.copyOf(array, currentLen); + for(int i = 0; i < currentLen; i++) + temp[i] = array[i]; + for(int i = 0; i < currentLen; i++) Writes.write(array, i, temp[triangle[i]], 1, true, false); } @@ -629,6 +712,9 @@ public void triangleRec(int[] array, int a, int b) { } }, TRIANGULAR { + public String getName() { + return "Triangular"; + } @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { int currentLen = ArrayVisualizer.getCurrentLength(); @@ -687,7 +773,7 @@ else if(k == n) { } }; - public void sort(int[] array, int start, int end, double sleep, Writes Writes) { + public void sort(int[] array, int start, int end, boolean delay, Writes Writes) { int min = array[start], max = min; for(int i = start+1; i < end; i++) { if(array[i] < min) min = array[i]; @@ -703,19 +789,19 @@ public void sort(int[] array, int start, int end, double sleep, Writes Writes) { for(int i = 0, j = start; i < size; i++) { while(holes[i] > 0) { Writes.write(holes, i, holes[i] - 1, 0, false, true); - Writes.write(array, j, i + min, sleep, true, false); + Writes.write(array, j, i + min, delay ? 1 : 0, true, false); j++; } } } public void shuffle(int[] array, int start, int end, double sleep, Writes Writes) { - Random random = new Random(); for(int i = start; i < end; i++){ - int randomIndex = random.nextInt(end - i) + i; + int randomIndex = (int) (Math.random() * (end - i)) + i; Writes.swap(array, i, randomIndex, sleep, true, false); } } - + + public abstract String getName(); public abstract void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes); -} \ No newline at end of file +}