From b4457aad0e4db635d2335d87be004db182896621 Mon Sep 17 00:00:00 2001 From: Zoey Llewellyn Hewll Date: Mon, 7 Jan 2019 18:50:56 +0800 Subject: [PATCH 1/3] Reformat code (whitespace) --- src/array/visualizer/AAFrame.java | 6 +- src/array/visualizer/ArrayController.java | 8 +- src/array/visualizer/ArrayVisualizer.java | 866 ++++++++++-------- src/array/visualizer/SortPrompt.java | 166 ++-- src/array/visualizer/UtilFrame.java | 147 +-- src/array/visualizer/ViewPrompt.java | 274 +++--- src/array/visualizer/sort/BitonicSort.java | 21 +- src/array/visualizer/sort/BogoSort.java | 33 +- src/array/visualizer/sort/BubbleSort.java | 23 +- src/array/visualizer/sort/CocktailShaker.java | 24 +- src/array/visualizer/sort/CountingSort.java | 19 +- .../visualizer/sort/DoubleSelection.java | 25 +- src/array/visualizer/sort/GravitySort.java | 45 +- src/array/visualizer/sort/InsertionSort.java | 36 +- src/array/visualizer/sort/MaxHeapSort.java | 77 +- src/array/visualizer/sort/MergeSort.java | 91 +- src/array/visualizer/sort/MergeSortOOP.java | 71 +- src/array/visualizer/sort/MinHeapSort.java | 5 +- src/array/visualizer/sort/QuickSort.java | 34 +- src/array/visualizer/sort/RadixLSD.java | 44 +- .../visualizer/sort/RadixLSDInPlace.java | 36 +- src/array/visualizer/sort/RadixMSD.java | 29 +- src/array/visualizer/sort/SelectionSort.java | 18 +- src/array/visualizer/sort/ShatterSorts.java | 50 +- src/array/visualizer/sort/ShellSort.java | 38 +- src/array/visualizer/sort/Sort.java | 1 + src/array/visualizer/sort/TimeSort.java | 32 +- src/array/visualizer/sort/WeaveMerge.java | 74 +- src/array/visualizer/utils/Analysis.java | 37 +- src/array/visualizer/utils/Searches.java | 28 +- src/array/visualizer/utils/Swaps.java | 66 +- .../visualizer/utils/Transcriptions.java | 97 +- 32 files changed, 1457 insertions(+), 1064 deletions(-) diff --git a/src/array/visualizer/AAFrame.java b/src/array/visualizer/AAFrame.java index f03f9b38..7b013fec 100644 --- a/src/array/visualizer/AAFrame.java +++ b/src/array/visualizer/AAFrame.java @@ -5,11 +5,13 @@ package array.visualizer; /** - * * @author S630690 */ -interface AAFrame { +interface AAFrame +{ abstract void reposition(); + abstract boolean isVisible(); + abstract void dispose(); } diff --git a/src/array/visualizer/ArrayController.java b/src/array/visualizer/ArrayController.java index 2b84dd74..dae7344d 100644 --- a/src/array/visualizer/ArrayController.java +++ b/src/array/visualizer/ArrayController.java @@ -4,7 +4,7 @@ public class ArrayController { - public final int [] array; + public final int[] array; public final int length; public final ArrayList marked; public long aa; @@ -21,19 +21,19 @@ public ArrayController(int length) public void clearMarked() { - for(int i = 0; i < length; i++) + for (int i = 0; i < length; i++) marked.set(i, -5); } public int get(int index) { - aa ++; + aa++; return array[index]; } public int compare(int a, int b) { - comps ++; + comps++; return Integer.compare(array[a], array[b]); } } diff --git a/src/array/visualizer/ArrayVisualizer.java b/src/array/visualizer/ArrayVisualizer.java index 63258d27..2f774553 100644 --- a/src/array/visualizer/ArrayVisualizer.java +++ b/src/array/visualizer/ArrayVisualizer.java @@ -13,6 +13,7 @@ import javax.swing.JFrame; import static array.visualizer.utils.Swaps.*; + import java.awt.BasicStroke; import java.awt.Graphics2D; import java.awt.Polygon; @@ -21,7 +22,8 @@ import javax.sound.midi.Instrument; import javax.swing.JOptionPane; -public class ArrayVisualizer { +public class ArrayVisualizer +{ static final JFrame window = new JFrame(); @@ -30,8 +32,8 @@ public class ArrayVisualizer { static int frames; static int snd = 0; static long nanos; - static Font fon = new Font("TimesRoman", Font.PLAIN, (int)(640/1280.0*25)); - + static Font fon = new Font("TimesRoman", Font.PLAIN, (int) (640 / 1280.0 * 25)); + static boolean CIRCLEDRAW = false; static boolean COLORONLY = false; static boolean PIXELDRAW = false; @@ -41,7 +43,7 @@ public class ArrayVisualizer { static boolean PYRAMIDDRAW = false; static boolean DRAWFLIPPED = false; static boolean XMASDRAW = false; - + static boolean SOUND = false; static double SOUNDMUL = 1.0; static double SLEEPRATIO = 1.0; @@ -51,61 +53,68 @@ public class ArrayVisualizer { static MidiChannel chan; static Thread sortingThread; static boolean SHUFFLEANIM = true; - + static long starttime = 0; static long stoptime = 0; static boolean running = false; static long sleeptime = 0; - + static long rtupdatefreq = 100; //How frequently to update the real time display in ms static double realt = 0d; static long lastrtupdate = 0; - + static int COLORSTRAT = 1; //0 = Solid, 1 = Rainbow, 2 = Segments static Color COLORSTRAT0col = new Color(0, 204, 0); static ArrayList COLORSTRAT2cols = new ArrayList(); - + static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); - + static int cx = 0; static int cy = 0; - - public static double calcVel(){ + + public static double calcVel() + { double count = 1; - for(int i : arrayController.marked) - if(i!=-5) - count+=0.75; + for (int i : arrayController.marked) + if (i != -5) + count += 0.75; return count; } - - public static synchronized void SetSound(boolean val){ + + public static synchronized void SetSound(boolean val) + { SOUND = val; } - + static double addamt = 0.0; - public static void sleep(double milis){ - if(milis <= 0) + + public static void sleep(double milis) + { + if (milis <= 0) return; - double tmp = (milis*(1000.0/arrayController.length)); - tmp = tmp * (1/SLEEPRATIO); + double tmp = (milis * (1000.0 / arrayController.length)); + tmp = tmp * (1 / SLEEPRATIO); addamt += tmp; - if(addamt<1.0) + if (addamt < 1.0) return; - try{ + try + { long actual = System.nanoTime(); - Thread.sleep((long)addamt); - actual = (System.nanoTime()-actual); - addamt-=(double)actual/1000000.0; - if(running) - sleeptime+=actual; - }catch(Exception ex){ + Thread.sleep((long) addamt); + actual = (System.nanoTime() - actual); + addamt -= (double) actual / 1000000.0; + if (running) + sleeptime += actual; + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } } - - public static void main(String[] args) throws Exception { - + + public static void main(String[] args) throws Exception + { + //Segment Colors COLORSTRAT2cols.add(new Color(0, 204, 0)); COLORSTRAT2cols.add(new Color(204, 0, 0)); @@ -120,56 +129,60 @@ public static void main(String[] args) throws Exception { // s++; // } chan = synth.getChannels()[0]; - for(Instrument i : synth.getLoadedInstruments()) - if(i.getName().toLowerCase().trim().contains("sine")){ + for (Instrument i : synth.getLoadedInstruments()) + if (i.getName().toLowerCase().trim().contains("sine")) + { chan.programChange(i.getPatch().getProgram()); break; } - - if(chan.getProgram() == 0) + + if (chan.getProgram() == 0) JOptionPane.showMessageDialog(null, "Could not find a valid instrument. Sound is disabled"); //chan.programChange(synth.getLoadedInstruments()[197].getPatch().getProgram()); - - for(int i = 0; i < arrayController.length; i++) + + for (int i = 0; i < arrayController.length; i++) arrayController.marked.add(-5); rianr(arrayController.array); - + window.setSize(640, 480); window.setLocation(0, 0); window.setVisible(true); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setTitle("Array Visualizer"); - + uf = new UtilFrame(window); - + //AUDIO THREAD new Thread() { @Override public void run() + { + while (true) { - while(true){ - chan.allNotesOff(); - if(SOUND == false){ - continue; - } + chan.allNotesOff(); + if (SOUND == false) + { + continue; + } - int tmp = 1; - for(int i : arrayController.marked) - if(i != -5) + int tmp = 1; + for (int i : arrayController.marked) + if (i != -5) tmp++; - for(int i : arrayController.marked) - if(i != -5){ - int pitch = (int)Math.round((double)arrayController.array[Math.min(Math.max(i, 0), arrayController.length-1)]/arrayController.length*96+16); + for (int i : arrayController.marked) + if (i != -5) + { + int pitch = (int) Math.round((double) arrayController.array[Math.min(Math.max(i, 0), arrayController.length - 1)] / arrayController.length * 96 + 16); //int vel = (int)(((128-pitch)/320.0+0.4) * (128.0/Math.pow(tmp, 0.33))); //int vel = (int)(64.0/Math.pow(tmp, 0.25)); //int vel = (int)((64.0-Math.pow((tmp-1)*10, 0.67d))*SOUNDMUL); - int vel = (int)(64.0/(Math.pow((double)tmp, 0.6)) * ((128-pitch)/64.0+1.0)*0.67*SOUNDMUL); //one day + int vel = (int) (64.0 / (Math.pow((double) tmp, 0.6)) * ((128 - pitch) / 64.0 + 1.0) * 0.67 * SOUNDMUL); //one day chan.noteOn(pitch, vel); } - //((int)((127-(array[Math.min(Math.max(i, 0), array.length-1)]/16.0))/Math.sqrt(calcVel())))*5 - //double tmp = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); - //chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2)); + //((int)((127-(array[Math.min(Math.max(i, 0), array.length-1)]/16.0))/Math.sqrt(calcVel())))*5 + //double tmp = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); + //chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2)); /* ArrayList tmp = new ArrayList(); for(int i : marked) @@ -184,405 +197,449 @@ public void run() double tmpd = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); chan.setPitchBend(8192*2-(int)((tmpd-Math.floor(tmpd))*8192*2)); }while(false);}*/ - try{sleep(1);}catch(Exception ex){ + try + { + sleep(1); + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } } } }.start(); - + //DRAW THREAD - new Thread(){ + new Thread() + { @Override - public void run(){ + public void run() + { int cw = 640; int ch = 480; Image img = window.createVolatileImage(cw, ch); //Graphics g = window.getGraphics(); - Graphics2D g = (Graphics2D)img.getGraphics(); - + Graphics2D g = (Graphics2D) img.getGraphics(); + double xscl, yscl; - while(true){ - - if(window.getWidth()!=cw|| window.getHeight()!=ch || window.getX() != cx || window.getY() != cy){ + while (true) + { + + if (window.getWidth() != cw || window.getHeight() != ch || window.getX() != cx || window.getY() != cy) + { uf.reposition(); - if(v != null && v.isVisible()) + if (v != null && v.isVisible()) v.reposition(); cx = window.getX(); - cy = window.getY(); + cy = window.getY(); } - - if(window.getWidth()!=cw|| window.getHeight()!=ch){ + + if (window.getWidth() != cw || window.getHeight() != ch) + { cw = window.getWidth(); ch = window.getHeight(); img = window.createVolatileImage(cw, ch); - fon = new Font("TimesRoman", Font.PLAIN, (int)(cw/1280.0*25)); - g = (Graphics2D)img.getGraphics(); - + fon = new Font("TimesRoman", Font.PLAIN, (int) (cw / 1280.0 * 25)); + g = (Graphics2D) img.getGraphics(); + } int gamt = 32;//(int)(frames/1000.0%64); g.setColor(new Color(gamt, gamt, gamt)); g.fillRect(0, 0, img.getWidth(null), img.getHeight(null)); - xscl = (double)window.getWidth()/arrayController.length; - yscl = (double)(window.getHeight()-30)/arrayController.length; + xscl = (double) window.getWidth() / arrayController.length; + yscl = (double) (window.getHeight() - 30) / arrayController.length; int amt = 0; - int circamt = arrayController.length/2; + int circamt = arrayController.length / 2; int linkedpixdrawx = 0; int linkedpixdrawy = 0; frames++; - - int halfwidth = window.getWidth()/2; - int halfheight = window.getHeight()/2; - int dotw = (int)(2*(window.getWidth()/640.0)); - int doth = (int)(2*(window.getHeight()/480.0)); - + + int halfwidth = window.getWidth() / 2; + int halfheight = window.getHeight() / 2; + int dotw = (int) (2 * (window.getWidth() / 640.0)); + int doth = (int) (2 * (window.getHeight() / 480.0)); + int sortErrors = 0; - - float strokew = 3f*(window.getWidth()/1920f); + + float strokew = 3f * (window.getWidth() / 1920f); g.setStroke(new BasicStroke(strokew)); - + //CHRISTMAS TREE? - if(XMASDRAW){ - - double trunksize = (double)ch*0.1; - int trunkstart = arrayController.length-(int)(trunksize/yscl); - - double ornamentsize = (double)ch*0.1; - int ornamentend = (int)(ornamentsize/yscl); - + if (XMASDRAW) + { + + double trunksize = (double) ch * 0.1; + int trunkstart = arrayController.length - (int) (trunksize / yscl); + + double ornamentsize = (double) ch * 0.1; + int ornamentend = (int) (ornamentsize / yscl); + int width = 0; - - for(int i = 0; i < arrayController.length; i++){ + + for (int i = 0; i < arrayController.length; i++) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - + int j = arrayController.array[i]; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) g.setColor(Color.BLACK); - else if(jtrunkstart) + else if (j > trunkstart) g.setColor(new Color(102, 34, 0)); else g.setColor(new Color(0, 102, 0)); - - if(jtrunkstart){ //TRUNK - width = (int)(cw*0.2); - }else //TREE - width = (int)((((j-ornamentend)%85)*0.005+0.075)*cw); - - int step = (int)(i*yscl+yscl)-(int)(i*yscl); - g.fillRect(halfwidth-width/2, (int)(i*yscl)+32, width, step); - + + if (j < ornamentend) + { //ORNAMENT + width = (int) (Math.sin(Math.acos((j * yscl) / ornamentsize * 2.0 - 1.0)) * ornamentsize); + } else if (j > trunkstart) + { //TRUNK + width = (int) (cw * 0.2); + } else //TREE + width = (int) ((((j - ornamentend) % 85) * 0.005 + 0.075) * cw); + + int step = (int) (i * yscl + yscl) - (int) (i * yscl); + g.fillRect(halfwidth - width / 2, (int) (i * yscl) + 32, width, step); + } } //HOOP DRAWING (PYRAMID CIRCLES) - else if(PYRAMIDDRAW && CIRCLEDRAW){ - + else if (PYRAMIDDRAW && CIRCLEDRAW) + { + g.setStroke(new BasicStroke(1.0f)); //significantly increased performance - - double maxdiam = (double)Math.min(cw, ch-32); + + double maxdiam = (double) Math.min(cw, ch - 32); double diameter = maxdiam; double diamstep = Math.min(xscl, yscl); - - for(int i = arrayController.length-1; i >= 0; i--){ + + for (int i = arrayController.length - 1; i >= 0; i--) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - + //Set Proper Color - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) g.setColor(Color.BLACK); else g.setColor(getIntColor(arrayController.array[i], sortErrors)); - - int radius = (int)(diameter/2.0); - - if(!DISPARITYDRAW) - g.drawOval(halfwidth-radius, halfheight-radius+15, (int)diameter, (int)diameter); - else{ - int dist = Math.abs(i-arrayController.array[i]); - double disparity = 1.0-((double)Math.min(dist, arrayController.length-dist)/((double)arrayController.length/2.0)); - int actualdiam = (int)(disparity*diameter); - g.drawOval(halfwidth-actualdiam/2, halfheight-radius+15, (int)actualdiam, (int)actualdiam); + + int radius = (int) (diameter / 2.0); + + if (!DISPARITYDRAW) + g.drawOval(halfwidth - radius, halfheight - radius + 15, (int) diameter, (int) diameter); + else + { + int dist = Math.abs(i - arrayController.array[i]); + double disparity = 1.0 - ((double) Math.min(dist, arrayController.length - dist) / ((double) arrayController.length / 2.0)); + int actualdiam = (int) (disparity * diameter); + g.drawOval(halfwidth - actualdiam / 2, halfheight - radius + 15, (int) actualdiam, (int) actualdiam); } - diameter-=diamstep; + diameter -= diamstep; } } //PYRAMID DRAW METHOD - else if(PYRAMIDDRAW){ - + else if (PYRAMIDDRAW) + { + //Setup double curpos = 0.0; - + //Draw loop - for(int i = 0; i < arrayController.length; i++){ + for (int i = 0; i < arrayController.length; i++) + { //Check for sort errors in array - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - + //Set Proper Color - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) g.setColor(Color.BLACK); else g.setColor(getIntColor(arrayController.array[i], sortErrors)); - + //Draw - if(!DRAWFLIPPED){ - int len = (int)((double)arrayController.array[i]/(double)arrayController.length*(double)cw); - int step = (int)(curpos+yscl) - (int)curpos; - g.fillRect(halfwidth-len/2, (int)curpos+32, len, step); + if (!DRAWFLIPPED) + { + int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * (double) cw); + int step = (int) (curpos + yscl) - (int) curpos; + g.fillRect(halfwidth - len / 2, (int) curpos + 32, len, step); curpos += yscl; - }else{ - int len = (int)((double)arrayController.array[i]/(double)arrayController.length*((double)ch-32.0)); - int step = (int)(curpos+xscl) - (int)curpos; - g.fillRect((int)curpos, halfheight-len/2+16, step, len); + } else + { + int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * ((double) ch - 32.0)); + int step = (int) (curpos + xscl) - (int) curpos; + g.fillRect((int) curpos, halfheight - len / 2 + 16, step, len); curpos += xscl; } } } //MESH DRAW METHOD - else if(MESHDRAW){ - - int trih = window.getHeight()/20; //Height of triangles to use, Width will be scaled accordingly - - int tripercol = window.getHeight()/trih*2; //Triangles per vertical column - int triperrow = arrayController.length/tripercol; //Triangles per horizontal row - double triw = (double)window.getWidth()/triperrow; //Width of triangles to use - + else if (MESHDRAW) + { + + int trih = window.getHeight() / 20; //Height of triangles to use, Width will be scaled accordingly + + int tripercol = window.getHeight() / trih * 2; //Triangles per vertical column + int triperrow = arrayController.length / tripercol; //Triangles per horizontal row + double triw = (double) window.getWidth() / triperrow; //Width of triangles to use + double curx = 0; int cury = 0; - + int[] triptsx = new int[3]; int[] triptsy = new int[3]; - - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - - if(arrayController.marked.contains(i)/*||marked.contains(i-1)||marked.contains(i-2)||marked.contains(i-3)*/) + + if (arrayController.marked.contains(i)/*||marked.contains(i-1)||marked.contains(i-2)||marked.contains(i-3)*/) g.setColor(Color.BLACK); else g.setColor(getIntColor(arrayController.array[i], sortErrors)); - + //If i/triperrow is even, then triangle points right, else left boolean direction = false; - if(((i-1)/triperrow)%2==0) + if (((i - 1) / triperrow) % 2 == 0) direction = true; - + //Make the triangle - if(!direction){ + if (!direction) + { //Pointing right - triptsx[0] = (int)curx; - triptsx[1] = (int)curx; - curx+=triw; - triptsx[2] = (int)curx; - - if(!COLORONLY) - triptsx[2] = (int)(triptsx[1]+triw*arrayController.array[i]/arrayController.length); - + triptsx[0] = (int) curx; + triptsx[1] = (int) curx; + curx += triw; + triptsx[2] = (int) curx; + + if (!COLORONLY) + triptsx[2] = (int) (triptsx[1] + triw * arrayController.array[i] / arrayController.length); + triptsy[0] = cury; - triptsy[2] = cury + trih/2; + triptsy[2] = cury + trih / 2; triptsy[1] = cury + trih; - }else{ + } else + { //Pointing left - triptsx[2] = (int)curx; - curx+=triw; - triptsx[0] = (int)curx; - triptsx[1] = (int)curx; - - if(!COLORONLY) - triptsx[2] = (int)(triptsx[0]-triw*arrayController.array[i]/arrayController.length); - + triptsx[2] = (int) curx; + curx += triw; + triptsx[0] = (int) curx; + triptsx[1] = (int) curx; + + if (!COLORONLY) + triptsx[2] = (int) (triptsx[0] - triw * arrayController.array[i] / arrayController.length); + triptsy[0] = cury; - triptsy[2] = cury + trih/2; + triptsy[2] = cury + trih / 2; triptsy[1] = cury + trih; } - + //Draw it g.fillPolygon(triptsx, triptsy, triptsx.length); - + //If at the end of a row, reset curx - if(i != 0 && i%triperrow == 0){ + if (i != 0 && i % triperrow == 0) + { curx = 0d; - cury+=trih/2; + cury += trih / 2; } } - } - else if(CIRCLEDRAW) - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) g.setColor(Color.BLACK); else g.setColor(getIntColor(arrayController.array[i], sortErrors)); - - double sinval = Math.sin(i*Math.PI/circamt); - double cosval = Math.cos(i*Math.PI/circamt); - + + double sinval = Math.sin(i * Math.PI / circamt); + double cosval = Math.cos(i * Math.PI / circamt); + //COLOR ONLY NO LENGTH - if(COLORONLY){ + if (COLORONLY) + { Polygon p = new Polygon(); p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(sinval*(window.getWidth()-64)/2.0), halfheight-(int)(cosval*(window.getHeight()-96)/2.0)); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*(window.getWidth()-64)/2.0), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*(window.getHeight()-96)/2.0)); + p.addPoint(halfwidth + (int) (sinval * (window.getWidth() - 64) / 2.0), halfheight - (int) (cosval * (window.getHeight() - 96) / 2.0)); + p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * (window.getWidth() - 64) / 2.0), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * (window.getHeight() - 96) / 2.0)); g.fillPolygon(p); //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*(window.getWidth()-64)/2.0), halfheight-(int)(cosval*(window.getHeight()-96)/2.0)); } //DISPARITY - else if (DISPARITYDRAW){ - double len = (500d-Math.min(Math.min(Math.abs(i-arrayController.array[i]), Math.abs(i-arrayController.array[i]+1000)), Math.abs(i-arrayController.array[i]-1000)))/500d; - - if(PIXELDRAW){ - int linkedpixX = halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*len)) + dotw/2; - int linkedpixY = halfheight-(int)(cosval*((window.getHeight()-96)/2.0*len)) + doth/2; - - g.fillRect(linkedpixX - dotw/2, linkedpixY - doth/2, dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) + else if (DISPARITYDRAW) + { + double len = (500d - Math.min(Math.min(Math.abs(i - arrayController.array[i]), Math.abs(i - arrayController.array[i] + 1000)), Math.abs(i - arrayController.array[i] - 1000))) / 500d; + + if (PIXELDRAW) + { + int linkedpixX = halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * len)) + dotw / 2; + int linkedpixY = halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * len)) + doth / 2; + + g.fillRect(linkedpixX - dotw / 2, linkedpixY - doth / 2, dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) g.drawLine(linkedpixX, linkedpixY, linkedpixdrawx, linkedpixdrawy); linkedpixdrawx = linkedpixX; linkedpixdrawy = linkedpixY; } - } - else{ + } else + { Polygon p = new Polygon(); p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*len)), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*len))); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*((window.getWidth()-64)/2.0*len)), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*((window.getHeight()-96)/2.0*len))); + p.addPoint(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * len)), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * len))); + p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * len)), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * len))); g.fillPolygon(p); } //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } //PIXELS ONLY - else if(PIXELDRAW){ - g.fillRect(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))), dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) - g.drawLine(halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))), linkedpixdrawx, linkedpixdrawy); - linkedpixdrawx = halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))); - linkedpixdrawy = halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length))); + else if (PIXELDRAW) + { + g.fillRect(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) + g.drawLine(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), linkedpixdrawx, linkedpixdrawy); + linkedpixdrawx = halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); + linkedpixdrawy = halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); } } //LENGTH AND COLOR - else{ + else + { Polygon p = new Polygon(); p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth+(int)(Math.sin((i)*Math.PI/circamt)*((window.getWidth()-64)/2.0*(arrayController.array[i]/(double)arrayController.length))), halfheight-(int)(Math.cos((i)*Math.PI/circamt)*((window.getHeight()-96)/2.0*(arrayController.array[i]/(double)arrayController.length)))); - p.addPoint(halfwidth+(int)(Math.sin((i+1)*Math.PI/circamt)*((window.getWidth()-64)/2.0*(arrayController.array[Math.min(i+1, arrayController.length-1)]/(double)arrayController.length))), halfheight-(int)(Math.cos((i+1)*Math.PI/circamt)*((window.getHeight()-96)/2.0*(arrayController.array[Math.min(i+1, arrayController.length-1)]/(double)arrayController.length)))); + p.addPoint(halfwidth + (int) (Math.sin((i) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (Math.cos((i) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length)))); + p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length))), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length)))); g.fillPolygon(p); //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } } else - for(int i = 0; i < arrayController.length; i++){ - if(i>0 && arrayController.array[i] 0 && arrayController.array[i] < arrayController.array[i - 1]) sortErrors++; - - if(arrayController.marked.contains(i)|| arrayController.marked.contains(i-1)|| arrayController.marked.contains(i-2)|| arrayController.marked.contains(i-3)) + + if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) g.setColor(Color.BLACK); else g.setColor(getIntColor(arrayController.array[i], sortErrors)); - + int y = 0; - int width = (int)(xscl*i)-amt; - - if(width>0){ - if(COLORONLY){ - y = (int)(window.getHeight()-750*yscl); - g.fillRect(amt, y, width, Math.max((int)(750*yscl), 1)); + int width = (int) (xscl * i) - amt; + + if (width > 0) + { + if (COLORONLY) + { + y = (int) (window.getHeight() - 750 * yscl); + g.fillRect(amt, y, width, Math.max((int) (750 * yscl), 1)); g.setColor(getRevColor()); - g.fillRect((int)(i*xscl), y, width, 6); - } - else if(PIXELDRAW){ - y = (int)(window.getHeight()-arrayController.array[i]*yscl); - g.fillRect(amt-dotw/2, y-doth/2, dotw, doth); - if(LINKEDPIXELDRAW){ - if(i>0) + g.fillRect((int) (i * xscl), y, width, 6); + } else if (PIXELDRAW) + { + y = (int) (window.getHeight() - arrayController.array[i] * yscl); + g.fillRect(amt - dotw / 2, y - doth / 2, dotw, doth); + if (LINKEDPIXELDRAW) + { + if (i > 0) g.drawLine(amt, y, linkedpixdrawx, linkedpixdrawy); linkedpixdrawx = amt; linkedpixdrawy = y; } - } - else{ - y = (int)(window.getHeight()-arrayController.array[i]*yscl); - g.fillRect(amt, y, width, Math.max((int)(arrayController.array[i]*yscl), 1)); + } else + { + y = (int) (window.getHeight() - arrayController.array[i] * yscl); + g.fillRect(amt, y, width, Math.max((int) (arrayController.array[i] * yscl), 1)); g.setColor(getRevColor()); //g.fillRect(amt, y, width, 6); FILAMENT } } - amt+=width; + amt += width; } - + int coltmp = 255;//(int)Math.abs(Math.sin(frames*0.01)*255); g.setColor(new Color(coltmp, coltmp, coltmp)); Font f = g.getFont(); g.setFont(fon); - g.drawString(heading, 10, (int)(cw/1280.0*20)+30); - g.drawString(formatNum(arrayController.comps)+" Comparison"+(arrayController.comps==1?"":"s"), 10, (int)(cw/1280.0*40)+30); - g.drawString(formatNum(arrayController.aa)+" Array Access"+(arrayController.aa==1?"":"es"), 10, (int)(cw/1280.0*60)+30); - int sortpercent = (int)((double)(arrayController.length-sortErrors)/(double)arrayController.length*100.0); - g.drawString(String.format("%d%% Sorted (%d Segment%c)", sortpercent, sortErrors+1, sortErrors==0?' ':'s'), 10, (int)(cw/1280.0*80)+30); + g.drawString(heading, 10, (int) (cw / 1280.0 * 20) + 30); + g.drawString(formatNum(arrayController.comps) + " Comparison" + (arrayController.comps == 1 ? "" : "s"), 10, (int) (cw / 1280.0 * 40) + 30); + g.drawString(formatNum(arrayController.aa) + " Array Access" + (arrayController.aa == 1 ? "" : "es"), 10, (int) (cw / 1280.0 * 60) + 30); + int sortpercent = (int) ((double) (arrayController.length - sortErrors) / (double) arrayController.length * 100.0); + g.drawString(String.format("%d%% Sorted (%d Segment%c)", sortpercent, sortErrors + 1, sortErrors == 0 ? ' ' : 's'), 10, (int) (cw / 1280.0 * 80) + 30); //g.drawString(String.format("%d Segment%c", sortErrors+1, sortErrors==0?' ':'s'), 10, (int)(cw/1280.0*100)+30); - double slpt = (double)sleeptime/1000000000.0; - if(System.currentTimeMillis()-lastrtupdate > rtupdatefreq){ - realt = (double)(running?(System.nanoTime()-starttime-sleeptime):(stoptime-starttime-sleeptime))/1000000.0; + double slpt = (double) sleeptime / 1000000000.0; + if (System.currentTimeMillis() - lastrtupdate > rtupdatefreq) + { + realt = (double) (running ? (System.nanoTime() - starttime - sleeptime) : (stoptime - starttime - sleeptime)) / 1000000.0; lastrtupdate = System.currentTimeMillis(); } - g.drawString(String.format("Real Time: %.2fms", realt), 10, (int)(cw/1280.0*100)+30); - g.drawString(String.format("Sleep Time: %.2fs", slpt), 10, (int)(cw/1280.0*120)+30); + g.drawString(String.format("Real Time: %.2fms", realt), 10, (int) (cw / 1280.0 * 100) + 30); + g.drawString(String.format("Sleep Time: %.2fs", slpt), 10, (int) (cw / 1280.0 * 120) + 30); g.setFont(f); Graphics g2 = window.getGraphics(); g2.setColor(Color.BLACK); g2.drawImage(img, 0, 0, null); } } - + //0 = Solid, 1 = Rainbow, 2 = Segments - public Color getIntColor(int i, int segnum) { - if(COLORSTRAT == 1) - return Color.getHSBColor(((float)i/arrayController.length), 1.0F, 0.8F); - else if(COLORSTRAT == 2){ - return COLORSTRAT2cols.get(segnum%COLORSTRAT2cols.size()); + public Color getIntColor(int i, int segnum) + { + if (COLORSTRAT == 1) + return Color.getHSBColor(((float) i / arrayController.length), 1.0F, 0.8F); + else if (COLORSTRAT == 2) + { + return COLORSTRAT2cols.get(segnum % COLORSTRAT2cols.size()); } return COLORSTRAT0col; } - public Color getRevColor(){ - return getIntColor((int)(Math.sin(frames/66.67)*arrayController.length), 0); + + public Color getRevColor() + { + return getIntColor((int) (Math.sin(frames / 66.67) * arrayController.length), 0); } }.start(); - + uf.setVisible(false); v = new ViewPrompt(window); - while(v.isVisible()) Thread.sleep(1); + while (v.isVisible()) Thread.sleep(1); uf.setVisible(true); - + //keep on keeping on - while(window.isActive())Thread.sleep(100); + while (window.isActive()) Thread.sleep(100); } - - public static void refresharray() throws Exception { + + public static void refresharray() throws Exception + { clearmarked(); - if(running){ + if (running) + { stoptime = System.nanoTime(); running = false; Thread.sleep(1000); } - + Thread.sleep(1000); boolean solved = true; - for(int i = 0; i < arrayController.length; i++){ - if(arrayController.array[i]!=i) + for (int i = 0; i < arrayController.length; i++) + { + if (arrayController.array[i] != i) solved = false; arrayController.marked.set(0, i); } - for(int i = 0; i < arrayController.length; i++) + for (int i = 0; i < arrayController.length; i++) arrayController.array[i] = i; //System.out.println(solved); arrayController.marked.set(0, -5); @@ -596,51 +653,63 @@ public static void refresharray() throws Exception { sleeptime = 0; running = true; } - - public static int getDigit(int a, int power, int radix){ + + public static int getDigit(int a, int power, int radix) + { return (int) (a / Math.pow(radix, power)) % radix; } - - public static int[] rianr(int [] arr) { + + public static int[] rianr(int[] arr) + { for (int i = 0; i < arr.length; i++) arr[i] = i; shuffle(arr); return arr; } - public static void clearmarked(){ + public static void clearmarked() + { arrayController.clearMarked(); } - public static void shuffle(int[] array) { + public static void shuffle(int[] array) + { String tmp = heading; heading = "Shuffling..."; - for(int i = 0; i < array.length; i++){ - swap(arrayController, i, (int)(Math.random()*array.length)); - arrayController.aa-=2; - if(SHUFFLEANIM) + for (int i = 0; i < array.length; i++) + { + swap(arrayController, i, (int) (Math.random() * array.length)); + arrayController.aa -= 2; + if (SHUFFLEANIM) sleep(1); } heading = tmp; } - public static int sleepTime(double d) { - return (int)(arrayController.length*d)/4; + public static int sleepTime(double d) + { + return (int) (arrayController.length * d) / 4; } - - public synchronized static void RunAllSorts(){ - if(sortingThread != null) - while(sortingThread.isAlive()) try { - Thread.sleep(100); - } catch (InterruptedException ex) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); - } + + public synchronized static void RunAllSorts() + { + if (sortingThread != null) + while (sortingThread.isAlive()) try + { + Thread.sleep(100); + } catch (InterruptedException ex) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } SetSound(true); - sortingThread = new Thread(){ + sortingThread = new Thread() + { @Override - public void run(){ - try{ + public void run() + { + try + { for (Sort sort : new Sort[]{ new SelectionSort(), new BubbleSort(), @@ -659,14 +728,15 @@ public void run(){ new RadixMSD(4), new RadixLSDInPlace(2), new RadixLSDInPlace(10)} - ) + ) { chan.allNotesOff(); refresharray(); heading = sort.name(); sort.sort(arrayController); } - }catch (Exception ex){ + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } SetSound(false); @@ -678,52 +748,68 @@ public void run(){ }; sortingThread.start(); } - - public static void ReportComparativeSort(int n){ - if(sortingThread != null && sortingThread.isAlive()) + + public static void ReportComparativeSort(int n) + { + if (sortingThread != null && sortingThread.isAlive()) return; - + final int num = n; SetSound(true); - sortingThread = new Thread(){ + sortingThread = new Thread() + { @Override - public void run(){ - try{ + public void run() + { + try + { refresharray(); Sort sort; switch (num) { case 0: - sort = new SelectionSort();break; + sort = new SelectionSort(); + break; case 1: - sort = new BubbleSort();break; + sort = new BubbleSort(); + break; case 2: - sort = new InsertionSort();break; + sort = new InsertionSort(); + break; case 3: - sort = new DoubleSelection();break; + sort = new DoubleSelection(); + break; case 4: - sort = new CocktailShaker();break; + sort = new CocktailShaker(); + break; case 5: - sort = new QuickSort();break; + sort = new QuickSort(); + break; case 6: - sort = new MergeSort();break; + sort = new MergeSort(); + break; case 7: - sort = new MergeSortOOP();break; + sort = new MergeSortOOP(); + break; case 8: - sort = new WeaveMerge();break; + sort = new WeaveMerge(); + break; case 9: - sort = new MaxHeapSort();break; + sort = new MaxHeapSort(); + break; case 10: - sort = new ShellSort();break; + sort = new ShellSort(); + break; default: - sort = null; break; + sort = null; + break; } if (sort != null) { heading = sort.name(); sort.sort(arrayController); } - }catch(Exception ex) + } catch (Exception ex) { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } @@ -734,78 +820,102 @@ public void run(){ }; sortingThread.start(); } - - public static void ReportDistributiveSort(int n){ - if(sortingThread != null && sortingThread.isAlive()) + + public static void ReportDistributiveSort(int n) + { + if (sortingThread != null && sortingThread.isAlive()) return; int bas = 10; - if(n != 3 && n != 5 && n != 7) - if(n != 4) - try{bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort"));}catch(Exception ex){ + if (n != 3 && n != 5 && n != 7) + if (n != 4) + try + { + bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort")); + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } else - try{bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions"));}catch(Exception ex){ + try + { + bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions")); + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } - + final int base = Math.max(bas, 2); final int num = n; SetSound(true); - sortingThread = new Thread(){ + sortingThread = new Thread() + { @Override - public void run(){ - try{ - refresharray(); - Sort sort; - switch (num) { - case 0: - sort = new RadixLSD(base);break; - case 1: - sort = new RadixMSD(base);break; - case 2: - sort = new RadixLSDInPlace(base);break; - case 3: - sort = new GravitySort();break; - case 4: - sort = new ShatterSorts(base);break; - case 5: - sort = new CountingSort();break; - case 6: - sort = new TimeSort(base);break; - case 7: - sort = new BogoSort();break; - default: - sort = null; break; - } - if (sort != null) + public void run() { - heading = sort.name(); - sort.sort(arrayController); - } - }catch(Exception ex){ - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); - } - SetSound(false); - stoptime = System.nanoTime(); - running = false; + try + { + refresharray(); + Sort sort; + switch (num) + { + case 0: + sort = new RadixLSD(base); + break; + case 1: + sort = new RadixMSD(base); + break; + case 2: + sort = new RadixLSDInPlace(base); + break; + case 3: + sort = new GravitySort(); + break; + case 4: + sort = new ShatterSorts(base); + break; + case 5: + sort = new CountingSort(); + break; + case 6: + sort = new TimeSort(base); + break; + case 7: + sort = new BogoSort(); + break; + default: + sort = null; + break; + } + if (sort != null) + { + heading = sort.name(); + sort.sort(arrayController); + } + } catch (Exception ex) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } + SetSound(false); + stoptime = System.nanoTime(); + running = false; } }; sortingThread.start(); } - - public static String formatNum(long a){ - if(a<0) + + public static String formatNum(long a) + { + if (a < 0) return "OVERFLOW"; - if(a>1000L*1000L*1000L*1000L) - return String.format("%.2fT", (double)a/1000000000000.0); - if(a>1000L*1000L*1000L) - return String.format("%.2fB", (double)a/1000000000.0); - if(a>1000L*1000L) - return String.format("%.2fM", (double)a/1000000.0); - else if(a>1000L) - return String.format("%.2fK", (double)a/1000.0); + if (a > 1000L * 1000L * 1000L * 1000L) + return String.format("%.2fT", (double) a / 1000000000000.0); + if (a > 1000L * 1000L * 1000L) + return String.format("%.2fB", (double) a / 1000000000.0); + if (a > 1000L * 1000L) + return String.format("%.2fM", (double) a / 1000000.0); + else if (a > 1000L) + return String.format("%.2fK", (double) a / 1000.0); else - return ""+a; + return "" + a; } } diff --git a/src/array/visualizer/SortPrompt.java b/src/array/visualizer/SortPrompt.java index 5dd395f0..1f5559a6 100644 --- a/src/array/visualizer/SortPrompt.java +++ b/src/array/visualizer/SortPrompt.java @@ -5,32 +5,37 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import javax.swing.JFrame; /** - * * @author S630690 */ -public class SortPrompt extends javax.swing.JFrame implements AAFrame{ +public class SortPrompt extends javax.swing.JFrame implements AAFrame +{ JFrame f; + /** * Creates new form SortPrompt */ - public SortPrompt(JFrame f) { + public SortPrompt(JFrame f) + { this.f = f; setAlwaysOnTop(true); setUndecorated(true); initComponents(); jList2.setListData(ComparativeSorts); jList1.setListData(DistributiveSorts); - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); } - public void reposition(){ - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + + public void reposition() + { + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); } - + /** * This method is called from within the constructor to initialize the form. @@ -39,7 +44,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); @@ -57,40 +63,66 @@ private void initComponents() { jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jList2.setModel(new javax.swing.AbstractListModel() { - String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; - public int getSize() { return strings.length; } - public Object getElementAt(int i) { return strings[i]; } + jList2.setModel(new javax.swing.AbstractListModel() + { + String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; + + public int getSize() + { + return strings.length; + } + + public Object getElementAt(int i) + { + return strings[i]; + } }); - jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() { - public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() + { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) + { jList2ValueChanged(evt); } }); jScrollPane1.setViewportView(jList2); jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jScrollPane2.addMouseListener(new java.awt.event.MouseAdapter() { - public void mouseClicked(java.awt.event.MouseEvent evt) { + jScrollPane2.addMouseListener(new java.awt.event.MouseAdapter() + { + public void mouseClicked(java.awt.event.MouseEvent evt) + { jScrollPane2MouseClicked(evt); } }); - jList1.setModel(new javax.swing.AbstractListModel() { - String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; - public int getSize() { return strings.length; } - public Object getElementAt(int i) { return strings[i]; } + jList1.setModel(new javax.swing.AbstractListModel() + { + String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; + + public int getSize() + { + return strings.length; + } + + public Object getElementAt(int i) + { + return strings[i]; + } }); - jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() { - public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() + { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) + { jList1ValueChanged(evt); } }); jScrollPane2.setViewportView(jList1); jButton1.setText("Run All"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); @@ -98,71 +130,81 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(23, 23, 23) - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jLabel2) - .addGap(32, 32, 32)) - .addGroup(layout.createSequentialGroup() - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1) - .addGap(82, 82, 82)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(23, 23, 23) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel2) + .addGap(32, 32, 32)) + .addGroup(layout.createSequentialGroup() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1) + .addGap(82, 82, 82)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel1) - .addComponent(jLabel2)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) - .addComponent(jScrollPane1)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1) - .addGap(0, 0, 0)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jLabel2)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) + .addComponent(jScrollPane1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1) + .addGap(0, 0, 0)) ); pack(); }// //GEN-END:initComponents - private void jScrollPane2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane2MouseClicked + private void jScrollPane2MouseClicked(java.awt.event.MouseEvent evt) + {//GEN-FIRST:event_jScrollPane2MouseClicked // TODO add your handling code here: }//GEN-LAST:event_jScrollPane2MouseClicked - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: - new Thread(){ - public void run(){ + new Thread() + { + public void run() + { RunAllSorts(); } }.start(); dispose(); }//GEN-LAST:event_jButton1ActionPerformed - private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged + private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) + {//GEN-FIRST:event_jList1ValueChanged // TODO add your handling code here: final int tmp = evt.getFirstIndex(); - new Thread(){ - public void run(){ + new Thread() + { + public void run() + { ReportDistributiveSort(tmp); } }.start(); dispose(); }//GEN-LAST:event_jList1ValueChanged - private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged + private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) + {//GEN-FIRST:event_jList2ValueChanged // TODO add your handling code here: final int tmp = evt.getFirstIndex(); - new Thread(){ - public void run(){ + new Thread() + { + public void run() + { ReportComparativeSort(tmp); } }.start(); diff --git a/src/array/visualizer/UtilFrame.java b/src/array/visualizer/UtilFrame.java index 641f8408..5ccce51f 100644 --- a/src/array/visualizer/UtilFrame.java +++ b/src/array/visualizer/UtilFrame.java @@ -5,6 +5,7 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import java.awt.Toolkit; import java.util.logging.Level; import java.util.logging.Logger; @@ -12,29 +13,32 @@ import javax.swing.JOptionPane; /** - * * @author S630690 */ -public class UtilFrame extends javax.swing.JFrame { +public class UtilFrame extends javax.swing.JFrame +{ JFrame f; AAFrame v; + /** * Creates new form UtilFrame */ - public UtilFrame(JFrame f) { + public UtilFrame(JFrame f) + { this.f = f; setUndecorated(true); initComponents(); - setLocation(f.getX()+f.getWidth(), f.getY()+29); + setLocation(f.getX() + f.getWidth(), f.getY() + 29); setAlwaysOnTop(true); setVisible(true); } - - public void reposition(){ + + public void reposition() + { toFront(); - setLocation(Math.min((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-getWidth(), f.getX()+f.getWidth()), f.getY()+29); - if(v!= null && v.isVisible()) + setLocation(Math.min((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - getWidth(), f.getX() + f.getWidth()), f.getY() + 29); + if (v != null && v.isVisible()) v.reposition(); } @@ -45,7 +49,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jCheckBox1 = new javax.swing.JCheckBox(); jButton1 = new javax.swing.JButton(); @@ -59,38 +64,48 @@ private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); jButton1.setText("Change Sort"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); jButton2.setText("Change View"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton2ActionPerformed(evt); } }); jButton3.setText("Change Spd"); - jButton3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton3ActionPerformed(evt); } }); jCheckBox2.setSelected(true); jCheckBox2.setText("Shuffle Anim"); - jCheckBox2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jCheckBox2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jCheckBox2ActionPerformed(evt); } }); jCheckBox3.setSelected(true); jCheckBox3.setText("Linked Dots"); - jCheckBox3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jCheckBox3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jCheckBox3ActionPerformed(evt); } }); @@ -98,73 +113,83 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jCheckBox3) - .addComponent(jCheckBox2) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) - .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addGap(0, 8, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jCheckBox3) + .addComponent(jCheckBox2) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addGap(0, 8, Short.MAX_VALUE)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jButton1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton3) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jCheckBox2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jCheckBox3) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jButton1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton3) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jCheckBox2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jCheckBox3) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: //CHANGE SORT - if(v != null && v.isVisible()){ - boolean tmp = v instanceof SortPrompt; - v.dispose(); - if(tmp) - return; + if (v != null && v.isVisible()) + { + boolean tmp = v instanceof SortPrompt; + v.dispose(); + if (tmp) + return; } v = new SortPrompt(f); }//GEN-LAST:event_jButton1ActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: //CHANGE VIEW - if(v != null && v.isVisible()){ - boolean tmp = v instanceof ViewPrompt; - v.dispose(); - if(tmp) - return; + if (v != null && v.isVisible()) + { + boolean tmp = v instanceof ViewPrompt; + v.dispose(); + if (tmp) + return; } v = new ViewPrompt(f); }//GEN-LAST:event_jButton2ActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: - try{ - SLEEPRATIO = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Speed Multiplier", SLEEPRATIO)); - }catch(Exception e) {} + try + { + SLEEPRATIO = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Speed Multiplier", SLEEPRATIO)); + } catch (Exception e) + { + } }//GEN-LAST:event_jButton3ActionPerformed - private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed + private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jCheckBox2ActionPerformed SHUFFLEANIM = jCheckBox2.isSelected(); }//GEN-LAST:event_jCheckBox2ActionPerformed - private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3ActionPerformed + private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jCheckBox3ActionPerformed LINKEDPIXELDRAW = jCheckBox3.isSelected(); }//GEN-LAST:event_jCheckBox3ActionPerformed diff --git a/src/array/visualizer/ViewPrompt.java b/src/array/visualizer/ViewPrompt.java index 80d42617..063ad3bb 100644 --- a/src/array/visualizer/ViewPrompt.java +++ b/src/array/visualizer/ViewPrompt.java @@ -5,29 +5,33 @@ package array.visualizer; import static array.visualizer.ArrayVisualizer.*; + import javax.swing.JFrame; /** - * * @author S630690 */ -public class ViewPrompt extends javax.swing.JFrame implements AAFrame { +public class ViewPrompt extends javax.swing.JFrame implements AAFrame +{ JFrame f; + /** * Creates new form ViewPrompt */ - public ViewPrompt(JFrame f) { + public ViewPrompt(JFrame f) + { this.f = f; setAlwaysOnTop(true); setUndecorated(true); initComponents(); - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); } - - public void reposition(){ - setLocation(f.getX()+(f.getWidth()-getWidth())/2, f.getY()+(f.getHeight()-getHeight())/2); + + public void reposition() + { + setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); } /** @@ -37,7 +41,8 @@ public void reposition(){ */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents - private void initComponents() { + private void initComponents() + { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); @@ -61,43 +66,55 @@ private void initComponents() { jButton1.setText("Classic"); jButton1.setMaximumSize(new java.awt.Dimension(87, 23)); jButton1.setMinimumSize(new java.awt.Dimension(87, 23)); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton1ActionPerformed(evt); } }); jButton2.setText("Color Circle"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton2ActionPerformed(evt); } }); jButton3.setText("Spiral"); - jButton3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton3ActionPerformed(evt); } }); jButton4.setText("Color Rect"); - jButton4.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton4.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton4ActionPerformed(evt); } }); jButton5.setText("Dots"); - jButton5.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton5.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton5ActionPerformed(evt); } }); jButton6.setText("Spiral Dots"); - jButton6.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton6.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton6ActionPerformed(evt); } }); @@ -105,57 +122,73 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText("Select View Type"); jButton7.setText("D. Circle"); - jButton7.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton7.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton7ActionPerformed(evt); } }); jButton8.setText("D. C. Dots"); - jButton8.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton8.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton8ActionPerformed(evt); } }); jButton9.setText("TriMesh"); - jButton9.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton9.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton9ActionPerformed(evt); } }); jButton10.setText("VW Tris"); - jButton10.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton10.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton10ActionPerformed(evt); } }); jButton11.setText("V. Pyr"); - jButton11.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton11.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton11ActionPerformed(evt); } }); jButton12.setText("H. Pyr"); - jButton12.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton12.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton12ActionPerformed(evt); } }); jButton13.setText("Hoops"); - jButton13.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton13.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton13ActionPerformed(evt); } }); jButton14.setText("D. Hoops"); - jButton14.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton14.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { jButton14ActionPerformed(evt); } }); @@ -163,75 +196,76 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(18, 18, 18) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) - .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(0, 0, Short.MAX_VALUE) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) - .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addContainerGap()) - .addGroup(layout.createSequentialGroup() - .addGap(65, 65, 65) - .addComponent(jLabel1) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(0, 0, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE) + .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap()) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(jLabel1) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(7, 7, 7) - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton4)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton2) - .addComponent(jButton5)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jButton6) - .addComponent(jButton3)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton7) - .addComponent(jButton8)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton9) - .addComponent(jButton10)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton11) - .addComponent(jButton12)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton13) - .addComponent(jButton14)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(7, 7, 7) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton4)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton2) + .addComponent(jButton5)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jButton6) + .addComponent(jButton3)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton7) + .addComponent(jButton8)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton9) + .addComponent(jButton10)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton11) + .addComponent(jButton12)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton13) + .addComponent(jButton14)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents - private void setAllFieldsFalse(){ + private void setAllFieldsFalse() + { CIRCLEDRAW = false; COLORONLY = false; PIXELDRAW = false; @@ -240,14 +274,16 @@ private void setAllFieldsFalse(){ PYRAMIDDRAW = false; DRAWFLIPPED = false; } - - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); dispose(); }//GEN-LAST:event_jButton1ActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -255,28 +291,32 @@ private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton2ActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; dispose(); }//GEN-LAST:event_jButton3ActionPerformed - private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed + private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton4ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); COLORONLY = true; dispose(); }//GEN-LAST:event_jButton4ActionPerformed - private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed + private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton5ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); PIXELDRAW = true; dispose(); }//GEN-LAST:event_jButton5ActionPerformed - private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed + private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton6ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -284,7 +324,8 @@ private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton6ActionPerformed - private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed + private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton7ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -292,7 +333,8 @@ private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton7ActionPerformed - private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed + private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton8ActionPerformed // TODO add your handling code here: setAllFieldsFalse(); CIRCLEDRAW = true; @@ -301,33 +343,38 @@ private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS dispose(); }//GEN-LAST:event_jButton8ActionPerformed - private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed + private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton9ActionPerformed setAllFieldsFalse(); MESHDRAW = true; COLORONLY = true; dispose(); }//GEN-LAST:event_jButton9ActionPerformed - private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed + private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton10ActionPerformed setAllFieldsFalse(); MESHDRAW = true; dispose(); }//GEN-LAST:event_jButton10ActionPerformed - private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed + private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton11ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; dispose(); }//GEN-LAST:event_jButton11ActionPerformed - private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed + private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton12ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; DRAWFLIPPED = true; dispose(); }//GEN-LAST:event_jButton12ActionPerformed - private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed + private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton13ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; CIRCLEDRAW = true; @@ -336,7 +383,8 @@ private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR dispose(); }//GEN-LAST:event_jButton13ActionPerformed - private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed + private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) + {//GEN-FIRST:event_jButton14ActionPerformed setAllFieldsFalse(); PYRAMIDDRAW = true; CIRCLEDRAW = true; diff --git a/src/array/visualizer/sort/BitonicSort.java b/src/array/visualizer/sort/BitonicSort.java index 578826b3..9c6ab85a 100644 --- a/src/array/visualizer/sort/BitonicSort.java +++ b/src/array/visualizer/sort/BitonicSort.java @@ -7,27 +7,28 @@ import array.visualizer.ArrayController; /** - * * @author S630690 */ public class BitonicSort implements Sort { - public static void bitonicSort(final ArrayController ac){ + public static void bitonicSort(final ArrayController ac) + { bitonicMerge(ac, 0, ac.length, true); } - - public static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir){ - int mid = (start+end)/2; - if(start==mid) + + public static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir) + { + int mid = (start + end) / 2; + if (start == mid) return; bitonicMerge(ac, start, mid, true); bitonicMerge(ac, mid, end, false); - + int low = start; int high = end; - - if(dir) - for(int i = 0; i < end-start; i++); + + if (dir) + for (int i = 0; i < end - start; i++) ; } @Override diff --git a/src/array/visualizer/sort/BogoSort.java b/src/array/visualizer/sort/BogoSort.java index 96145292..e0b95cd4 100644 --- a/src/array/visualizer/sort/BogoSort.java +++ b/src/array/visualizer/sort/BogoSort.java @@ -5,31 +5,38 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class BogoSort implements Sort { - public static boolean bogoIsSorted(final ArrayController ac){ - for(int i = 1; i < ac.length; i++){ +public class BogoSort implements Sort +{ + public static boolean bogoIsSorted(final ArrayController ac) + { + for (int i = 1; i < ac.length; i++) + { ac.comps++; ac.aa++; ac.marked.set(1, i); - ac.marked.set(2, i-1); + ac.marked.set(2, i - 1); sleep(1); - if(ac.array[i] 0; i--){ - for(int j = 0; j < i; j++){ +public class BubbleSort implements Sort +{ + public static void bubbleSort(final ArrayController ac) + { + for (int i = ac.length - 1; i > 0; i--) + { + for (int j = 0; j < i; j++) + { sleep(0.005); - if(ac.array[j]>ac.array[j+1]){ + if (ac.array[j] > ac.array[j + 1]) + { ac.comps++; - swap(ac, j, j+1, 0.01); - }else{ - ac.marked.set(1, j+1); + swap(ac, j, j + 1, 0.01); + } else + { + ac.marked.set(1, j + 1); ac.marked.set(2, -5); } } diff --git a/src/array/visualizer/sort/CocktailShaker.java b/src/array/visualizer/sort/CocktailShaker.java index ac311d92..4f26d5e9 100644 --- a/src/array/visualizer/sort/CocktailShaker.java +++ b/src/array/visualizer/sort/CocktailShaker.java @@ -9,22 +9,26 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class CocktailShaker implements Sort { - public static void cocktailShakerSort(final ArrayController ac){ +public class CocktailShaker implements Sort +{ + public static void cocktailShakerSort(final ArrayController ac) + { int i = 0; - while(i< ac.length/2){ - for(int j = i; j < ac.length-i-1; j++){ + while (i < ac.length / 2) + { + for (int j = i; j < ac.length - i - 1; j++) + { ac.comps++; - if(ac.array[j]>ac.array[j+1]) - swap(ac, j, j+1, 0.022); + if (ac.array[j] > ac.array[j + 1]) + swap(ac, j, j + 1, 0.022); } - for(int j = ac.length-i-1; j > i; j--){ + for (int j = ac.length - i - 1; j > i; j--) + { ac.comps++; - if(ac.array[j]ac.array[biggest]) + if (ac.array[i] > ac.array[biggest]) biggest = i; - if(ac.array[i]0&&ac.array[pos]<=ac.array[pos-1]){ - ac.comps+=2; - swap(ac, pos, pos-1, 0.02); + while (pos > 0 && ac.array[pos] <= ac.array[pos - 1]) + { + ac.comps += 2; + swap(ac, pos, pos - 1, 0.02); pos--; } } } - - public static void insertionSort(final ArrayController ac, int start, int end, double slpamt) { + + public static void insertionSort(final ArrayController ac, int start, int end, double slpamt) + { int pos; - for(int i = start; i < end; i++){ + for (int i = start; i < end; i++) + { pos = i; ac.marked.set(1, i); ac.marked.set(2, -5); - while(pos>start&&ac.array[pos]<=ac.array[pos-1]){ - ac.comps+=2; - swap(ac, pos, pos-1); - sleep(slpamt); - pos--; + while (pos > start && ac.array[pos] <= ac.array[pos - 1]) + { + ac.comps += 2; + swap(ac, pos, pos - 1); + sleep(slpamt); + pos--; } } } diff --git a/src/array/visualizer/sort/MaxHeapSort.java b/src/array/visualizer/sort/MaxHeapSort.java index 899d4510..36bd0e85 100644 --- a/src/array/visualizer/sort/MaxHeapSort.java +++ b/src/array/visualizer/sort/MaxHeapSort.java @@ -4,62 +4,70 @@ import static array.visualizer.utils.Swaps.*; -public class MaxHeapSort implements Sort { - +public class MaxHeapSort implements Sort +{ + static int SLP = 1; - - static void maxheapifyrec(final ArrayController ac, int pos, boolean max){ - if(pos>= ac.length) + + static void maxheapifyrec(final ArrayController ac, int pos, boolean max) + { + if (pos >= ac.length) return; - - int child1 = pos*2+1; - int child2 = pos*2+2; - + + int child1 = pos * 2 + 1; + int child2 = pos * 2 + 2; + maxheapifyrec(ac, child1, max); maxheapifyrec(ac, child2, max); - - if(child2>= ac.length){ - if(child1>= ac.length) + + if (child2 >= ac.length) + { + if (child1 >= ac.length) return; //Done, no children ac.comps++; - if(ac.array[child1]>ac.array[pos]) + if (ac.array[child1] > ac.array[pos]) swap(ac, pos, child1, SLP); return; } - + //Find largest child int lrg = child1; ac.comps++; - if(ac.array[child2]>ac.array[child1]) + if (ac.array[child2] > ac.array[child1]) lrg = child2; - + //Swap with largest child ac.comps++; - if(ac.array[lrg]>ac.array[pos]){ + if (ac.array[lrg] > ac.array[pos]) + { swap(ac, pos, lrg, SLP); percdwn(ac, lrg, true, ac.length); return; } } - - static void percdwn(final ArrayController ac, int pos, boolean max, int len){ - int child1 = pos*2+1; - int child2 = pos*2+2; - - if(child2 >= len){ - if(child1 >= len) //Done + + static void percdwn(final ArrayController ac, int pos, boolean max, int len) + { + int child1 = pos * 2 + 1; + int child2 = pos * 2 + 2; + + if (child2 >= len) + { + if (child1 >= len) //Done return; - else{ + else + { //Single Child ac.comps++; - if((max && (ac.array[child1]>ac.array[pos])) || (!max && (ac.array[child1] ac.array[pos])) || (!max && (ac.array[child1] < ac.array[pos]))) swap(ac, pos, child1, SLP); return; } } ac.comps++; - if(ac.array[child1]>ac.array[child2]){ + if (ac.array[child1] > ac.array[child2]) + { //Ensure child1 is the smallest for easy programming int tmp = child1; child1 = child2; @@ -67,19 +75,22 @@ static void percdwn(final ArrayController ac, int pos, boolean max, int len){ } ac.comps++; - if(max && (ac.array[child2]>ac.array[pos])){ + if (max && (ac.array[child2] > ac.array[pos])) + { swap(ac, pos, child2, SLP); percdwn(ac, child2, max, len); - } - else if (!max && (ac.array[child1] 0; i--){ + for (int i = ac.length - 1; i > 0; i--) + { swap(ac, 0, i, SLP); percdwn(ac, 0, true, i); } diff --git a/src/array/visualizer/sort/MergeSort.java b/src/array/visualizer/sort/MergeSort.java index 794a5488..82f7a109 100644 --- a/src/array/visualizer/sort/MergeSort.java +++ b/src/array/visualizer/sort/MergeSort.java @@ -8,59 +8,70 @@ import array.visualizer.ArrayVisualizer; import static array.visualizer.utils.Swaps.*; + import java.util.logging.Level; import java.util.logging.Logger; /** - * * @author S630690 */ -public class MergeSort implements Sort { - static void merge(final ArrayController ac, int min, int max, int mid){ - try { +public class MergeSort implements Sort +{ + static void merge(final ArrayController ac, int min, int max, int mid) + { + try + { //radixLSDsortnd(2, min, max); - - - int i=min; - while(i<=mid){ - if(ac.array[i]>ac.array[mid+1]){ - ac.comps++; - swap(ac, i, mid+1, 1.5); - push(ac, mid+1, max); - } - i++; - } - - } catch (Exception ex) { + + + int i = min; + while (i <= mid) + { + if (ac.array[i] > ac.array[mid + 1]) + { + ac.comps++; + swap(ac, i, mid + 1, 1.5); + push(ac, mid + 1, max); + } + i++; + } + + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } - } - - static void push(final ArrayController ac, int s, int e){ - - for(int i=s;iac.array[i+1]){ + } + + static void push(final ArrayController ac, int s, int e) + { + + for (int i = s; i < e; i++) + { + if (ac.array[i] > ac.array[i + 1]) + { ac.comps++; - swap(ac, i, i+1, 0.0175); - } - } - - - } - - public static void mergeSort(final ArrayController ac, int min, int max){ - if(max-min==0){//only one element. - //no swap - } - else if(max-min==1){//only two elements and swaps them - if(ac.array[min]>ac.array[max]) + swap(ac, i, i + 1, 0.0175); + } + } + + + } + + public static void mergeSort(final ArrayController ac, int min, int max) + { + if (max - min == 0) + {//only one element. + //no swap + } else if (max - min == 1) + {//only two elements and swaps them + if (ac.array[min] > ac.array[max]) swap(ac, min, max); - } - else{ - int mid=((int) Math.floor((min+max)/2));//The midpoint + } else + { + int mid = ((int) Math.floor((min + max) / 2));//The midpoint mergeSort(ac, min, mid);//sort the left side - mergeSort(ac, mid+1, max);//sort the right side + mergeSort(ac, mid + 1, max);//sort the right side merge(ac, min, max, mid);//combines them } } diff --git a/src/array/visualizer/sort/MergeSortOOP.java b/src/array/visualizer/sort/MergeSortOOP.java index 6c814c8a..dee909f3 100644 --- a/src/array/visualizer/sort/MergeSortOOP.java +++ b/src/array/visualizer/sort/MergeSortOOP.java @@ -9,63 +9,68 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class MergeSortOOP implements Sort { - public static void mergeSortOP(final ArrayController ac) { +public class MergeSortOOP implements Sort +{ + public static void mergeSortOP(final ArrayController ac) + { int start = 0; int end = ac.length; - int mid = (end+start)/2; + int mid = (end + start) / 2; mergeOP(ac, start, mid, end); } - public static void mergeOP(final ArrayController ac, int start, int mid, int end){ - if(start==mid) + public static void mergeOP(final ArrayController ac, int start, int mid, int end) + { + if (start == mid) return; - mergeOP(ac, start, (mid+start)/2, mid); - mergeOP(ac, mid, (mid+end)/2, end); - - int[] tmp = new int[end-start]; - + mergeOP(ac, start, (mid + start) / 2, mid); + mergeOP(ac, mid, (mid + end) / 2, end); + + int[] tmp = new int[end - start]; + int low = start; int high = mid; - for(int nxt = 0; nxt < tmp.length; nxt++){ - if(low >= mid && high >= end) + for (int nxt = 0; nxt < tmp.length; nxt++) + { + if (low >= mid && high >= end) break; - if(low < mid && high >= end){ - tmp[nxt]=ac.array[low]; + if (low < mid && high >= end) + { + tmp[nxt] = ac.array[low]; low++; - ac.comps+=2; - } - else if(low >= mid && high < end){ - tmp[nxt]=ac.array[high]; + ac.comps += 2; + } else if (low >= mid && high < end) + { + tmp[nxt] = ac.array[high]; high++; - ac.comps+=2; - } - else if(ac.array[low]=array.length/10) - sleep(0.65); + sleep(0.65); } //System.arraycopy(tmp, 0, array, start, tmp.length); ac.marked.set(2, -5); - for(int i = 0; i < tmp.length; i++){ - ac.array[start+i]=tmp[i]; + for (int i = 0; i < tmp.length; i++) + { + ac.array[start + i] = tmp[i]; ac.aa++; - ac.marked.set(1, start+i); - if(end-start>= ac.length/100) + ac.marked.set(1, start + i); + if (end - start >= ac.length / 100) sleep(0.5); } } diff --git a/src/array/visualizer/sort/MinHeapSort.java b/src/array/visualizer/sort/MinHeapSort.java index 522c2710..5b85dfcd 100644 --- a/src/array/visualizer/sort/MinHeapSort.java +++ b/src/array/visualizer/sort/MinHeapSort.java @@ -4,8 +4,9 @@ import static array.visualizer.utils.Swaps.swap; -public class MinHeapSort implements Sort { - +public class MinHeapSort implements Sort +{ + static void minheapsort(ArrayController ac) { } diff --git a/src/array/visualizer/sort/QuickSort.java b/src/array/visualizer/sort/QuickSort.java index b52ce4f1..618de75c 100644 --- a/src/array/visualizer/sort/QuickSort.java +++ b/src/array/visualizer/sort/QuickSort.java @@ -10,42 +10,46 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class QuickSort implements Sort { +public class QuickSort implements Sort +{ public static void quickSort(final ArrayController ac, int p, int r) { - if(pp && ac.array[j] > x){ + while (j > p && ac.array[j] > x) + { j--; ac.marked.set(2, j); sleep(0.45); - ac.comps+=2; + ac.comps += 2; } if (i < j) @@ -64,6 +68,6 @@ public String name() @Override public void sort(ArrayController ac) { - quickSort(ac, 0, ac.length-1); + quickSort(ac, 0, ac.length - 1); } } diff --git a/src/array/visualizer/sort/RadixLSD.java b/src/array/visualizer/sort/RadixLSD.java index c554b8b2..9ebe5043 100644 --- a/src/array/visualizer/sort/RadixLSD.java +++ b/src/array/visualizer/sort/RadixLSD.java @@ -9,13 +9,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Analysis.*; import static array.visualizer.utils.Transcriptions.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class RadixLSD implements Sort { +public class RadixLSD implements Sort +{ private final int radix; @@ -24,14 +25,17 @@ public RadixLSD(int radix) this.radix = radix; } - public static void radixLSDsort_(final ArrayController ac, int radix) { + public static void radixLSDsort_(final ArrayController ac, int radix) + { clearmarked(); int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) + for (int i = 0; i < radix; i++) registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = 0; i < ac.length; i++){ + for (int p = 0; p <= highestpower; p++) + { + for (int i = 0; i < ac.length; i++) + { ac.marked.set(1, i); sleep(1); registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); @@ -40,33 +44,39 @@ public static void radixLSDsort_(final ArrayController ac, int radix) { transcribe(registers, ac); } } - - public static void radixLSDsort(final ArrayController ac, int radix){ + + public static void radixLSDsort(final ArrayController ac, int radix) + { clearmarked(); int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) + for (int i = 0; i < radix; i++) registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = 0; i < ac.length; i++){ + for (int p = 0; p <= highestpower; p++) + { + for (int i = 0; i < ac.length; i++) + { ac.aa++; ac.marked.set(1, i); - if(i%2==0) + if (i % 2 == 0) sleep(1); registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } fancyTranscribe(ac, registers); } } - - public static void radixLSDsortnd(final ArrayController ac, int radix, int min, int max) { + + public static void radixLSDsortnd(final ArrayController ac, int radix, int min, int max) + { clearmarked(); int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) + for (int i = 0; i < radix; i++) registers[i] = new ArrayList(); - for(int p = 0; p <= highestpower; p++){ - for(int i = min; i < max; i++){ + for (int p = 0; p <= highestpower; p++) + { + for (int i = min; i < max; i++) + { registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } //transcribe(registers, array); diff --git a/src/array/visualizer/sort/RadixLSDInPlace.java b/src/array/visualizer/sort/RadixLSDInPlace.java index 0ccf72e9..dffd795c 100644 --- a/src/array/visualizer/sort/RadixLSDInPlace.java +++ b/src/array/visualizer/sort/RadixLSDInPlace.java @@ -6,7 +6,8 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class RadixLSDInPlace implements Sort { +public class RadixLSDInPlace implements Sort +{ private final int radix; public RadixLSDInPlace(int radix) @@ -14,29 +15,34 @@ public RadixLSDInPlace(int radix) this.radix = radix; } - public static void inPlaceRadixLSDSort(final ArrayController ac, int radix){ + public static void inPlaceRadixLSDSort(final ArrayController ac, int radix) + { int pos = 0; - int[] vregs = new int[radix-1]; + int[] vregs = new int[radix - 1]; int maxpower = analyze(ac, radix); double smul = Math.sqrt(radix); - for(int p = 0; p <= maxpower; p++){ - for(int i = 0; i < vregs.length; i++) - vregs[i]= ac.length-1; + for (int p = 0; p <= maxpower; p++) + { + for (int i = 0; i < vregs.length; i++) + vregs[i] = ac.length - 1; pos = 0; - for(int i = 0; i < ac.length; i++){ + for (int i = 0; i < ac.length; i++) + { int digit = getDigit(ac.array[pos], p, radix); - if(digit==0) { + if (digit == 0) + { pos++; ac.marked.set(0, pos); - } else { - for(int j = 0; j 0; j--) - vregs[j-1]--; + } else + { + for (int j = 0; j < vregs.length; j++) + ac.marked.set(j + 1, vregs[j]); + swapUpToNM(ac, pos, vregs[digit - 1], 0.0011 * smul); + for (int j = digit - 1; j > 0; j--) + vregs[j - 1]--; } } - + } } diff --git a/src/array/visualizer/sort/RadixMSD.java b/src/array/visualizer/sort/RadixMSD.java index 0542569f..556f1e20 100644 --- a/src/array/visualizer/sort/RadixMSD.java +++ b/src/array/visualizer/sort/RadixMSD.java @@ -9,13 +9,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Transcriptions.*; import static array.visualizer.utils.Analysis.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class RadixMSD implements Sort { +public class RadixMSD implements Sort +{ private final int radix; public RadixMSD(int radix) @@ -23,33 +24,37 @@ public RadixMSD(int radix) this.radix = radix; } - public static void radixMSDSort(final ArrayController ac, int radix) { + public static void radixMSDSort(final ArrayController ac, int radix) + { clearmarked(); int highestpower = analyze(ac, radix); int[] tmp = new int[ac.length]; System.arraycopy(ac.array, 0, tmp, 0, ac.length); radixMSDRec(ac, 0, ac.length, radix, highestpower); } - - public static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow){ - if(min >= max || pow < 0) + + public static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow) + { + if (min >= max || pow < 0) return; ac.marked.set(2, max); ac.marked.set(3, min); ArrayList[] registers = new ArrayList[radix]; - for(int i = 0; i < radix; i++) + for (int i = 0; i < radix; i++) registers[i] = new ArrayList(); - for(int i = min; i < max; i++){ + for (int i = min; i < max; i++) + { ac.marked.set(1, i); registers[getDigit(ac.array[i], pow, radix)].add(ac.array[i]); ac.aa++; } transcribermsd(ac, registers, min); - + int sum = 0; - for(int i = 0; i < registers.length; i++){ - radixMSDRec(ac, sum+min, sum+min+registers[i].size(), radix, pow-1); - sum+=registers[i].size(); + for (int i = 0; i < registers.length; i++) + { + radixMSDRec(ac, sum + min, sum + min + registers[i].size(), radix, pow - 1); + sum += registers[i].size(); registers[i].clear(); } } diff --git a/src/array/visualizer/sort/SelectionSort.java b/src/array/visualizer/sort/SelectionSort.java index 74610794..a7c66808 100644 --- a/src/array/visualizer/sort/SelectionSort.java +++ b/src/array/visualizer/sort/SelectionSort.java @@ -10,16 +10,20 @@ import static array.visualizer.utils.Swaps.*; /** - * * @author S630690 */ -public class SelectionSort implements Sort { - - public static void selectionSort(final ArrayController ac) { - for (int i = 0; i < ac.length - 1; i++) { +public class SelectionSort implements Sort +{ + + public static void selectionSort(final ArrayController ac) + { + for (int i = 0; i < ac.length - 1; i++) + { int lowestindex = i; - for (int j = i + 1; j < ac.length; j++) { - if (ac.array[j] < ac.array[lowestindex]){ + for (int j = i + 1; j < ac.length; j++) + { + if (ac.array[j] < ac.array[lowestindex]) + { lowestindex = j; } sleep(0.01); diff --git a/src/array/visualizer/sort/ShatterSorts.java b/src/array/visualizer/sort/ShatterSorts.java index 80962613..4d710d4b 100644 --- a/src/array/visualizer/sort/ShatterSorts.java +++ b/src/array/visualizer/sort/ShatterSorts.java @@ -8,13 +8,14 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Transcriptions.*; + import java.util.ArrayList; /** - * * @author S630690 */ -public class ShatterSorts implements Sort { +public class ShatterSorts implements Sort +{ private final int base; public ShatterSorts(int base) @@ -22,44 +23,51 @@ public ShatterSorts(int base) this.base = base; } - public static void shatterPartition(final ArrayController ac, int num) { - int shatters = (int)Math.ceil(ac.length/(double)num); + public static void shatterPartition(final ArrayController ac, int num) + { + int shatters = (int) Math.ceil(ac.length / (double) num); ArrayList[] registers = new ArrayList[shatters]; - for(int i = 0; i < shatters; i++) + for (int i = 0; i < shatters; i++) registers[i] = new ArrayList(); - for(int i = 0; i < ac.length; i++){ - registers[ac.array[i]/num].add(ac.array[i]); + for (int i = 0; i < ac.length; i++) + { + registers[ac.array[i] / num].add(ac.array[i]); ac.aa++; } transcribe(registers, ac); } - - public static void shatterSort(final ArrayController ac, int num) { - int shatters = (int)Math.ceil(ac.length/(double)num); + + public static void shatterSort(final ArrayController ac, int num) + { + int shatters = (int) Math.ceil(ac.length / (double) num); shatterPartition(ac, num); int[] tmp = new int[num]; - for(int i = 0; i < shatters; i++){ - for(int j = 0; j < num; j++){ - if(i*num+j>= ac.length) + for (int i = 0; i < shatters; i++) + { + for (int j = 0; j < num; j++) + { + if (i * num + j >= ac.length) tmp[j] = -1; else - tmp[j]=ac.array[i*num+j]; + tmp[j] = ac.array[i * num + j]; ac.aa++; } - for(int j = 0; j < tmp.length; j++){ + for (int j = 0; j < tmp.length; j++) + { int tmpj = tmp[j]; - if(i*num+(tmpj%num)>= ac.length || tmpj == -1) + if (i * num + (tmpj % num) >= ac.length || tmpj == -1) break; - ac.array[i*num+(tmpj%num)]=tmpj; + ac.array[i * num + (tmpj % num)] = tmpj; ac.aa++; - ac.marked.set(1, i*num+(tmpj%num)); + ac.marked.set(1, i * num + (tmpj % num)); sleep(sleepTime(0.02)); } } } - - public static void simpleShatterSort(final ArrayController ac, int num, int rate) { - for(int i = num; i > 1; i = i/rate) + + public static void simpleShatterSort(final ArrayController ac, int num, int rate) + { + for (int i = num; i > 1; i = i / rate) shatterPartition(ac, i); shatterPartition(ac, 1); } diff --git a/src/array/visualizer/sort/ShellSort.java b/src/array/visualizer/sort/ShellSort.java index 8c91544a..9784a454 100644 --- a/src/array/visualizer/sort/ShellSort.java +++ b/src/array/visualizer/sort/ShellSort.java @@ -5,33 +5,41 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.utils.Swaps.*; -public class ShellSort implements Sort { - public static void shellSort(final ArrayController ac, int gap, int divrate){ +public class ShellSort implements Sort +{ + public static void shellSort(final ArrayController ac, int gap, int divrate) + { double sleepamt = 1d; - while(gap>0){ - for(int j = 0; j <= gap-1; j++){ - for(int i = j+gap; i < ac.length; i+=gap){ + while (gap > 0) + { + for (int j = 0; j <= gap - 1; j++) + { + for (int i = j + gap; i < ac.length; i += gap) + { int pos = i; - int prev = pos-gap; - while(prev>=0){ - if(ac.array[pos] < ac.array[prev]){ + int prev = pos - gap; + while (prev >= 0) + { + if (ac.array[pos] < ac.array[prev]) + { ac.comps++; swap(ac, pos, prev); sleep(sleepamt); - }else{ - ac.aa+=2; + } else + { + ac.aa += 2; break; } pos = prev; - prev = pos-gap; + prev = pos - gap; } } } - - if(gap==1) //Done + + if (gap == 1) //Done break; - - gap = Math.max(gap/divrate, 1); //Ensure that we do gap 1 + + gap = Math.max(gap / divrate, 1); //Ensure that we do gap 1 //sleepamt /= divrate; } } diff --git a/src/array/visualizer/sort/Sort.java b/src/array/visualizer/sort/Sort.java index dc0ff2e5..e03c9009 100644 --- a/src/array/visualizer/sort/Sort.java +++ b/src/array/visualizer/sort/Sort.java @@ -5,5 +5,6 @@ public interface Sort { String name(); + void sort(ArrayController ac); } diff --git a/src/array/visualizer/sort/TimeSort.java b/src/array/visualizer/sort/TimeSort.java index 89820eb0..59db45e4 100644 --- a/src/array/visualizer/sort/TimeSort.java +++ b/src/array/visualizer/sort/TimeSort.java @@ -10,14 +10,16 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.sort.InsertionSort.*; + import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; + /** - * * @author S630690 */ -public class TimeSort implements Sort { +public class TimeSort implements Sort +{ private final int magnitude; public TimeSort(int magnitude) @@ -31,30 +33,38 @@ public static void timeSort(final ArrayController ac, int magnitude) next = 0; ArrayList threads = new ArrayList(); final int[] tmp = ac.array.clone(); - for(int i = 0; i < ac.length; i++){ + for (int i = 0; i < ac.length; i++) + { ac.marked.set(0, i); sleep(1); final int c = i; - threads.add(new Thread(){ - public void run() { + threads.add(new Thread() + { + public void run() + { int a = tmp[c]; - try { - sleep(a*A); - } catch (InterruptedException ex) { + try + { + sleep(a * A); + } catch (InterruptedException ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } report(ac, a); } }); } - for(Thread t : threads) + for (Thread t : threads) t.start(); sleep(ac.length * A); insertionSort(ac, 0, ac.length, 0.2d); - + } + static volatile int next = 0; - public static synchronized void report(final ArrayController ac, int a){ + + public static synchronized void report(final ArrayController ac, int a) + { ac.marked.set(0, next); ac.array[next] = a; ac.aa++; diff --git a/src/array/visualizer/sort/WeaveMerge.java b/src/array/visualizer/sort/WeaveMerge.java index ddf69e27..e51db6b0 100644 --- a/src/array/visualizer/sort/WeaveMerge.java +++ b/src/array/visualizer/sort/WeaveMerge.java @@ -10,57 +10,67 @@ import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.sort.InsertionSort.*; import static array.visualizer.utils.Swaps.*; + import java.util.logging.Level; import java.util.logging.Logger; /** - * * @author S630690 */ -public class WeaveMerge implements Sort { - static void weaveMerge(final ArrayController ac, int min, int max, int mid){ - try { +public class WeaveMerge implements Sort +{ + static void weaveMerge(final ArrayController ac, int min, int max, int mid) + { + try + { //radixLSDsortnd(2, min, max); - int i=1; - int target = (mid-min); - while(i<=target){ - //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); - swapUpTo(ac, mid+i, min+i*2-1, 0.01); - i++; - sleep(1); + int i = 1; + int target = (mid - min); + while (i <= target) + { + //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); + swapUpTo(ac, mid + i, min + i * 2 - 1, 0.01); + i++; + sleep(1); } - insertionSort(ac, min, max+1, 0.15); + insertionSort(ac, min, max + 1, 0.15); //sleep(100); - - } catch (Exception ex) { + + } catch (Exception ex) + { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } } - - static void push(final ArrayController ac, int s, int e){ - - for(int i=s;iac.array[i+1]){ + + static void push(final ArrayController ac, int s, int e) + { + + for (int i = s; i < e; i++) + { + if (ac.array[i] > ac.array[i + 1]) + { ac.comps++; - swap(ac, i, i+1); + swap(ac, i, i + 1); } - } + } } - - public static void weaveMergeSort(final ArrayController ac, int min, int max){ - if(max-min==0){//only one element. - //no swap - } - else if(max-min==1){//only two elements and swaps them - if(ac.array[min]>ac.array[max]) + + public static void weaveMergeSort(final ArrayController ac, int min, int max) + { + if (max - min == 0) + {//only one element. + //no swap + } else if (max - min == 1) + {//only two elements and swaps them + if (ac.array[min] > ac.array[max]) swap(ac, min, max); - } - else{ - int mid=((int) Math.floor((min+max)/2));//The midpoint + } else + { + int mid = ((int) Math.floor((min + max) / 2));//The midpoint weaveMergeSort(ac, min, mid);//sort the left side - weaveMergeSort(ac, mid+1, max);//sort the right side + weaveMergeSort(ac, mid + 1, max);//sort the right side weaveMerge(ac, min, max, mid);//combines them } } diff --git a/src/array/visualizer/utils/Analysis.java b/src/array/visualizer/utils/Analysis.java index fc3b1544..8b98bb50 100644 --- a/src/array/visualizer/utils/Analysis.java +++ b/src/array/visualizer/utils/Analysis.java @@ -9,37 +9,44 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class Analysis { - public static int analyze(final ArrayController ac, int base) { +public class Analysis +{ + public static int analyze(final ArrayController ac, int base) + { int a = 0; - for(int i = 0; i < ac.length; i++){ + for (int i = 0; i < ac.length; i++) + { ac.marked.set(1, i); ac.aa++; sleep(1); - if((int)(Math.log(ac.array[i])/Math.log(base))>a){ - a=(int)(Math.log(ac.array[i])/Math.log(base)); + if ((int) (Math.log(ac.array[i]) / Math.log(base)) > a) + { + a = (int) (Math.log(ac.array[i]) / Math.log(base)); } } return a; } - public static int analyze_sneaky(int[] array, int base) { + public static int analyze_sneaky(int[] array, int base) + { int a = 0; - for(int i = 0; i < array.length; i++) - if((int)(Math.log(array[i])/Math.log(base))>a){ - a=(int)(Math.log(array[i])/Math.log(base)); + for (int i = 0; i < array.length; i++) + if ((int) (Math.log(array[i]) / Math.log(base)) > a) + { + a = (int) (Math.log(array[i]) / Math.log(base)); } return a; } - - public static int analyzemax(final ArrayController ac) { + + public static int analyzemax(final ArrayController ac) + { int a = 0; - for(int i = 0; i < ac.length; i++){ - if(ac.array[i]>a) - a=ac.array[i]; + for (int i = 0; i < ac.length; i++) + { + if (ac.array[i] > a) + a = ac.array[i]; ac.marked.set(1, i); ac.aa++; sleep(1.0); diff --git a/src/array/visualizer/utils/Searches.java b/src/array/visualizer/utils/Searches.java index e1432eac..f2c7a8e4 100644 --- a/src/array/visualizer/utils/Searches.java +++ b/src/array/visualizer/utils/Searches.java @@ -9,31 +9,35 @@ import static array.visualizer.ArrayVisualizer.*; /** - * * @author S630690 */ -public class Searches { - public static void linearSearch(final ArrayController ac, int find) throws Exception { - for(int i = 0; i < ac.length; i++){ +public class Searches +{ + public static void linearSearch(final ArrayController ac, int find) throws Exception + { + for (int i = 0; i < ac.length; i++) + { ac.aa++; ac.comps++; - if(ac.array[i]==find) + if (ac.array[i] == find) break; ac.marked.set(0, i); sleep(5); } sleep(1000); } - - public static void binarySearch(final ArrayController ac, int find) throws Exception { - int at = ac.length/2; - int change = ac.length/4; - while(ac.array[at]!=find && change > 0){ + + public static void binarySearch(final ArrayController ac, int find) throws Exception + { + int at = ac.length / 2; + int change = ac.length / 4; + while (ac.array[at] != find && change > 0) + { ac.marked.set(0, ac.array[at]); - ac.comps+=2; + ac.comps += 2; ac.aa++; Thread.sleep(1000); - if(ac.array[at] 0) - for(int i = pos; i < to; i++) - swap(ac, i, i+1, pause); + + public static void swapUpTo(final ArrayController ac, int pos, int to, double pause) + { + if (to - pos > 0) + for (int i = pos; i < to; i++) + swap(ac, i, i + 1, pause); else - for(int i = pos; i > to; i--) - swap(ac, i, i-1, pause); + for (int i = pos; i > to; i--) + swap(ac, i, i - 1, pause); } - - public static void swapUpToNM(final ArrayController ac, int pos, int to, double pause){ - if(to - pos > 0) - for(int i = pos; i < to; i++) - swapnm(ac, i, i+1, pause); + + public static void swapUpToNM(final ArrayController ac, int pos, int to, double pause) + { + if (to - pos > 0) + for (int i = pos; i < to; i++) + swapnm(ac, i, i + 1, pause); else - for(int i = pos; i > to; i--) - swapnm(ac, i, i-1, pause); + for (int i = pos; i > to; i--) + swapnm(ac, i, i - 1, pause); } - - public static void swapUp(final ArrayController ac, int pos, double pause) { - for(int i = pos; i < ac.length; i++) - swap(ac, i, i+1, pause); + + public static void swapUp(final ArrayController ac, int pos, double pause) + { + for (int i = pos; i < ac.length; i++) + swap(ac, i, i + 1, pause); } } diff --git a/src/array/visualizer/utils/Transcriptions.java b/src/array/visualizer/utils/Transcriptions.java index 834befba..9c84db0e 100644 --- a/src/array/visualizer/utils/Transcriptions.java +++ b/src/array/visualizer/utils/Transcriptions.java @@ -11,14 +11,17 @@ import java.util.ArrayList; /** - * * @author S630690 */ -public class Transcriptions { - public static void transcribe(ArrayList[] registers, final ArrayController ac) { +public class Transcriptions +{ + public static void transcribe(ArrayList[] registers, final ArrayController ac) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ + for (int ai = 0; ai < registers.length; ai++) + { + for (int i = 0; i < registers[ai].size(); i++) + { sleep(1); ac.array[total] = registers[ai].get(i); ac.marked.set(1, total); @@ -28,52 +31,64 @@ public static void transcribe(ArrayList[] registers, final ArrayControl registers[ai].clear(); } } - - public static void transcribenm(ArrayList[] registers, int[] array) { + + public static void transcribenm(ArrayList[] registers, int[] array) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ + for (int ai = 0; ai < registers.length; ai++) + { + for (int i = 0; i < registers[ai].size(); i++) + { array[total] = registers[ai].get(i); total++; } registers[ai].clear(); } } - - public static void transcribend(final ArrayController ac, ArrayList[] registers, int min) { + + public static void transcribend(final ArrayController ac, ArrayList[] registers, int min) + { int total = 0; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ - sleep((min+i)%5/4); - ac.array[total+min] = registers[ai].get(i); - ac.marked.set(1, total+min); + for (int ai = 0; ai < registers.length; ai++) + { + for (int i = 0; i < registers[ai].size(); i++) + { + sleep((min + i) % 5 / 4); + ac.array[total + min] = registers[ai].get(i); + ac.marked.set(1, total + min); total++; ac.aa++; } registers[ai].clear(); } } - - public static void transcribermsd(final ArrayController ac, ArrayList[] registers, int min) { + + public static void transcribermsd(final ArrayController ac, ArrayList[] registers, int min) + { int total = 0; - for(ArrayList ai : registers) - total+=ai.size(); + for (ArrayList ai : registers) + total += ai.size(); int tmp = 0; - for(int ai = registers.length-1; ai >= 0; ai--){ - for(int i = registers[ai].size()-1; i >= 0; i--){ - sleep(1+2/registers[ai].size()); - ac.array[total+min-tmp-1] = registers[ai].get(i); - ac.marked.set(1, total+min-tmp-1); + for (int ai = registers.length - 1; ai >= 0; ai--) + { + for (int i = registers[ai].size() - 1; i >= 0; i--) + { + sleep(1 + 2 / registers[ai].size()); + ac.array[total + min - tmp - 1] = registers[ai].get(i); + ac.marked.set(1, total + min - tmp - 1); tmp++; ac.aa++; } } } - - public static void transcribe(ArrayList[] registers, final ArrayController ac, int start) { + + public static void transcribe(ArrayList[] registers, final ArrayController ac, int start) + { int total = start; - for(int ai = 0; ai < registers.length; ai++){ - for(int i = 0; i < registers[ai].size(); i++){ + for (int ai = 0; ai < registers.length; ai++) + { + for (int i = 0; i < registers[ai].size(); i++) + { sleep(1); ac.array[total] = registers[ai].get(i); ac.marked.set(1, total); @@ -84,26 +99,30 @@ public static void transcribe(ArrayList[] registers, final ArrayControl } } - public static void fancyTranscribe(final ArrayController ac, ArrayList[] registers) { + public static void fancyTranscribe(final ArrayController ac, ArrayList[] registers) + { int[] tmp = new int[ac.length]; boolean[] tmpwrite = new boolean[ac.length]; int radix = registers.length; transcribenm(registers, tmp); - for(int i = 0; i < tmp.length; i++){ - int register = i%radix; - if(register == 0) + for (int i = 0; i < tmp.length; i++) + { + int register = i % radix; + if (register == 0) sleep(radix);//radix - int pos = (int)(((double)register*((double)tmp.length/radix))+((double)i/radix)); - if(tmpwrite[pos]==false){ - ac.array[pos]=tmp[pos]; + int pos = (int) (((double) register * ((double) tmp.length / radix)) + ((double) i / radix)); + if (tmpwrite[pos] == false) + { + ac.array[pos] = tmp[pos]; ac.aa++; tmpwrite[pos] = true; } ac.marked.set(register, pos); } - for(int i = 0; i < tmpwrite.length; i++) - if(tmpwrite[i]==false){ - ac.array[i]=tmp[i]; + for (int i = 0; i < tmpwrite.length; i++) + if (tmpwrite[i] == false) + { + ac.array[i] = tmp[i]; ac.aa++; } clearmarked(); From 14e7d7245a373b3f5ba2d758ab85e71f0bd97f7f Mon Sep 17 00:00:00 2001 From: Zoey Llewellyn Hewll Date: Mon, 7 Jan 2019 21:01:28 +0800 Subject: [PATCH 2/3] Automated cleanup * Remove redundant statements, declarations, type parameters, casts, etc * Add braces to all control flow statements * Mark methods private where possible * Simplify boolean expressions (eg `b == true`) * Replace `for(;;)` with `for(:)` where possible --- src/array/visualizer/AAFrame.java | 6 +- src/array/visualizer/ArrayController.java | 2 + src/array/visualizer/ArrayVisualizer.java | 167 +++++++++++++++--- src/array/visualizer/SortPrompt.java | 2 +- src/array/visualizer/UtilFrame.java | 8 +- src/array/visualizer/sort/BitonicSort.java | 8 +- src/array/visualizer/sort/BogoSort.java | 6 +- src/array/visualizer/sort/BubbleSort.java | 2 +- src/array/visualizer/sort/CocktailShaker.java | 6 +- src/array/visualizer/sort/CountingSort.java | 4 +- .../visualizer/sort/DoubleSelection.java | 8 +- src/array/visualizer/sort/GravitySort.java | 8 +- src/array/visualizer/sort/InsertionSort.java | 2 +- src/array/visualizer/sort/MaxHeapSort.java | 22 ++- src/array/visualizer/sort/MergeSort.java | 10 +- src/array/visualizer/sort/MergeSortOOP.java | 10 +- src/array/visualizer/sort/MinHeapSort.java | 4 +- src/array/visualizer/sort/QuickSort.java | 9 +- src/array/visualizer/sort/RadixLSD.java | 16 +- .../visualizer/sort/RadixLSDInPlace.java | 8 +- src/array/visualizer/sort/RadixMSD.java | 18 +- src/array/visualizer/sort/SelectionSort.java | 2 +- src/array/visualizer/sort/ShatterSorts.java | 20 ++- src/array/visualizer/sort/ShellSort.java | 4 +- src/array/visualizer/sort/TimeSort.java | 10 +- src/array/visualizer/sort/WeaveMerge.java | 8 +- src/array/visualizer/utils/Analysis.java | 10 +- src/array/visualizer/utils/Searches.java | 9 +- src/array/visualizer/utils/Swaps.java | 20 ++- .../visualizer/utils/Transcriptions.java | 42 +++-- 30 files changed, 337 insertions(+), 114 deletions(-) diff --git a/src/array/visualizer/AAFrame.java b/src/array/visualizer/AAFrame.java index 7b013fec..0b5ceda4 100644 --- a/src/array/visualizer/AAFrame.java +++ b/src/array/visualizer/AAFrame.java @@ -9,9 +9,9 @@ */ interface AAFrame { - abstract void reposition(); + void reposition(); - abstract boolean isVisible(); + boolean isVisible(); - abstract void dispose(); + void dispose(); } diff --git a/src/array/visualizer/ArrayController.java b/src/array/visualizer/ArrayController.java index dae7344d..855cc948 100644 --- a/src/array/visualizer/ArrayController.java +++ b/src/array/visualizer/ArrayController.java @@ -22,7 +22,9 @@ public ArrayController(int length) public void clearMarked() { for (int i = 0; i < length; i++) + { marked.set(i, -5); + } } public int get(int index) diff --git a/src/array/visualizer/ArrayVisualizer.java b/src/array/visualizer/ArrayVisualizer.java index 2f774553..7106d3b5 100644 --- a/src/array/visualizer/ArrayVisualizer.java +++ b/src/array/visualizer/ArrayVisualizer.java @@ -65,7 +65,7 @@ public class ArrayVisualizer static int COLORSTRAT = 1; //0 = Solid, 1 = Rainbow, 2 = Segments static Color COLORSTRAT0col = new Color(0, 204, 0); - static ArrayList COLORSTRAT2cols = new ArrayList(); + static ArrayList COLORSTRAT2cols = new ArrayList<>(); static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); @@ -77,8 +77,12 @@ public static double calcVel() { double count = 1; for (int i : arrayController.marked) + { if (i != -5) + { count += 0.75; + } + } return count; } @@ -92,12 +96,16 @@ public static synchronized void SetSound(boolean val) public static void sleep(double milis) { if (milis <= 0) + { return; + } double tmp = (milis * (1000.0 / arrayController.length)); tmp = tmp * (1 / SLEEPRATIO); addamt += tmp; if (addamt < 1.0) + { return; + } try { long actual = System.nanoTime(); @@ -105,7 +113,9 @@ public static void sleep(double milis) actual = (System.nanoTime() - actual); addamt -= (double) actual / 1000000.0; if (running) + { sleeptime += actual; + } } catch (Exception ex) { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); @@ -130,18 +140,24 @@ public static void main(String[] args) throws Exception // } chan = synth.getChannels()[0]; for (Instrument i : synth.getLoadedInstruments()) + { if (i.getName().toLowerCase().trim().contains("sine")) { chan.programChange(i.getPatch().getProgram()); break; } + } if (chan.getProgram() == 0) + { JOptionPane.showMessageDialog(null, "Could not find a valid instrument. Sound is disabled"); + } //chan.programChange(synth.getLoadedInstruments()[197].getPatch().getProgram()); for (int i = 0; i < arrayController.length; i++) + { arrayController.marked.add(-5); + } rianr(arrayController.array); window.setSize(640, 480); @@ -161,16 +177,21 @@ public void run() while (true) { chan.allNotesOff(); - if (SOUND == false) + if (!SOUND) { continue; } int tmp = 1; for (int i : arrayController.marked) + { if (i != -5) + { tmp++; + } + } for (int i : arrayController.marked) + { if (i != -5) { int pitch = (int) Math.round((double) arrayController.array[Math.min(Math.max(i, 0), arrayController.length - 1)] / arrayController.length * 96 + 16); @@ -180,6 +201,7 @@ public void run() int vel = (int) (64.0 / (Math.pow((double) tmp, 0.6)) * ((128 - pitch) / 64.0 + 1.0) * 0.67 * SOUNDMUL); //one day chan.noteOn(pitch, vel); } + } //((int)((127-(array[Math.min(Math.max(i, 0), array.length-1)]/16.0))/Math.sqrt(calcVel())))*5 //double tmp = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47); //chan.setPitchBend(8192*2-(int)((tmp-Math.floor(tmp))*8192*2)); @@ -228,7 +250,9 @@ public void run() { uf.reposition(); if (v != null && v.isVisible()) + { v.reposition(); + } cx = window.getX(); cy = window.getY(); } @@ -279,18 +303,25 @@ public void run() { //Check for sort errors in array if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } int j = arrayController.array[i]; if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else if (j < ornamentend) + } else if (j < ornamentend) + { g.setColor(new Color(255, 0, 0)); - else if (j > trunkstart) + } else if (j > trunkstart) + { g.setColor(new Color(102, 34, 0)); - else + } else + { g.setColor(new Color(0, 102, 0)); + } if (j < ornamentend) { //ORNAMENT @@ -299,7 +330,9 @@ else if (j > trunkstart) { //TRUNK width = (int) (cw * 0.2); } else //TREE + { width = (int) ((((j - ornamentend) % 85) * 0.005 + 0.075) * cw); + } int step = (int) (i * yscl + yscl) - (int) (i * yscl); g.fillRect(halfwidth - width / 2, (int) (i * yscl) + 32, width, step); @@ -312,32 +345,37 @@ else if (PYRAMIDDRAW && CIRCLEDRAW) g.setStroke(new BasicStroke(1.0f)); //significantly increased performance - double maxdiam = (double) Math.min(cw, ch - 32); - double diameter = maxdiam; + double diameter = (double) Math.min(cw, ch - 32); double diamstep = Math.min(xscl, yscl); for (int i = arrayController.length - 1; i >= 0; i--) { //Check for sort errors in array if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } //Set Proper Color if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); + } int radius = (int) (diameter / 2.0); if (!DISPARITYDRAW) + { g.drawOval(halfwidth - radius, halfheight - radius + 15, (int) diameter, (int) diameter); - else + } else { int dist = Math.abs(i - arrayController.array[i]); double disparity = 1.0 - ((double) Math.min(dist, arrayController.length - dist) / ((double) arrayController.length / 2.0)); int actualdiam = (int) (disparity * diameter); - g.drawOval(halfwidth - actualdiam / 2, halfheight - radius + 15, (int) actualdiam, (int) actualdiam); + g.drawOval(halfwidth - actualdiam / 2, halfheight - radius + 15, actualdiam, actualdiam); } diameter -= diamstep; } @@ -354,13 +392,18 @@ else if (PYRAMIDDRAW) { //Check for sort errors in array if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } //Set Proper Color if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); + } //Draw if (!DRAWFLIPPED) @@ -397,17 +440,24 @@ else if (MESHDRAW) for (int i = 0; i < arrayController.length; i++) { if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } if (arrayController.marked.contains(i)/*||marked.contains(i-1)||marked.contains(i-2)||marked.contains(i-3)*/) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); + } //If i/triperrow is even, then triangle points right, else left boolean direction = false; if (((i - 1) / triperrow) % 2 == 0) + { direction = true; + } //Make the triangle if (!direction) @@ -419,7 +469,9 @@ else if (MESHDRAW) triptsx[2] = (int) curx; if (!COLORONLY) + { triptsx[2] = (int) (triptsx[1] + triw * arrayController.array[i] / arrayController.length); + } triptsy[0] = cury; triptsy[2] = cury + trih / 2; @@ -433,7 +485,9 @@ else if (MESHDRAW) triptsx[1] = (int) curx; if (!COLORONLY) + { triptsx[2] = (int) (triptsx[0] - triw * arrayController.array[i] / arrayController.length); + } triptsy[0] = cury; triptsy[2] = cury + trih / 2; @@ -451,15 +505,21 @@ else if (MESHDRAW) } } } else if (CIRCLEDRAW) + { for (int i = 0; i < arrayController.length; i++) { if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); + } double sinval = Math.sin(i * Math.PI / circamt); double cosval = Math.cos(i * Math.PI / circamt); @@ -488,7 +548,9 @@ else if (DISPARITYDRAW) if (LINKEDPIXELDRAW) { if (i > 0) + { g.drawLine(linkedpixX, linkedpixY, linkedpixdrawx, linkedpixdrawy); + } linkedpixdrawx = linkedpixX; linkedpixdrawy = linkedpixY; } @@ -509,7 +571,9 @@ else if (PIXELDRAW) if (LINKEDPIXELDRAW) { if (i > 0) + { g.drawLine(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), linkedpixdrawx, linkedpixdrawy); + } linkedpixdrawx = halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); linkedpixdrawy = halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); } @@ -525,16 +589,22 @@ else if (PIXELDRAW) //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } } - else + } else + { for (int i = 0; i < arrayController.length; i++) { if (i > 0 && arrayController.array[i] < arrayController.array[i - 1]) + { sortErrors++; + } if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) + { g.setColor(Color.BLACK); - else + } else + { g.setColor(getIntColor(arrayController.array[i], sortErrors)); + } int y = 0; int width = (int) (xscl * i) - amt; @@ -554,7 +624,9 @@ else if (PIXELDRAW) if (LINKEDPIXELDRAW) { if (i > 0) + { g.drawLine(amt, y, linkedpixdrawx, linkedpixdrawy); + } linkedpixdrawx = amt; linkedpixdrawy = y; } @@ -568,6 +640,7 @@ else if (PIXELDRAW) } amt += width; } + } int coltmp = 255;//(int)Math.abs(Math.sin(frames*0.01)*255); g.setColor(new Color(coltmp, coltmp, coltmp)); @@ -598,8 +671,9 @@ else if (PIXELDRAW) public Color getIntColor(int i, int segnum) { if (COLORSTRAT == 1) + { return Color.getHSBColor(((float) i / arrayController.length), 1.0F, 0.8F); - else if (COLORSTRAT == 2) + } else if (COLORSTRAT == 2) { return COLORSTRAT2cols.get(segnum % COLORSTRAT2cols.size()); } @@ -614,11 +688,17 @@ public Color getRevColor() uf.setVisible(false); v = new ViewPrompt(window); - while (v.isVisible()) Thread.sleep(1); + while (v.isVisible()) + { + Thread.sleep(1); + } uf.setVisible(true); //keep on keeping on - while (window.isActive()) Thread.sleep(100); + while (window.isActive()) + { + Thread.sleep(100); + } } public static void refresharray() throws Exception @@ -636,11 +716,15 @@ public static void refresharray() throws Exception for (int i = 0; i < arrayController.length; i++) { if (arrayController.array[i] != i) + { solved = false; + } arrayController.marked.set(0, i); } for (int i = 0; i < arrayController.length; i++) + { arrayController.array[i] = i; + } //System.out.println(solved); arrayController.marked.set(0, -5); heading = ""; @@ -659,12 +743,13 @@ public static int getDigit(int a, int power, int radix) return (int) (a / Math.pow(radix, power)) % radix; } - public static int[] rianr(int[] arr) + public static void rianr(int[] arr) { for (int i = 0; i < arr.length; i++) + { arr[i] = i; + } shuffle(arr); - return arr; } public static void clearmarked() @@ -681,7 +766,9 @@ public static void shuffle(int[] array) swap(arrayController, i, (int) (Math.random() * array.length)); arrayController.aa -= 2; if (SHUFFLEANIM) + { sleep(1); + } } heading = tmp; } @@ -694,13 +781,18 @@ public static int sleepTime(double d) public synchronized static void RunAllSorts() { if (sortingThread != null) - while (sortingThread.isAlive()) try - { - Thread.sleep(100); - } catch (InterruptedException ex) + { + while (sortingThread.isAlive()) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + try + { + Thread.sleep(100); + } catch (InterruptedException ex) + { + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + } } + } SetSound(true); sortingThread = new Thread() @@ -752,7 +844,9 @@ public void run() public static void ReportComparativeSort(int n) { if (sortingThread != null && sortingThread.isAlive()) + { return; + } final int num = n; SetSound(true); @@ -824,10 +918,14 @@ public void run() public static void ReportDistributiveSort(int n) { if (sortingThread != null && sortingThread.isAlive()) + { return; + } int bas = 10; if (n != 3 && n != 5 && n != 7) + { if (n != 4) + { try { bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort")); @@ -835,7 +933,8 @@ public static void ReportDistributiveSort(int n) { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } - else + } else + { try { bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions")); @@ -843,6 +942,8 @@ public static void ReportDistributiveSort(int n) { Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } + } + } final int base = Math.max(bas, 2); final int num = n; @@ -906,16 +1007,26 @@ public void run() public static String formatNum(long a) { if (a < 0) + { return "OVERFLOW"; + } if (a > 1000L * 1000L * 1000L * 1000L) + { return String.format("%.2fT", (double) a / 1000000000000.0); + } if (a > 1000L * 1000L * 1000L) + { return String.format("%.2fB", (double) a / 1000000000.0); + } if (a > 1000L * 1000L) + { return String.format("%.2fM", (double) a / 1000000.0); - else if (a > 1000L) + } else if (a > 1000L) + { return String.format("%.2fK", (double) a / 1000.0); - else + } else + { return "" + a; + } } } diff --git a/src/array/visualizer/SortPrompt.java b/src/array/visualizer/SortPrompt.java index 1f5559a6..10ff69f7 100644 --- a/src/array/visualizer/SortPrompt.java +++ b/src/array/visualizer/SortPrompt.java @@ -14,7 +14,7 @@ public class SortPrompt extends javax.swing.JFrame implements AAFrame { - JFrame f; + private JFrame f; /** * Creates new form SortPrompt diff --git a/src/array/visualizer/UtilFrame.java b/src/array/visualizer/UtilFrame.java index 5ccce51f..5a86d323 100644 --- a/src/array/visualizer/UtilFrame.java +++ b/src/array/visualizer/UtilFrame.java @@ -7,8 +7,6 @@ import static array.visualizer.ArrayVisualizer.*; import java.awt.Toolkit; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; @@ -39,7 +37,9 @@ public void reposition() toFront(); setLocation(Math.min((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - getWidth(), f.getX() + f.getWidth()), f.getY() + 29); if (v != null && v.isVisible()) + { v.reposition(); + } } /** @@ -153,7 +153,9 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) boolean tmp = v instanceof SortPrompt; v.dispose(); if (tmp) + { return; + } } v = new SortPrompt(f); }//GEN-LAST:event_jButton1ActionPerformed @@ -167,7 +169,9 @@ private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) boolean tmp = v instanceof ViewPrompt; v.dispose(); if (tmp) + { return; + } } v = new ViewPrompt(f); }//GEN-LAST:event_jButton2ActionPerformed diff --git a/src/array/visualizer/sort/BitonicSort.java b/src/array/visualizer/sort/BitonicSort.java index 9c6ab85a..a59b9c97 100644 --- a/src/array/visualizer/sort/BitonicSort.java +++ b/src/array/visualizer/sort/BitonicSort.java @@ -11,16 +11,18 @@ */ public class BitonicSort implements Sort { - public static void bitonicSort(final ArrayController ac) + private static void bitonicSort(final ArrayController ac) { bitonicMerge(ac, 0, ac.length, true); } - public static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir) + private static void bitonicMerge(final ArrayController ac, int start, int end, boolean dir) { int mid = (start + end) / 2; if (start == mid) + { return; + } bitonicMerge(ac, start, mid, true); bitonicMerge(ac, mid, end, false); @@ -28,7 +30,9 @@ public static void bitonicMerge(final ArrayController ac, int start, int end, bo int high = end; if (dir) + { for (int i = 0; i < end - start; i++) ; + } } @Override diff --git a/src/array/visualizer/sort/BogoSort.java b/src/array/visualizer/sort/BogoSort.java index e0b95cd4..b2b69ff5 100644 --- a/src/array/visualizer/sort/BogoSort.java +++ b/src/array/visualizer/sort/BogoSort.java @@ -7,7 +7,7 @@ public class BogoSort implements Sort { - public static boolean bogoIsSorted(final ArrayController ac) + private static boolean bogoIsSorted(final ArrayController ac) { for (int i = 1; i < ac.length; i++) { @@ -17,12 +17,14 @@ public static boolean bogoIsSorted(final ArrayController ac) ac.marked.set(2, i - 1); sleep(1); if (ac.array[i] < ac.array[i - 1]) + { return false; + } } return true; } - public static void bogoSort(final ArrayController ac) + private static void bogoSort(final ArrayController ac) { while (!bogoIsSorted(ac)) { diff --git a/src/array/visualizer/sort/BubbleSort.java b/src/array/visualizer/sort/BubbleSort.java index d4cf44b9..51a5274d 100644 --- a/src/array/visualizer/sort/BubbleSort.java +++ b/src/array/visualizer/sort/BubbleSort.java @@ -14,7 +14,7 @@ */ public class BubbleSort implements Sort { - public static void bubbleSort(final ArrayController ac) + private static void bubbleSort(final ArrayController ac) { for (int i = ac.length - 1; i > 0; i--) { diff --git a/src/array/visualizer/sort/CocktailShaker.java b/src/array/visualizer/sort/CocktailShaker.java index 4f26d5e9..c64874e6 100644 --- a/src/array/visualizer/sort/CocktailShaker.java +++ b/src/array/visualizer/sort/CocktailShaker.java @@ -13,7 +13,7 @@ */ public class CocktailShaker implements Sort { - public static void cocktailShakerSort(final ArrayController ac) + private static void cocktailShakerSort(final ArrayController ac) { int i = 0; while (i < ac.length / 2) @@ -22,13 +22,17 @@ public static void cocktailShakerSort(final ArrayController ac) { ac.comps++; if (ac.array[j] > ac.array[j + 1]) + { swap(ac, j, j + 1, 0.022); + } } for (int j = ac.length - i - 1; j > i; j--) { ac.comps++; if (ac.array[j] < ac.array[j - 1]) + { swap(ac, j, j - 1, 0.022); + } } i++; } diff --git a/src/array/visualizer/sort/CountingSort.java b/src/array/visualizer/sort/CountingSort.java index 8c738885..b943a0c3 100644 --- a/src/array/visualizer/sort/CountingSort.java +++ b/src/array/visualizer/sort/CountingSort.java @@ -14,7 +14,7 @@ */ public class CountingSort implements Sort { - public static void countingSort(final ArrayController ac) + private static void countingSort(final ArrayController ac) { int max = analyzemax(ac); int[] counts = new int[max + 1]; @@ -29,7 +29,9 @@ public static void countingSort(final ArrayController ac) for (int i = 0; i < ac.length; i++) { if (counts[x] == 0) + { x++; + } ac.array[i] = x; ac.aa++; counts[x]--; diff --git a/src/array/visualizer/sort/DoubleSelection.java b/src/array/visualizer/sort/DoubleSelection.java index 3fde5310..cf661113 100644 --- a/src/array/visualizer/sort/DoubleSelection.java +++ b/src/array/visualizer/sort/DoubleSelection.java @@ -14,7 +14,7 @@ */ public class DoubleSelection implements Sort { - public static void doubleSelectionSort(final ArrayController ac) + private static void doubleSelectionSort(final ArrayController ac) { int left = 0; @@ -27,14 +27,20 @@ public static void doubleSelectionSort(final ArrayController ac) { ac.marked.set(1, i); if (ac.array[i] > ac.array[biggest]) + { biggest = i; + } if (ac.array[i] < ac.array[smallest]) + { smallest = i; + } ac.comps += 2; sleep(0.02); } if (biggest == left) + { biggest = smallest; + } swap(ac, left, smallest, 0.01); swap(ac, right, biggest, 0.01); left++; diff --git a/src/array/visualizer/sort/GravitySort.java b/src/array/visualizer/sort/GravitySort.java index cc2e774a..0d8c8c61 100644 --- a/src/array/visualizer/sort/GravitySort.java +++ b/src/array/visualizer/sort/GravitySort.java @@ -14,14 +14,16 @@ */ public class GravitySort implements Sort { - public static void gravitySort(final ArrayController ac) + private static void gravitySort(final ArrayController ac) { int max = analyzemax(ac); int[][] abacus = new int[ac.length][max]; for (int i = 0; i < ac.length; i++) { for (int j = 0; j < ac.array[i]; j++) + { abacus[i][abacus[0].length - j - 1] = 1; + } } //apply gravity for (int i = 0; i < abacus[0].length; i++) @@ -33,7 +35,9 @@ public static void gravitySort(final ArrayController ac) //Drop it int droppos = j; while (droppos + 1 < abacus.length && abacus[droppos][i] == 1) + { droppos++; + } if (abacus[droppos][i] == 0) { abacus[j][i] = 0; @@ -48,7 +52,9 @@ public static void gravitySort(final ArrayController ac) { count = 0; for (int y = 0; y < abacus[0].length; y++) + { count += abacus[x][y]; + } ac.array[x] = count; sleep(0.002); } diff --git a/src/array/visualizer/sort/InsertionSort.java b/src/array/visualizer/sort/InsertionSort.java index 6f3f5726..9d894b76 100644 --- a/src/array/visualizer/sort/InsertionSort.java +++ b/src/array/visualizer/sort/InsertionSort.java @@ -14,7 +14,7 @@ */ public class InsertionSort implements Sort { - public static void insertionSort(final ArrayController ac) + private static void insertionSort(final ArrayController ac) { int pos; for (int i = 1; i < ac.length; i++) diff --git a/src/array/visualizer/sort/MaxHeapSort.java b/src/array/visualizer/sort/MaxHeapSort.java index 36bd0e85..f6ea19b3 100644 --- a/src/array/visualizer/sort/MaxHeapSort.java +++ b/src/array/visualizer/sort/MaxHeapSort.java @@ -7,12 +7,14 @@ public class MaxHeapSort implements Sort { - static int SLP = 1; + private static int SLP = 1; - static void maxheapifyrec(final ArrayController ac, int pos, boolean max) + private static void maxheapifyrec(final ArrayController ac, int pos, boolean max) { if (pos >= ac.length) + { return; + } int child1 = pos * 2 + 1; int child2 = pos * 2 + 2; @@ -23,10 +25,14 @@ static void maxheapifyrec(final ArrayController ac, int pos, boolean max) if (child2 >= ac.length) { if (child1 >= ac.length) + { return; //Done, no children + } ac.comps++; if (ac.array[child1] > ac.array[pos]) + { swap(ac, pos, child1, SLP); + } return; } @@ -34,7 +40,9 @@ static void maxheapifyrec(final ArrayController ac, int pos, boolean max) int lrg = child1; ac.comps++; if (ac.array[child2] > ac.array[child1]) + { lrg = child2; + } //Swap with largest child ac.comps++; @@ -42,11 +50,10 @@ static void maxheapifyrec(final ArrayController ac, int pos, boolean max) { swap(ac, pos, lrg, SLP); percdwn(ac, lrg, true, ac.length); - return; } } - static void percdwn(final ArrayController ac, int pos, boolean max, int len) + private static void percdwn(final ArrayController ac, int pos, boolean max, int len) { int child1 = pos * 2 + 1; int child2 = pos * 2 + 2; @@ -54,13 +61,16 @@ static void percdwn(final ArrayController ac, int pos, boolean max, int len) if (child2 >= len) { if (child1 >= len) //Done + { return; - else + } else { //Single Child ac.comps++; if ((max && (ac.array[child1] > ac.array[pos])) || (!max && (ac.array[child1] < ac.array[pos]))) + { swap(ac, pos, child1, SLP); + } return; } } @@ -86,7 +96,7 @@ static void percdwn(final ArrayController ac, int pos, boolean max, int len) } } - public static void maxheapsort(final ArrayController ac) + private static void maxheapsort(final ArrayController ac) { maxheapifyrec(ac, 0, true); for (int i = ac.length - 1; i > 0; i--) diff --git a/src/array/visualizer/sort/MergeSort.java b/src/array/visualizer/sort/MergeSort.java index 82f7a109..ef2f3b00 100644 --- a/src/array/visualizer/sort/MergeSort.java +++ b/src/array/visualizer/sort/MergeSort.java @@ -17,7 +17,7 @@ */ public class MergeSort implements Sort { - static void merge(final ArrayController ac, int min, int max, int mid) + private static void merge(final ArrayController ac, int min, int max, int mid) { try { @@ -42,7 +42,7 @@ static void merge(final ArrayController ac, int min, int max, int mid) } } - static void push(final ArrayController ac, int s, int e) + private static void push(final ArrayController ac, int s, int e) { for (int i = s; i < e; i++) @@ -57,7 +57,7 @@ static void push(final ArrayController ac, int s, int e) } - public static void mergeSort(final ArrayController ac, int min, int max) + private static void mergeSort(final ArrayController ac, int min, int max) { if (max - min == 0) {//only one element. @@ -65,10 +65,12 @@ public static void mergeSort(final ArrayController ac, int min, int max) } else if (max - min == 1) {//only two elements and swaps them if (ac.array[min] > ac.array[max]) + { swap(ac, min, max); + } } else { - int mid = ((int) Math.floor((min + max) / 2));//The midpoint + int mid = (min + max) / 2;//The midpoint mergeSort(ac, min, mid);//sort the left side mergeSort(ac, mid + 1, max);//sort the right side diff --git a/src/array/visualizer/sort/MergeSortOOP.java b/src/array/visualizer/sort/MergeSortOOP.java index dee909f3..fa4cd0c8 100644 --- a/src/array/visualizer/sort/MergeSortOOP.java +++ b/src/array/visualizer/sort/MergeSortOOP.java @@ -13,7 +13,7 @@ */ public class MergeSortOOP implements Sort { - public static void mergeSortOP(final ArrayController ac) + private static void mergeSortOP(final ArrayController ac) { int start = 0; int end = ac.length; @@ -21,10 +21,12 @@ public static void mergeSortOP(final ArrayController ac) mergeOP(ac, start, mid, end); } - public static void mergeOP(final ArrayController ac, int start, int mid, int end) + private static void mergeOP(final ArrayController ac, int start, int mid, int end) { if (start == mid) + { return; + } mergeOP(ac, start, (mid + start) / 2, mid); mergeOP(ac, mid, (mid + end) / 2, end); @@ -35,7 +37,9 @@ public static void mergeOP(final ArrayController ac, int start, int mid, int end for (int nxt = 0; nxt < tmp.length; nxt++) { if (low >= mid && high >= end) + { break; + } if (low < mid && high >= end) { tmp[nxt] = ac.array[low]; @@ -71,7 +75,9 @@ public static void mergeOP(final ArrayController ac, int start, int mid, int end ac.aa++; ac.marked.set(1, start + i); if (end - start >= ac.length / 100) + { sleep(0.5); + } } } diff --git a/src/array/visualizer/sort/MinHeapSort.java b/src/array/visualizer/sort/MinHeapSort.java index 5b85dfcd..06a2265e 100644 --- a/src/array/visualizer/sort/MinHeapSort.java +++ b/src/array/visualizer/sort/MinHeapSort.java @@ -2,12 +2,10 @@ import array.visualizer.ArrayController; -import static array.visualizer.utils.Swaps.swap; - public class MinHeapSort implements Sort { - static void minheapsort(ArrayController ac) + private static void minheapsort(ArrayController ac) { } diff --git a/src/array/visualizer/sort/QuickSort.java b/src/array/visualizer/sort/QuickSort.java index 618de75c..5cc3d2fc 100644 --- a/src/array/visualizer/sort/QuickSort.java +++ b/src/array/visualizer/sort/QuickSort.java @@ -14,7 +14,7 @@ */ public class QuickSort implements Sort { - public static void quickSort(final ArrayController ac, int p, int r) + private static void quickSort(final ArrayController ac, int p, int r) { if (p < r) { @@ -25,7 +25,7 @@ public static void quickSort(final ArrayController ac, int p, int r) } } - public static int partition(final ArrayController ac, int p, int r) + private static int partition(final ArrayController ac, int p, int r) { int x = ac.array[p]; @@ -53,9 +53,12 @@ public static int partition(final ArrayController ac, int p, int r) } if (i < j) + { swap(ac, i, j); - else + } else + { return j; + } } } diff --git a/src/array/visualizer/sort/RadixLSD.java b/src/array/visualizer/sort/RadixLSD.java index 9ebe5043..a2283e68 100644 --- a/src/array/visualizer/sort/RadixLSD.java +++ b/src/array/visualizer/sort/RadixLSD.java @@ -31,7 +31,9 @@ public static void radixLSDsort_(final ArrayController ac, int radix) int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) - registers[i] = new ArrayList(); + { + registers[i] = new ArrayList<>(); + } for (int p = 0; p <= highestpower; p++) { for (int i = 0; i < ac.length; i++) @@ -45,13 +47,15 @@ public static void radixLSDsort_(final ArrayController ac, int radix) } } - public static void radixLSDsort(final ArrayController ac, int radix) + private static void radixLSDsort(final ArrayController ac, int radix) { clearmarked(); int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) - registers[i] = new ArrayList(); + { + registers[i] = new ArrayList<>(); + } for (int p = 0; p <= highestpower; p++) { for (int i = 0; i < ac.length; i++) @@ -59,7 +63,9 @@ public static void radixLSDsort(final ArrayController ac, int radix) ac.aa++; ac.marked.set(1, i); if (i % 2 == 0) + { sleep(1); + } registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } fancyTranscribe(ac, registers); @@ -72,7 +78,9 @@ public static void radixLSDsortnd(final ArrayController ac, int radix, int min, int highestpower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) - registers[i] = new ArrayList(); + { + registers[i] = new ArrayList<>(); + } for (int p = 0; p <= highestpower; p++) { for (int i = min; i < max; i++) diff --git a/src/array/visualizer/sort/RadixLSDInPlace.java b/src/array/visualizer/sort/RadixLSDInPlace.java index dffd795c..990cd1c6 100644 --- a/src/array/visualizer/sort/RadixLSDInPlace.java +++ b/src/array/visualizer/sort/RadixLSDInPlace.java @@ -15,7 +15,7 @@ public RadixLSDInPlace(int radix) this.radix = radix; } - public static void inPlaceRadixLSDSort(final ArrayController ac, int radix) + private static void inPlaceRadixLSDSort(final ArrayController ac, int radix) { int pos = 0; int[] vregs = new int[radix - 1]; @@ -24,7 +24,9 @@ public static void inPlaceRadixLSDSort(final ArrayController ac, int radix) for (int p = 0; p <= maxpower; p++) { for (int i = 0; i < vregs.length; i++) + { vregs[i] = ac.length - 1; + } pos = 0; for (int i = 0; i < ac.length; i++) { @@ -36,10 +38,14 @@ public static void inPlaceRadixLSDSort(final ArrayController ac, int radix) } else { for (int j = 0; j < vregs.length; j++) + { ac.marked.set(j + 1, vregs[j]); + } swapUpToNM(ac, pos, vregs[digit - 1], 0.0011 * smul); for (int j = digit - 1; j > 0; j--) + { vregs[j - 1]--; + } } } diff --git a/src/array/visualizer/sort/RadixMSD.java b/src/array/visualizer/sort/RadixMSD.java index 556f1e20..3af34515 100644 --- a/src/array/visualizer/sort/RadixMSD.java +++ b/src/array/visualizer/sort/RadixMSD.java @@ -24,7 +24,7 @@ public RadixMSD(int radix) this.radix = radix; } - public static void radixMSDSort(final ArrayController ac, int radix) + private static void radixMSDSort(final ArrayController ac, int radix) { clearmarked(); int highestpower = analyze(ac, radix); @@ -33,15 +33,19 @@ public static void radixMSDSort(final ArrayController ac, int radix) radixMSDRec(ac, 0, ac.length, radix, highestpower); } - public static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow) + private static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow) { if (min >= max || pow < 0) + { return; + } ac.marked.set(2, max); ac.marked.set(3, min); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) - registers[i] = new ArrayList(); + { + registers[i] = new ArrayList<>(); + } for (int i = min; i < max; i++) { ac.marked.set(1, i); @@ -51,11 +55,11 @@ public static void radixMSDRec(final ArrayController ac, int min, int max, int r transcribermsd(ac, registers, min); int sum = 0; - for (int i = 0; i < registers.length; i++) + for (ArrayList register : registers) { - radixMSDRec(ac, sum + min, sum + min + registers[i].size(), radix, pow - 1); - sum += registers[i].size(); - registers[i].clear(); + radixMSDRec(ac, sum + min, sum + min + register.size(), radix, pow - 1); + sum += register.size(); + register.clear(); } } diff --git a/src/array/visualizer/sort/SelectionSort.java b/src/array/visualizer/sort/SelectionSort.java index a7c66808..fa4abdc2 100644 --- a/src/array/visualizer/sort/SelectionSort.java +++ b/src/array/visualizer/sort/SelectionSort.java @@ -15,7 +15,7 @@ public class SelectionSort implements Sort { - public static void selectionSort(final ArrayController ac) + private static void selectionSort(final ArrayController ac) { for (int i = 0; i < ac.length - 1; i++) { diff --git a/src/array/visualizer/sort/ShatterSorts.java b/src/array/visualizer/sort/ShatterSorts.java index 4d710d4b..e0150216 100644 --- a/src/array/visualizer/sort/ShatterSorts.java +++ b/src/array/visualizer/sort/ShatterSorts.java @@ -23,12 +23,14 @@ public ShatterSorts(int base) this.base = base; } - public static void shatterPartition(final ArrayController ac, int num) + private static void shatterPartition(final ArrayController ac, int num) { int shatters = (int) Math.ceil(ac.length / (double) num); ArrayList[] registers = new ArrayList[shatters]; for (int i = 0; i < shatters; i++) - registers[i] = new ArrayList(); + { + registers[i] = new ArrayList<>(); + } for (int i = 0; i < ac.length; i++) { registers[ac.array[i] / num].add(ac.array[i]); @@ -37,7 +39,7 @@ public static void shatterPartition(final ArrayController ac, int num) transcribe(registers, ac); } - public static void shatterSort(final ArrayController ac, int num) + private static void shatterSort(final ArrayController ac, int num) { int shatters = (int) Math.ceil(ac.length / (double) num); shatterPartition(ac, num); @@ -47,16 +49,20 @@ public static void shatterSort(final ArrayController ac, int num) for (int j = 0; j < num; j++) { if (i * num + j >= ac.length) + { tmp[j] = -1; - else + } else + { tmp[j] = ac.array[i * num + j]; + } ac.aa++; } - for (int j = 0; j < tmp.length; j++) + for (int tmpj : tmp) { - int tmpj = tmp[j]; if (i * num + (tmpj % num) >= ac.length || tmpj == -1) + { break; + } ac.array[i * num + (tmpj % num)] = tmpj; ac.aa++; ac.marked.set(1, i * num + (tmpj % num)); @@ -68,7 +74,9 @@ public static void shatterSort(final ArrayController ac, int num) public static void simpleShatterSort(final ArrayController ac, int num, int rate) { for (int i = num; i > 1; i = i / rate) + { shatterPartition(ac, i); + } shatterPartition(ac, 1); } diff --git a/src/array/visualizer/sort/ShellSort.java b/src/array/visualizer/sort/ShellSort.java index 9784a454..4457367e 100644 --- a/src/array/visualizer/sort/ShellSort.java +++ b/src/array/visualizer/sort/ShellSort.java @@ -7,7 +7,7 @@ public class ShellSort implements Sort { - public static void shellSort(final ArrayController ac, int gap, int divrate) + private static void shellSort(final ArrayController ac, int gap, int divrate) { double sleepamt = 1d; while (gap > 0) @@ -37,7 +37,9 @@ public static void shellSort(final ArrayController ac, int gap, int divrate) } if (gap == 1) //Done + { break; + } gap = Math.max(gap / divrate, 1); //Ensure that we do gap 1 //sleepamt /= divrate; diff --git a/src/array/visualizer/sort/TimeSort.java b/src/array/visualizer/sort/TimeSort.java index 59db45e4..400fe979 100644 --- a/src/array/visualizer/sort/TimeSort.java +++ b/src/array/visualizer/sort/TimeSort.java @@ -27,11 +27,11 @@ public TimeSort(int magnitude) this.magnitude = magnitude; } - public static void timeSort(final ArrayController ac, int magnitude) + private static void timeSort(final ArrayController ac, int magnitude) { final int A = magnitude; next = 0; - ArrayList threads = new ArrayList(); + ArrayList threads = new ArrayList<>(); final int[] tmp = ac.array.clone(); for (int i = 0; i < ac.length; i++) { @@ -55,15 +55,17 @@ public void run() }); } for (Thread t : threads) + { t.start(); + } sleep(ac.length * A); insertionSort(ac, 0, ac.length, 0.2d); } - static volatile int next = 0; + private static volatile int next = 0; - public static synchronized void report(final ArrayController ac, int a) + private static synchronized void report(final ArrayController ac, int a) { ac.marked.set(0, next); ac.array[next] = a; diff --git a/src/array/visualizer/sort/WeaveMerge.java b/src/array/visualizer/sort/WeaveMerge.java index e51db6b0..ee0066fa 100644 --- a/src/array/visualizer/sort/WeaveMerge.java +++ b/src/array/visualizer/sort/WeaveMerge.java @@ -19,7 +19,7 @@ */ public class WeaveMerge implements Sort { - static void weaveMerge(final ArrayController ac, int min, int max, int mid) + private static void weaveMerge(final ArrayController ac, int min, int max, int mid) { try { @@ -56,7 +56,7 @@ static void push(final ArrayController ac, int s, int e) } } - public static void weaveMergeSort(final ArrayController ac, int min, int max) + private static void weaveMergeSort(final ArrayController ac, int min, int max) { if (max - min == 0) {//only one element. @@ -64,10 +64,12 @@ public static void weaveMergeSort(final ArrayController ac, int min, int max) } else if (max - min == 1) {//only two elements and swaps them if (ac.array[min] > ac.array[max]) + { swap(ac, min, max); + } } else { - int mid = ((int) Math.floor((min + max) / 2));//The midpoint + int mid = (min + max) / 2;//The midpoint weaveMergeSort(ac, min, mid);//sort the left side weaveMergeSort(ac, mid + 1, max);//sort the right side diff --git a/src/array/visualizer/utils/Analysis.java b/src/array/visualizer/utils/Analysis.java index 8b98bb50..bfc6c48c 100644 --- a/src/array/visualizer/utils/Analysis.java +++ b/src/array/visualizer/utils/Analysis.java @@ -32,11 +32,13 @@ public static int analyze(final ArrayController ac, int base) public static int analyze_sneaky(int[] array, int base) { int a = 0; - for (int i = 0; i < array.length; i++) - if ((int) (Math.log(array[i]) / Math.log(base)) > a) + for (int anArray : array) + { + if ((int) (Math.log(anArray) / Math.log(base)) > a) { - a = (int) (Math.log(array[i]) / Math.log(base)); + a = (int) (Math.log(anArray) / Math.log(base)); } + } return a; } @@ -46,7 +48,9 @@ public static int analyzemax(final ArrayController ac) for (int i = 0; i < ac.length; i++) { if (ac.array[i] > a) + { a = ac.array[i]; + } ac.marked.set(1, i); ac.aa++; sleep(1.0); diff --git a/src/array/visualizer/utils/Searches.java b/src/array/visualizer/utils/Searches.java index f2c7a8e4..076f2ccc 100644 --- a/src/array/visualizer/utils/Searches.java +++ b/src/array/visualizer/utils/Searches.java @@ -13,14 +13,16 @@ */ public class Searches { - public static void linearSearch(final ArrayController ac, int find) throws Exception + public static void linearSearch(final ArrayController ac, int find) { for (int i = 0; i < ac.length; i++) { ac.aa++; ac.comps++; if (ac.array[i] == find) + { break; + } ac.marked.set(0, i); sleep(5); } @@ -38,9 +40,12 @@ public static void binarySearch(final ArrayController ac, int find) throws Excep ac.aa++; Thread.sleep(1000); if (ac.array[at] < find) + { at += change; - else + } else + { at -= change; + } change /= 2; } sleep(1000); diff --git a/src/array/visualizer/utils/Swaps.java b/src/array/visualizer/utils/Swaps.java index b087973b..9a3562ec 100644 --- a/src/array/visualizer/utils/Swaps.java +++ b/src/array/visualizer/utils/Swaps.java @@ -46,26 +46,42 @@ public static void swapnm(final ArrayController ac, int i, int j, double pause) public static void swapUpTo(final ArrayController ac, int pos, int to, double pause) { if (to - pos > 0) + { for (int i = pos; i < to; i++) + { swap(ac, i, i + 1, pause); - else + } + } else + { for (int i = pos; i > to; i--) + { swap(ac, i, i - 1, pause); + } + } } public static void swapUpToNM(final ArrayController ac, int pos, int to, double pause) { if (to - pos > 0) + { for (int i = pos; i < to; i++) + { swapnm(ac, i, i + 1, pause); - else + } + } else + { for (int i = pos; i > to; i--) + { swapnm(ac, i, i - 1, pause); + } + } } public static void swapUp(final ArrayController ac, int pos, double pause) { for (int i = pos; i < ac.length; i++) + { swap(ac, i, i + 1, pause); + } } } diff --git a/src/array/visualizer/utils/Transcriptions.java b/src/array/visualizer/utils/Transcriptions.java index 9c84db0e..ac76324f 100644 --- a/src/array/visualizer/utils/Transcriptions.java +++ b/src/array/visualizer/utils/Transcriptions.java @@ -18,48 +18,48 @@ public class Transcriptions public static void transcribe(ArrayList[] registers, final ArrayController ac) { int total = 0; - for (int ai = 0; ai < registers.length; ai++) + for (ArrayList register : registers) { - for (int i = 0; i < registers[ai].size(); i++) + for (Integer aRegister : register) { sleep(1); - ac.array[total] = registers[ai].get(i); + ac.array[total] = aRegister; ac.marked.set(1, total); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } public static void transcribenm(ArrayList[] registers, int[] array) { int total = 0; - for (int ai = 0; ai < registers.length; ai++) + for (ArrayList register : registers) { - for (int i = 0; i < registers[ai].size(); i++) + for (Integer aRegister : register) { - array[total] = registers[ai].get(i); + array[total] = aRegister; total++; } - registers[ai].clear(); + register.clear(); } } public static void transcribend(final ArrayController ac, ArrayList[] registers, int min) { int total = 0; - for (int ai = 0; ai < registers.length; ai++) + for (ArrayList register : registers) { - for (int i = 0; i < registers[ai].size(); i++) + for (int i = 0; i < register.size(); i++) { sleep((min + i) % 5 / 4); - ac.array[total + min] = registers[ai].get(i); + ac.array[total + min] = register.get(i); ac.marked.set(1, total + min); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } @@ -67,7 +67,9 @@ public static void transcribermsd(final ArrayController ac, ArrayList[] { int total = 0; for (ArrayList ai : registers) + { total += ai.size(); + } int tmp = 0; for (int ai = registers.length - 1; ai >= 0; ai--) { @@ -85,17 +87,17 @@ public static void transcribermsd(final ArrayController ac, ArrayList[] public static void transcribe(ArrayList[] registers, final ArrayController ac, int start) { int total = start; - for (int ai = 0; ai < registers.length; ai++) + for (ArrayList register : registers) { - for (int i = 0; i < registers[ai].size(); i++) + for (Integer aRegister : register) { sleep(1); - ac.array[total] = registers[ai].get(i); + ac.array[total] = aRegister; ac.marked.set(1, total); total++; ac.aa++; } - registers[ai].clear(); + register.clear(); } } @@ -109,9 +111,11 @@ public static void fancyTranscribe(final ArrayController ac, ArrayList[ { int register = i % radix; if (register == 0) + { sleep(radix);//radix + } int pos = (int) (((double) register * ((double) tmp.length / radix)) + ((double) i / radix)); - if (tmpwrite[pos] == false) + if (!tmpwrite[pos]) { ac.array[pos] = tmp[pos]; ac.aa++; @@ -120,11 +124,13 @@ public static void fancyTranscribe(final ArrayController ac, ArrayList[ ac.marked.set(register, pos); } for (int i = 0; i < tmpwrite.length; i++) - if (tmpwrite[i] == false) + { + if (!tmpwrite[i]) { ac.array[i] = tmp[i]; ac.aa++; } + } clearmarked(); } } From d45581744c29a8825780ca75f3cfe4e9a3c9e36e Mon Sep 17 00:00:00 2001 From: Zoey Llewellyn Hewll Date: Mon, 7 Jan 2019 21:06:24 +0800 Subject: [PATCH 3/3] Manual cleanup * Narrow catch clauses * Ensure method names are in lowerCamelCase and classes in UpperCamelCase * Ensure variable names are in lowerCamelCase or ALLCAPS * Mark variables private where possible (ignoring variables in all caps) * Remove unused methods and variables (ignoring incomplete methods, members that are used in commented-out code, alternative sorting algorithms, and utility methods) --- src/array/visualizer/ArrayVisualizer.java | 267 +++++++++--------- src/array/visualizer/SortPrompt.java | 10 +- src/array/visualizer/sort/BogoSort.java | 5 - src/array/visualizer/sort/CountingSort.java | 2 +- src/array/visualizer/sort/GravitySort.java | 15 +- src/array/visualizer/sort/MaxHeapSort.java | 22 +- src/array/visualizer/sort/MergeSort.java | 31 +- src/array/visualizer/sort/MinHeapSort.java | 4 +- src/array/visualizer/sort/RadixLSD.java | 40 +-- .../visualizer/sort/RadixLSDInPlace.java | 7 +- src/array/visualizer/sort/RadixMSD.java | 9 +- src/array/visualizer/sort/SelectionSort.java | 8 +- src/array/visualizer/sort/ShellSort.java | 10 +- src/array/visualizer/sort/WeaveMerge.java | 33 +-- src/array/visualizer/utils/Analysis.java | 15 +- src/array/visualizer/utils/Searches.java | 4 +- src/array/visualizer/utils/Swaps.java | 6 +- .../visualizer/utils/Transcriptions.java | 20 +- 18 files changed, 214 insertions(+), 294 deletions(-) diff --git a/src/array/visualizer/ArrayVisualizer.java b/src/array/visualizer/ArrayVisualizer.java index 7106d3b5..35e0293a 100644 --- a/src/array/visualizer/ArrayVisualizer.java +++ b/src/array/visualizer/ArrayVisualizer.java @@ -2,10 +2,13 @@ import array.visualizer.sort.*; +import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.Image; +import java.awt.Polygon; import java.util.ArrayList; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiSystem; @@ -14,9 +17,6 @@ import static array.visualizer.utils.Swaps.*; -import java.awt.BasicStroke; -import java.awt.Graphics2D; -import java.awt.Polygon; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.midi.Instrument; @@ -24,15 +24,13 @@ public class ArrayVisualizer { + private static final JFrame window = new JFrame(); - static final JFrame window = new JFrame(); - - static ArrayController arrayController = new ArrayController(1000); - static String heading = ""; - static int frames; - static int snd = 0; - static long nanos; - static Font fon = new Font("TimesRoman", Font.PLAIN, (int) (640 / 1280.0 * 25)); + private static ArrayController arrayController = new ArrayController(1000); + private static String heading = ""; + private static int frames; + private static int snd = 0; + private static Font fon = new Font("TimesRoman", Font.PLAIN, (int) (640 / 1280.0 * 25)); static boolean CIRCLEDRAW = false; static boolean COLORONLY = false; @@ -47,31 +45,33 @@ public class ArrayVisualizer static boolean SOUND = false; static double SOUNDMUL = 1.0; static double SLEEPRATIO = 1.0; - static UtilFrame uf; - static ViewPrompt v; - static Synthesizer synth; - static MidiChannel chan; - static Thread sortingThread; + private static UtilFrame uf; + private static ViewPrompt v; + private static Synthesizer synth; + private static MidiChannel chan; + private static Thread sortingThread; static boolean SHUFFLEANIM = true; - static long starttime = 0; - static long stoptime = 0; - static boolean running = false; - static long sleeptime = 0; + private static long startTime = 0; + private static long stopTime = 0; + private static boolean running = false; + private static long sleepTime = 0; - static long rtupdatefreq = 100; //How frequently to update the real time display in ms - static double realt = 0d; - static long lastrtupdate = 0; + private static long rtUpdateFreq = 100; //How frequently to update the real time display in ms + private static double realt = 0d; + private static long lastRtUpdate = 0; static int COLORSTRAT = 1; //0 = Solid, 1 = Rainbow, 2 = Segments static Color COLORSTRAT0col = new Color(0, 204, 0); static ArrayList COLORSTRAT2cols = new ArrayList<>(); - static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); - static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); + static String[] comparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!"); + static String[] distributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!"); - static int cx = 0; - static int cy = 0; + private static int cx = 0; + private static int cy = 0; + + private static double addamt = 0.0; public static double calcVel() { @@ -86,13 +86,11 @@ public static double calcVel() return count; } - public static synchronized void SetSound(boolean val) + public static synchronized void setSound(boolean val) { SOUND = val; } - static double addamt = 0.0; - public static void sleep(double milis) { if (milis <= 0) @@ -114,11 +112,11 @@ public static void sleep(double milis) addamt -= (double) actual / 1000000.0; if (running) { - sleeptime += actual; + sleepTime += actual; } - } catch (Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } } @@ -222,9 +220,10 @@ public void run() try { sleep(1); - } catch (Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); + e.printStackTrace(); } } } @@ -273,12 +272,12 @@ public void run() yscl = (double) (window.getHeight() - 30) / arrayController.length; int amt = 0; int circamt = arrayController.length / 2; - int linkedpixdrawx = 0; - int linkedpixdrawy = 0; + int linkedPixDrawX = 0; + int linkedPixDrawY= 0; frames++; - int halfwidth = window.getWidth() / 2; - int halfheight = window.getHeight() / 2; + int halfWidth = window.getWidth() / 2; + int halfHeight = window.getHeight() / 2; int dotw = (int) (2 * (window.getWidth() / 640.0)); int doth = (int) (2 * (window.getHeight() / 480.0)); @@ -291,11 +290,11 @@ public void run() if (XMASDRAW) { - double trunksize = (double) ch * 0.1; - int trunkstart = arrayController.length - (int) (trunksize / yscl); + double trunkSize = (double) ch * 0.1; + int trunkStart = arrayController.length - (int) (trunkSize / yscl); - double ornamentsize = (double) ch * 0.1; - int ornamentend = (int) (ornamentsize / yscl); + double ornamentSize = (double) ch * 0.1; + int ornamentEnd = (int) (ornamentSize / yscl); int width = 0; @@ -312,10 +311,10 @@ public void run() if (arrayController.marked.contains(i) || arrayController.marked.contains(i - 1) || arrayController.marked.contains(i - 2) || arrayController.marked.contains(i - 3)) { g.setColor(Color.BLACK); - } else if (j < ornamentend) + } else if (j < ornamentEnd) { g.setColor(new Color(255, 0, 0)); - } else if (j > trunkstart) + } else if (j > trunkStart) { g.setColor(new Color(102, 34, 0)); } else @@ -323,19 +322,19 @@ public void run() g.setColor(new Color(0, 102, 0)); } - if (j < ornamentend) + if (j < ornamentEnd) { //ORNAMENT - width = (int) (Math.sin(Math.acos((j * yscl) / ornamentsize * 2.0 - 1.0)) * ornamentsize); - } else if (j > trunkstart) + width = (int) (Math.sin(Math.acos((j * yscl) / ornamentSize * 2.0 - 1.0)) * ornamentSize); + } else if (j > trunkStart) { //TRUNK width = (int) (cw * 0.2); } else //TREE { - width = (int) ((((j - ornamentend) % 85) * 0.005 + 0.075) * cw); + width = (int) ((((j - ornamentEnd) % 85) * 0.005 + 0.075) * cw); } int step = (int) (i * yscl + yscl) - (int) (i * yscl); - g.fillRect(halfwidth - width / 2, (int) (i * yscl) + 32, width, step); + g.fillRect(halfWidth - width / 2, (int) (i * yscl) + 32, width, step); } } @@ -346,7 +345,7 @@ else if (PYRAMIDDRAW && CIRCLEDRAW) g.setStroke(new BasicStroke(1.0f)); //significantly increased performance double diameter = (double) Math.min(cw, ch - 32); - double diamstep = Math.min(xscl, yscl); + double diamStep = Math.min(xscl, yscl); for (int i = arrayController.length - 1; i >= 0; i--) { @@ -369,15 +368,15 @@ else if (PYRAMIDDRAW && CIRCLEDRAW) if (!DISPARITYDRAW) { - g.drawOval(halfwidth - radius, halfheight - radius + 15, (int) diameter, (int) diameter); + g.drawOval(halfWidth - radius, halfHeight - radius + 15, (int) diameter, (int) diameter); } else { int dist = Math.abs(i - arrayController.array[i]); double disparity = 1.0 - ((double) Math.min(dist, arrayController.length - dist) / ((double) arrayController.length / 2.0)); - int actualdiam = (int) (disparity * diameter); - g.drawOval(halfwidth - actualdiam / 2, halfheight - radius + 15, actualdiam, actualdiam); + int actualDiam = (int) (disparity * diameter); + g.drawOval(halfWidth - actualDiam / 2, halfHeight - radius + 15, actualDiam, actualDiam); } - diameter -= diamstep; + diameter -= diamStep; } } //PYRAMID DRAW METHOD @@ -385,7 +384,7 @@ else if (PYRAMIDDRAW) { //Setup - double curpos = 0.0; + double curPos = 0.0; //Draw loop for (int i = 0; i < arrayController.length; i++) @@ -409,15 +408,15 @@ else if (PYRAMIDDRAW) if (!DRAWFLIPPED) { int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * (double) cw); - int step = (int) (curpos + yscl) - (int) curpos; - g.fillRect(halfwidth - len / 2, (int) curpos + 32, len, step); - curpos += yscl; + int step = (int) (curPos + yscl) - (int) curPos; + g.fillRect(halfWidth - len / 2, (int) curPos + 32, len, step); + curPos += yscl; } else { int len = (int) ((double) arrayController.array[i] / (double) arrayController.length * ((double) ch - 32.0)); - int step = (int) (curpos + xscl) - (int) curpos; - g.fillRect((int) curpos, halfheight - len / 2 + 16, step, len); - curpos += xscl; + int step = (int) (curPos + xscl) - (int) curPos; + g.fillRect((int) curPos, halfHeight - len / 2 + 16, step, len); + curPos += xscl; } } } @@ -427,9 +426,9 @@ else if (MESHDRAW) int trih = window.getHeight() / 20; //Height of triangles to use, Width will be scaled accordingly - int tripercol = window.getHeight() / trih * 2; //Triangles per vertical column - int triperrow = arrayController.length / tripercol; //Triangles per horizontal row - double triw = (double) window.getWidth() / triperrow; //Width of triangles to use + int triPerCol = window.getHeight() / trih * 2; //Triangles per vertical column + int triPerRow = arrayController.length / triPerCol; //Triangles per horizontal row + double triw = (double) window.getWidth() / triPerRow; //Width of triangles to use double curx = 0; int cury = 0; @@ -452,9 +451,9 @@ else if (MESHDRAW) g.setColor(getIntColor(arrayController.array[i], sortErrors)); } - //If i/triperrow is even, then triangle points right, else left + //If i/triPerRow is even, then triangle points right, else left boolean direction = false; - if (((i - 1) / triperrow) % 2 == 0) + if (((i - 1) / triPerRow) % 2 == 0) { direction = true; } @@ -498,7 +497,7 @@ else if (MESHDRAW) g.fillPolygon(triptsx, triptsy, triptsx.length); //If at the end of a row, reset curx - if (i != 0 && i % triperrow == 0) + if (i != 0 && i % triPerRow == 0) { curx = 0d; cury += trih / 2; @@ -521,18 +520,18 @@ else if (MESHDRAW) g.setColor(getIntColor(arrayController.array[i], sortErrors)); } - double sinval = Math.sin(i * Math.PI / circamt); - double cosval = Math.cos(i * Math.PI / circamt); + double sinVal = Math.sin(i * Math.PI / circamt); + double cosVal = Math.cos(i * Math.PI / circamt); //COLOR ONLY NO LENGTH if (COLORONLY) { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth + (int) (sinval * (window.getWidth() - 64) / 2.0), halfheight - (int) (cosval * (window.getHeight() - 96) / 2.0)); - p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * (window.getWidth() - 64) / 2.0), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * (window.getHeight() - 96) / 2.0)); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (sinVal * (window.getWidth() - 64) / 2.0), halfHeight - (int) (cosVal * (window.getHeight() - 96) / 2.0)); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * (window.getWidth() - 64) / 2.0), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * (window.getHeight() - 96) / 2.0)); g.fillPolygon(p); - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*(window.getWidth()-64)/2.0), halfheight-(int)(cosval*(window.getHeight()-96)/2.0)); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*(window.getWidth()-64)/2.0), halfHeight-(int)(cosVal*(window.getHeight()-96)/2.0)); } //DISPARITY else if (DISPARITYDRAW) @@ -541,52 +540,52 @@ else if (DISPARITYDRAW) if (PIXELDRAW) { - int linkedpixX = halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * len)) + dotw / 2; - int linkedpixY = halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * len)) + doth / 2; + int linkedPixX = halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * len)) + dotw / 2; + int linkedPixY = halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * len)) + doth / 2; - g.fillRect(linkedpixX - dotw / 2, linkedpixY - doth / 2, dotw, doth); + g.fillRect(linkedPixX - dotw / 2, linkedPixY - doth / 2, dotw, doth); if (LINKEDPIXELDRAW) { if (i > 0) { - g.drawLine(linkedpixX, linkedpixY, linkedpixdrawx, linkedpixdrawy); + g.drawLine(linkedPixX, linkedPixY, linkedPixDrawX, linkedPixDrawY); } - linkedpixdrawx = linkedpixX; - linkedpixdrawy = linkedpixY; + linkedPixDrawX = linkedPixX; + linkedPixDrawY = linkedPixY; } } else { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * len)), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * len))); - p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * len)), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * len))); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * len)), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * len))); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * len)), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * len))); g.fillPolygon(p); } - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfHeight-(int)(cosVal*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } //PIXELS ONLY else if (PIXELDRAW) { - g.fillRect(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), dotw, doth); + g.fillRect(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), dotw, doth); if (LINKEDPIXELDRAW) { if (i > 0) { - g.drawLine(halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), linkedpixdrawx, linkedpixdrawy); + g.drawLine(halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), linkedPixDrawX, linkedPixDrawY); } - linkedpixdrawx = halfwidth + (int) (sinval * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); - linkedpixdrawy = halfheight - (int) (cosval * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); + linkedPixDrawX = halfWidth + (int) (sinVal * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); + linkedPixDrawY = halfHeight - (int) (cosVal * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length))); } } //LENGTH AND COLOR else { Polygon p = new Polygon(); - p.addPoint(halfwidth, halfheight); - p.addPoint(halfwidth + (int) (Math.sin((i) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfheight - (int) (Math.cos((i) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length)))); - p.addPoint(halfwidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length))), halfheight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length)))); + p.addPoint(halfWidth, halfHeight); + p.addPoint(halfWidth + (int) (Math.sin((i) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[i] / (double) arrayController.length))), halfHeight - (int) (Math.cos((i) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[i] / (double) arrayController.length)))); + p.addPoint(halfWidth + (int) (Math.sin((i + 1) * Math.PI / circamt) * ((window.getWidth() - 64) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length))), halfHeight - (int) (Math.cos((i + 1) * Math.PI / circamt) * ((window.getHeight() - 96) / 2.0 * (arrayController.array[Math.min(i + 1, arrayController.length - 1)] / (double) arrayController.length)))); g.fillPolygon(p); - //g.drawLine(halfwidth, halfheight, halfwidth+(int)(sinval*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfheight-(int)(cosval*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); + //g.drawLine(halfWidth, halfHeight, halfWidth+(int)(sinVal*((window.getWidth()-64)/2.0*(array[i]/(double)array.length))), halfHeight-(int)(cosVal*((window.getHeight()-96)/2.0*(array[i]/(double)array.length)))); } } } else @@ -625,10 +624,10 @@ else if (PIXELDRAW) { if (i > 0) { - g.drawLine(amt, y, linkedpixdrawx, linkedpixdrawy); + g.drawLine(amt, y, linkedPixDrawX, linkedPixDrawY); } - linkedpixdrawx = amt; - linkedpixdrawy = y; + linkedPixDrawX = amt; + linkedPixDrawY = y; } } else { @@ -652,11 +651,11 @@ else if (PIXELDRAW) int sortpercent = (int) ((double) (arrayController.length - sortErrors) / (double) arrayController.length * 100.0); g.drawString(String.format("%d%% Sorted (%d Segment%c)", sortpercent, sortErrors + 1, sortErrors == 0 ? ' ' : 's'), 10, (int) (cw / 1280.0 * 80) + 30); //g.drawString(String.format("%d Segment%c", sortErrors+1, sortErrors==0?' ':'s'), 10, (int)(cw/1280.0*100)+30); - double slpt = (double) sleeptime / 1000000000.0; - if (System.currentTimeMillis() - lastrtupdate > rtupdatefreq) + double slpt = (double) sleepTime / 1000000000.0; + if (System.currentTimeMillis() - lastRtUpdate > rtUpdateFreq) { - realt = (double) (running ? (System.nanoTime() - starttime - sleeptime) : (stoptime - starttime - sleeptime)) / 1000000.0; - lastrtupdate = System.currentTimeMillis(); + realt = (double) (running ? (System.nanoTime() - startTime - sleepTime) : (stopTime - startTime - sleepTime)) / 1000000.0; + lastRtUpdate = System.currentTimeMillis(); } g.drawString(String.format("Real Time: %.2fms", realt), 10, (int) (cw / 1280.0 * 100) + 30); g.drawString(String.format("Sleep Time: %.2fs", slpt), 10, (int) (cw / 1280.0 * 120) + 30); @@ -668,14 +667,14 @@ else if (PIXELDRAW) } //0 = Solid, 1 = Rainbow, 2 = Segments - public Color getIntColor(int i, int segnum) + public Color getIntColor(int i, int segNum) { if (COLORSTRAT == 1) { return Color.getHSBColor(((float) i / arrayController.length), 1.0F, 0.8F); } else if (COLORSTRAT == 2) { - return COLORSTRAT2cols.get(segnum % COLORSTRAT2cols.size()); + return COLORSTRAT2cols.get(segNum % COLORSTRAT2cols.size()); } return COLORSTRAT0col; } @@ -701,12 +700,12 @@ public Color getRevColor() } } - public static void refresharray() throws Exception + public static void refreshArray() throws InterruptedException { - clearmarked(); + clearMarked(); if (running) { - stoptime = System.nanoTime(); + stopTime = System.nanoTime(); running = false; Thread.sleep(1000); } @@ -731,10 +730,10 @@ public static void refresharray() throws Exception arrayController.aa = 0; arrayController.comps = 0; shuffle(arrayController.array); - clearmarked(); + clearMarked(); Thread.sleep(500); - starttime = System.nanoTime(); - sleeptime = 0; + startTime = System.nanoTime(); + sleepTime = 0; running = true; } @@ -752,7 +751,7 @@ public static void rianr(int[] arr) shuffle(arr); } - public static void clearmarked() + public static void clearMarked() { arrayController.clearMarked(); } @@ -778,7 +777,7 @@ public static int sleepTime(double d) return (int) (arrayController.length * d) / 4; } - public synchronized static void RunAllSorts() + public synchronized static void runAllSorts() { if (sortingThread != null) { @@ -794,7 +793,7 @@ public synchronized static void RunAllSorts() } } - SetSound(true); + setSound(true); sortingThread = new Thread() { @Override @@ -823,25 +822,25 @@ public void run() ) { chan.allNotesOff(); - refresharray(); + refreshArray(); heading = sort.name(); sort.sort(arrayController); } - } catch (Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - SetSound(false); - stoptime = System.nanoTime(); + setSound(false); + stopTime = System.nanoTime(); running = false; chan.allNotesOff(); - clearmarked(); + clearMarked(); } }; sortingThread.start(); } - public static void ReportComparativeSort(int n) + public static void reportComparativeSort(int n) { if (sortingThread != null && sortingThread.isAlive()) { @@ -849,7 +848,7 @@ public static void ReportComparativeSort(int n) } final int num = n; - SetSound(true); + setSound(true); sortingThread = new Thread() { @Override @@ -857,7 +856,7 @@ public void run() { try { - refresharray(); + refreshArray(); Sort sort; switch (num) { @@ -903,19 +902,19 @@ public void run() heading = sort.name(); sort.sort(arrayController); } - } catch (Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - SetSound(false); - stoptime = System.nanoTime(); + setSound(false); + stopTime = System.nanoTime(); running = false; } }; sortingThread.start(); } - public static void ReportDistributiveSort(int n) + public static void reportDistributiveSort(int n) { if (sortingThread != null && sortingThread.isAlive()) { @@ -929,25 +928,25 @@ public static void ReportDistributiveSort(int n) try { bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Base for Sort")); - } catch (Exception ex) + } catch (NumberFormatException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } } else { try { bas = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Size of Partitions")); - } catch (Exception ex) + } catch (NumberFormatException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } } } final int base = Math.max(bas, 2); final int num = n; - SetSound(true); + setSound(true); sortingThread = new Thread() { @Override @@ -955,7 +954,7 @@ public void run() { try { - refresharray(); + refreshArray(); Sort sort; switch (num) { @@ -992,12 +991,12 @@ public void run() heading = sort.name(); sort.sort(arrayController); } - } catch (Exception ex) + } catch (InterruptedException e) { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, e); } - SetSound(false); - stoptime = System.nanoTime(); + setSound(false); + stopTime = System.nanoTime(); running = false; } }; diff --git a/src/array/visualizer/SortPrompt.java b/src/array/visualizer/SortPrompt.java index 10ff69f7..7121c6fc 100644 --- a/src/array/visualizer/SortPrompt.java +++ b/src/array/visualizer/SortPrompt.java @@ -25,8 +25,8 @@ public SortPrompt(JFrame f) setAlwaysOnTop(true); setUndecorated(true); initComponents(); - jList2.setListData(ComparativeSorts); - jList1.setListData(DistributiveSorts); + jList2.setListData(comparativeSorts); + jList1.setListData(distributiveSorts); setLocation(f.getX() + (f.getWidth() - getWidth()) / 2, f.getY() + (f.getHeight() - getHeight()) / 2); setVisible(true); } @@ -177,7 +177,7 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { public void run() { - RunAllSorts(); + runAllSorts(); } }.start(); dispose(); @@ -191,7 +191,7 @@ private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) { public void run() { - ReportDistributiveSort(tmp); + reportDistributiveSort(tmp); } }.start(); dispose(); @@ -205,7 +205,7 @@ private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) { public void run() { - ReportComparativeSort(tmp); + reportComparativeSort(tmp); } }.start(); dispose(); diff --git a/src/array/visualizer/sort/BogoSort.java b/src/array/visualizer/sort/BogoSort.java index b2b69ff5..da0272ef 100644 --- a/src/array/visualizer/sort/BogoSort.java +++ b/src/array/visualizer/sort/BogoSort.java @@ -36,11 +36,6 @@ private static void bogoSort(final ArrayController ac) } } - public static void bogobogoSort() - { - - } - @Override public String name() { diff --git a/src/array/visualizer/sort/CountingSort.java b/src/array/visualizer/sort/CountingSort.java index b943a0c3..f6c7716c 100644 --- a/src/array/visualizer/sort/CountingSort.java +++ b/src/array/visualizer/sort/CountingSort.java @@ -16,7 +16,7 @@ public class CountingSort implements Sort { private static void countingSort(final ArrayController ac) { - int max = analyzemax(ac); + int max = analyzeMax(ac); int[] counts = new int[max + 1]; for (int i = 0; i < ac.length; i++) { diff --git a/src/array/visualizer/sort/GravitySort.java b/src/array/visualizer/sort/GravitySort.java index 0d8c8c61..fd6cd01c 100644 --- a/src/array/visualizer/sort/GravitySort.java +++ b/src/array/visualizer/sort/GravitySort.java @@ -16,7 +16,7 @@ public class GravitySort implements Sort { private static void gravitySort(final ArrayController ac) { - int max = analyzemax(ac); + int max = analyzeMax(ac); int[][] abacus = new int[ac.length][max]; for (int i = 0; i < ac.length; i++) { @@ -33,24 +33,23 @@ private static void gravitySort(final ArrayController ac) if (abacus[j][i] == 1) { //Drop it - int droppos = j; - while (droppos + 1 < abacus.length && abacus[droppos][i] == 1) + int dropPos = j; + while (dropPos + 1 < abacus.length && abacus[dropPos][i] == 1) { - droppos++; + dropPos++; } - if (abacus[droppos][i] == 0) + if (abacus[dropPos][i] == 0) { abacus[j][i] = 0; - abacus[droppos][i] = 1; + abacus[dropPos][i] = 1; ac.aa += 2; } } } - int count = 0; for (int x = 0; x < abacus.length; x++) { - count = 0; + int count = 0; for (int y = 0; y < abacus[0].length; y++) { count += abacus[x][y]; diff --git a/src/array/visualizer/sort/MaxHeapSort.java b/src/array/visualizer/sort/MaxHeapSort.java index f6ea19b3..6eedf832 100644 --- a/src/array/visualizer/sort/MaxHeapSort.java +++ b/src/array/visualizer/sort/MaxHeapSort.java @@ -9,7 +9,7 @@ public class MaxHeapSort implements Sort private static int SLP = 1; - private static void maxheapifyrec(final ArrayController ac, int pos, boolean max) + private static void maxHeapifyRec(final ArrayController ac, int pos, boolean max) { if (pos >= ac.length) { @@ -19,8 +19,8 @@ private static void maxheapifyrec(final ArrayController ac, int pos, boolean max int child1 = pos * 2 + 1; int child2 = pos * 2 + 2; - maxheapifyrec(ac, child1, max); - maxheapifyrec(ac, child2, max); + maxHeapifyRec(ac, child1, max); + maxHeapifyRec(ac, child2, max); if (child2 >= ac.length) { @@ -49,11 +49,11 @@ private static void maxheapifyrec(final ArrayController ac, int pos, boolean max if (ac.array[lrg] > ac.array[pos]) { swap(ac, pos, lrg, SLP); - percdwn(ac, lrg, true, ac.length); + percDwn(ac, lrg, true, ac.length); } } - private static void percdwn(final ArrayController ac, int pos, boolean max, int len) + private static void percDwn(final ArrayController ac, int pos, boolean max, int len) { int child1 = pos * 2 + 1; int child2 = pos * 2 + 2; @@ -88,21 +88,21 @@ private static void percdwn(final ArrayController ac, int pos, boolean max, int if (max && (ac.array[child2] > ac.array[pos])) { swap(ac, pos, child2, SLP); - percdwn(ac, child2, max, len); + percDwn(ac, child2, max, len); } else if (!max && (ac.array[child1] < ac.array[pos])) { swap(ac, pos, child1, SLP); - percdwn(ac, child1, max, len); + percDwn(ac, child1, max, len); } } - private static void maxheapsort(final ArrayController ac) + private static void maxHeapSort(final ArrayController ac) { - maxheapifyrec(ac, 0, true); + maxHeapifyRec(ac, 0, true); for (int i = ac.length - 1; i > 0; i--) { swap(ac, 0, i, SLP); - percdwn(ac, 0, true, i); + percDwn(ac, 0, true, i); } } @@ -115,6 +115,6 @@ public String name() @Override public void sort(ArrayController ac) { - maxheapsort(ac); + maxHeapSort(ac); } } diff --git a/src/array/visualizer/sort/MergeSort.java b/src/array/visualizer/sort/MergeSort.java index ef2f3b00..3712c0b3 100644 --- a/src/array/visualizer/sort/MergeSort.java +++ b/src/array/visualizer/sort/MergeSort.java @@ -5,13 +5,9 @@ package array.visualizer.sort; import array.visualizer.ArrayController; -import array.visualizer.ArrayVisualizer; import static array.visualizer.utils.Swaps.*; -import java.util.logging.Level; -import java.util.logging.Logger; - /** * @author S630690 */ @@ -19,32 +15,20 @@ public class MergeSort implements Sort { private static void merge(final ArrayController ac, int min, int max, int mid) { - try + //radixLSDSortNd(2, min, max); + for (int i = min; i <= mid; i++) { - //radixLSDsortnd(2, min, max); - - - int i = min; - while (i <= mid) + if (ac.array[i] > ac.array[mid + 1]) { - if (ac.array[i] > ac.array[mid + 1]) - { - ac.comps++; - swap(ac, i, mid + 1, 1.5); - push(ac, mid + 1, max); - } - i++; + ac.comps++; + swap(ac, i, mid + 1, 1.5); + push(ac, mid + 1, max); } - - } catch (Exception ex) - { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); } } private static void push(final ArrayController ac, int s, int e) { - for (int i = s; i < e; i++) { if (ac.array[i] > ac.array[i + 1]) @@ -53,8 +37,6 @@ private static void push(final ArrayController ac, int s, int e) swap(ac, i, i + 1, 0.0175); } } - - } private static void mergeSort(final ArrayController ac, int min, int max) @@ -71,7 +53,6 @@ private static void mergeSort(final ArrayController ac, int min, int max) } else { int mid = (min + max) / 2;//The midpoint - mergeSort(ac, min, mid);//sort the left side mergeSort(ac, mid + 1, max);//sort the right side merge(ac, min, max, mid);//combines them diff --git a/src/array/visualizer/sort/MinHeapSort.java b/src/array/visualizer/sort/MinHeapSort.java index 06a2265e..88836bef 100644 --- a/src/array/visualizer/sort/MinHeapSort.java +++ b/src/array/visualizer/sort/MinHeapSort.java @@ -5,7 +5,7 @@ public class MinHeapSort implements Sort { - private static void minheapsort(ArrayController ac) + private static void minHeapSort(ArrayController ac) { } @@ -18,6 +18,6 @@ public String name() @Override public void sort(ArrayController ac) { - minheapsort(ac); + minHeapSort(ac); } } diff --git a/src/array/visualizer/sort/RadixLSD.java b/src/array/visualizer/sort/RadixLSD.java index a2283e68..1598840b 100644 --- a/src/array/visualizer/sort/RadixLSD.java +++ b/src/array/visualizer/sort/RadixLSD.java @@ -25,38 +25,15 @@ public RadixLSD(int radix) this.radix = radix; } - public static void radixLSDsort_(final ArrayController ac, int radix) + private static void radixLSDSort(final ArrayController ac, int radix) { - clearmarked(); - int highestpower = analyze(ac, radix); + int highestPower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) { registers[i] = new ArrayList<>(); } - for (int p = 0; p <= highestpower; p++) - { - for (int i = 0; i < ac.length; i++) - { - ac.marked.set(1, i); - sleep(1); - registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); - } - //transcribe(registers, array); - transcribe(registers, ac); - } - } - - private static void radixLSDsort(final ArrayController ac, int radix) - { - clearmarked(); - int highestpower = analyze(ac, radix); - ArrayList[] registers = new ArrayList[radix]; - for (int i = 0; i < radix; i++) - { - registers[i] = new ArrayList<>(); - } - for (int p = 0; p <= highestpower; p++) + for (int p = 0; p <= highestPower; p++) { for (int i = 0; i < ac.length; i++) { @@ -72,23 +49,22 @@ private static void radixLSDsort(final ArrayController ac, int radix) } } - public static void radixLSDsortnd(final ArrayController ac, int radix, int min, int max) + public static void radixLSDSortNd(final ArrayController ac, int radix, int min, int max) { - clearmarked(); - int highestpower = analyze(ac, radix); + int highestPower = analyze(ac, radix); ArrayList[] registers = new ArrayList[radix]; for (int i = 0; i < radix; i++) { registers[i] = new ArrayList<>(); } - for (int p = 0; p <= highestpower; p++) + for (int p = 0; p <= highestPower; p++) { for (int i = min; i < max; i++) { registers[getDigit(ac.array[i], p, radix)].add(ac.array[i]); } //transcribe(registers, array); - transcribend(ac, registers, min); + transcribeNd(ac, registers, min); } } @@ -101,6 +77,6 @@ public String name() @Override public void sort(ArrayController ac) { - radixLSDsort(ac, radix); + radixLSDSort(ac, radix); } } diff --git a/src/array/visualizer/sort/RadixLSDInPlace.java b/src/array/visualizer/sort/RadixLSDInPlace.java index 990cd1c6..e5f4318f 100644 --- a/src/array/visualizer/sort/RadixLSDInPlace.java +++ b/src/array/visualizer/sort/RadixLSDInPlace.java @@ -17,17 +17,16 @@ public RadixLSDInPlace(int radix) private static void inPlaceRadixLSDSort(final ArrayController ac, int radix) { - int pos = 0; int[] vregs = new int[radix - 1]; - int maxpower = analyze(ac, radix); + int maxPower = analyze(ac, radix); double smul = Math.sqrt(radix); - for (int p = 0; p <= maxpower; p++) + for (int p = 0; p <= maxPower; p++) { + int pos = 0; for (int i = 0; i < vregs.length; i++) { vregs[i] = ac.length - 1; } - pos = 0; for (int i = 0; i < ac.length; i++) { int digit = getDigit(ac.array[pos], p, radix); diff --git a/src/array/visualizer/sort/RadixMSD.java b/src/array/visualizer/sort/RadixMSD.java index 3af34515..7f2b43f2 100644 --- a/src/array/visualizer/sort/RadixMSD.java +++ b/src/array/visualizer/sort/RadixMSD.java @@ -26,11 +26,8 @@ public RadixMSD(int radix) private static void radixMSDSort(final ArrayController ac, int radix) { - clearmarked(); - int highestpower = analyze(ac, radix); - int[] tmp = new int[ac.length]; - System.arraycopy(ac.array, 0, tmp, 0, ac.length); - radixMSDRec(ac, 0, ac.length, radix, highestpower); + int highestPower = analyze(ac, radix); + radixMSDRec(ac, 0, ac.length, radix, highestPower); } private static void radixMSDRec(final ArrayController ac, int min, int max, int radix, int pow) @@ -52,7 +49,7 @@ private static void radixMSDRec(final ArrayController ac, int min, int max, int registers[getDigit(ac.array[i], pow, radix)].add(ac.array[i]); ac.aa++; } - transcribermsd(ac, registers, min); + transcribeRmsd(ac, registers, min); int sum = 0; for (ArrayList register : registers) diff --git a/src/array/visualizer/sort/SelectionSort.java b/src/array/visualizer/sort/SelectionSort.java index fa4abdc2..aacc2ce9 100644 --- a/src/array/visualizer/sort/SelectionSort.java +++ b/src/array/visualizer/sort/SelectionSort.java @@ -19,17 +19,17 @@ private static void selectionSort(final ArrayController ac) { for (int i = 0; i < ac.length - 1; i++) { - int lowestindex = i; + int lowestIndex = i; for (int j = i + 1; j < ac.length; j++) { - if (ac.array[j] < ac.array[lowestindex]) + if (ac.array[j] < ac.array[lowestIndex]) { - lowestindex = j; + lowestIndex = j; } sleep(0.01); ac.comps++; } - swap(ac, i, lowestindex); + swap(ac, i, lowestIndex); } } diff --git a/src/array/visualizer/sort/ShellSort.java b/src/array/visualizer/sort/ShellSort.java index 4457367e..75bf6af9 100644 --- a/src/array/visualizer/sort/ShellSort.java +++ b/src/array/visualizer/sort/ShellSort.java @@ -7,9 +7,9 @@ public class ShellSort implements Sort { - private static void shellSort(final ArrayController ac, int gap, int divrate) + private static void shellSort(final ArrayController ac, int gap, int divRate) { - double sleepamt = 1d; + double sleepAmt = 1d; while (gap > 0) { for (int j = 0; j <= gap - 1; j++) @@ -24,7 +24,7 @@ private static void shellSort(final ArrayController ac, int gap, int divrate) { ac.comps++; swap(ac, pos, prev); - sleep(sleepamt); + sleep(sleepAmt); } else { ac.aa += 2; @@ -41,8 +41,8 @@ private static void shellSort(final ArrayController ac, int gap, int divrate) break; } - gap = Math.max(gap / divrate, 1); //Ensure that we do gap 1 - //sleepamt /= divrate; + gap = Math.max(gap / divRate, 1); //Ensure that we do gap 1 + //sleepAmt /= divRate; } } diff --git a/src/array/visualizer/sort/WeaveMerge.java b/src/array/visualizer/sort/WeaveMerge.java index ee0066fa..6a264657 100644 --- a/src/array/visualizer/sort/WeaveMerge.java +++ b/src/array/visualizer/sort/WeaveMerge.java @@ -5,15 +5,11 @@ package array.visualizer.sort; import array.visualizer.ArrayController; -import array.visualizer.ArrayVisualizer; import static array.visualizer.ArrayVisualizer.*; import static array.visualizer.sort.InsertionSort.*; import static array.visualizer.utils.Swaps.*; -import java.util.logging.Level; -import java.util.logging.Logger; - /** * @author S630690 */ @@ -21,31 +17,22 @@ public class WeaveMerge implements Sort { private static void weaveMerge(final ArrayController ac, int min, int max, int mid) { - try + //radixLSDSortNd(2, min, max); + int i = 1; + int target = (mid - min); + while (i <= target) { - //radixLSDsortnd(2, min, max); - - int i = 1; - int target = (mid - min); - while (i <= target) - { - //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); - swapUpTo(ac, mid + i, min + i * 2 - 1, 0.01); - i++; - sleep(1); - } - insertionSort(ac, min, max + 1, 0.15); - //sleep(100); - - } catch (Exception ex) - { - Logger.getLogger(ArrayVisualizer.class.getName()).log(Level.SEVERE, null, ex); + //swapUpTo(mid+(i-min), min+(i-min)*2, 0.01); + swapUpTo(ac, mid + i, min + i * 2 - 1, 0.01); + i++; + sleep(1); } + insertionSort(ac, min, max + 1, 0.15); + //sleep(100); } static void push(final ArrayController ac, int s, int e) { - for (int i = s; i < e; i++) { if (ac.array[i] > ac.array[i + 1]) diff --git a/src/array/visualizer/utils/Analysis.java b/src/array/visualizer/utils/Analysis.java index bfc6c48c..72cf0f6f 100644 --- a/src/array/visualizer/utils/Analysis.java +++ b/src/array/visualizer/utils/Analysis.java @@ -29,20 +29,7 @@ public static int analyze(final ArrayController ac, int base) return a; } - public static int analyze_sneaky(int[] array, int base) - { - int a = 0; - for (int anArray : array) - { - if ((int) (Math.log(anArray) / Math.log(base)) > a) - { - a = (int) (Math.log(anArray) / Math.log(base)); - } - } - return a; - } - - public static int analyzemax(final ArrayController ac) + public static int analyzeMax(final ArrayController ac) { int a = 0; for (int i = 0; i < ac.length; i++) diff --git a/src/array/visualizer/utils/Searches.java b/src/array/visualizer/utils/Searches.java index 076f2ccc..02feaafb 100644 --- a/src/array/visualizer/utils/Searches.java +++ b/src/array/visualizer/utils/Searches.java @@ -29,7 +29,7 @@ public static void linearSearch(final ArrayController ac, int find) sleep(1000); } - public static void binarySearch(final ArrayController ac, int find) throws Exception + public static void binarySearch(final ArrayController ac, int find) { int at = ac.length / 2; int change = ac.length / 4; @@ -38,7 +38,7 @@ public static void binarySearch(final ArrayController ac, int find) throws Excep ac.marked.set(0, ac.array[at]); ac.comps += 2; ac.aa++; - Thread.sleep(1000); + sleep(1000); if (ac.array[at] < find) { at += change; diff --git a/src/array/visualizer/utils/Swaps.java b/src/array/visualizer/utils/Swaps.java index 9a3562ec..88ec57b3 100644 --- a/src/array/visualizer/utils/Swaps.java +++ b/src/array/visualizer/utils/Swaps.java @@ -34,7 +34,7 @@ public static void swap(final ArrayController ac, int i, int j, double pause) ac.array[j] = temp; } - public static void swapnm(final ArrayController ac, int i, int j, double pause) + public static void swapNm(final ArrayController ac, int i, int j, double pause) { sleep(pause); ac.aa += 2; @@ -66,13 +66,13 @@ public static void swapUpToNM(final ArrayController ac, int pos, int to, double { for (int i = pos; i < to; i++) { - swapnm(ac, i, i + 1, pause); + swapNm(ac, i, i + 1, pause); } } else { for (int i = pos; i > to; i--) { - swapnm(ac, i, i - 1, pause); + swapNm(ac, i, i - 1, pause); } } } diff --git a/src/array/visualizer/utils/Transcriptions.java b/src/array/visualizer/utils/Transcriptions.java index ac76324f..edf5138d 100644 --- a/src/array/visualizer/utils/Transcriptions.java +++ b/src/array/visualizer/utils/Transcriptions.java @@ -32,7 +32,7 @@ public static void transcribe(ArrayList[] registers, final ArrayControl } } - public static void transcribenm(ArrayList[] registers, int[] array) + public static void transcribeNm(ArrayList[] registers, int[] array) { int total = 0; for (ArrayList register : registers) @@ -46,7 +46,7 @@ public static void transcribenm(ArrayList[] registers, int[] array) } } - public static void transcribend(final ArrayController ac, ArrayList[] registers, int min) + public static void transcribeNd(final ArrayController ac, ArrayList[] registers, int min) { int total = 0; for (ArrayList register : registers) @@ -63,7 +63,7 @@ public static void transcribend(final ArrayController ac, ArrayList[] r } } - public static void transcribermsd(final ArrayController ac, ArrayList[] registers, int min) + public static void transcribeRmsd(final ArrayController ac, ArrayList[] registers, int min) { int total = 0; for (ArrayList ai : registers) @@ -104,9 +104,9 @@ public static void transcribe(ArrayList[] registers, final ArrayControl public static void fancyTranscribe(final ArrayController ac, ArrayList[] registers) { int[] tmp = new int[ac.length]; - boolean[] tmpwrite = new boolean[ac.length]; + boolean[] tmpWrite = new boolean[ac.length]; int radix = registers.length; - transcribenm(registers, tmp); + transcribeNm(registers, tmp); for (int i = 0; i < tmp.length; i++) { int register = i % radix; @@ -115,22 +115,22 @@ public static void fancyTranscribe(final ArrayController ac, ArrayList[ sleep(radix);//radix } int pos = (int) (((double) register * ((double) tmp.length / radix)) + ((double) i / radix)); - if (!tmpwrite[pos]) + if (!tmpWrite[pos]) { ac.array[pos] = tmp[pos]; ac.aa++; - tmpwrite[pos] = true; + tmpWrite[pos] = true; } ac.marked.set(register, pos); } - for (int i = 0; i < tmpwrite.length; i++) + for (int i = 0; i < tmpWrite.length; i++) { - if (!tmpwrite[i]) + if (!tmpWrite[i]) { ac.array[i] = tmp[i]; ac.aa++; } } - clearmarked(); + clearMarked(); } }