11package array .visualizer ;
22
3- import array .visualizer .sort .RadixLSDInPlace ;
3+ import array .visualizer .sort .* ;
44
55import java .awt .Color ;
66import java .awt .Font ;
1313import javax .swing .JFrame ;
1414
1515import static array .visualizer .utils .Swaps .*;
16- import static array .visualizer .sort .BubbleSort .*;
17- import static array .visualizer .sort .CocktailShaker .*;
18- import static array .visualizer .sort .CountingSort .*;
19- import static array .visualizer .sort .DoubleSelection .*;
20- import static array .visualizer .sort .GravitySort .*;
21- import static array .visualizer .sort .InsertionSort .*;
22- import static array .visualizer .sort .MergeSort .*;
23- import static array .visualizer .sort .MergeSortOOP .*;
24- import static array .visualizer .sort .QuickSort .*;
25- import static array .visualizer .sort .RadixLSD .*;
26- import static array .visualizer .sort .RadixMSD .*;
27- import static array .visualizer .sort .SelectionSort .*;
28- import static array .visualizer .sort .ShatterSorts .*;
29- import static array .visualizer .sort .TimeSort .*;
30- import static array .visualizer .sort .WeaveMerge .*;
31- import static array .visualizer .sort .RadixLSDInPlace .*;
32- import static array .visualizer .sort .BogoSort .*;
33- import static array .visualizer .sort .HeapSort .*;
34- import static array .visualizer .sort .ShellSort .*;
3516import java .awt .BasicStroke ;
3617import java .awt .Graphics2D ;
3718import java .awt .Polygon ;
38- import static java .lang .Thread .sleep ;
3919import java .util .logging .Level ;
4020import java .util .logging .Logger ;
4121import javax .sound .midi .Instrument ;
@@ -119,7 +99,9 @@ public static void sleep(double milis){
11999 addamt -=(double )actual /1000000.0 ;
120100 if (running )
121101 sleeptime +=actual ;
122- }catch (Throwable t ){}
102+ }catch (Exception ex ){
103+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
104+ }
123105 }
124106
125107 public static void main (String [] args ) throws Exception {
@@ -202,7 +184,9 @@ public void run()
202184 double tmpd = (array[Math.min(Math.max(i, 0), array.length-1)]/32.0+47);
203185 chan.setPitchBend(8192*2-(int)((tmpd-Math.floor(tmpd))*8192*2));
204186 }while(false);}*/
205- try {sleep (1 );}catch (Exception e ){}
187+ try {sleep (1 );}catch (Exception ex ){
188+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
189+ }
206190 }
207191 }
208192 }.start ();
@@ -657,97 +641,34 @@ public synchronized static void RunAllSorts(){
657641 @ Override
658642 public void run (){
659643 try {
660-
661- refresharray ();
662- heading = "Selection Sort" ;
663- selectionSort (arrayController );
664-
665- chan .allNotesOff ();
666- refresharray ();
667- heading = "Bubble Sort" ;
668- bubbleSort (arrayController );
669-
670- chan .allNotesOff ();
671- refresharray ();
672- heading = "Insertion Sort" ;
673- insertionSort (arrayController );
674-
675- chan .allNotesOff ();
676- refresharray ();
677- heading = "Cocktail Shaker Sort" ;
678- cocktailShakerSort (arrayController );
679-
680- chan .allNotesOff ();
681- refresharray ();
682- heading = "Double Selection Sort" ;
683- doubleSelectionSort (arrayController );
684-
685- chan .allNotesOff ();
686- refresharray ();
687- heading = "Shell Sort" ;
688- shellSort (arrayController , arrayController .length , 2 );
689-
690- chan .allNotesOff ();
691- refresharray ();
692- heading = "Merge Sort" ;
693- mergeSortOP (arrayController );
694-
695- chan .allNotesOff ();
696- refresharray ();
697- heading = "Merge Sort In-Place" ;
698- mergeSort (arrayController , 0 , arrayController .length - 1 );
699-
700- chan .allNotesOff ();
701- refresharray ();
702- heading = "Merge+Insertion Sort" ;
703- weaveMergeSort (arrayController , 0 , arrayController .length -1 );
704-
705- chan .allNotesOff ();
706- refresharray ();
707- heading = "Max Heap Sort" ;
708- maxheapsort (arrayController );
709-
710- chan .allNotesOff ();
711- refresharray ();
712- heading = "Quick Sort" ;
713- quickSort (arrayController , 0 , arrayController .length -1 );
714-
715- chan .allNotesOff ();
716- refresharray ();
717- heading = "Counting Sort" ;
718- countingSort (arrayController );
719-
720- chan .allNotesOff ();
721- refresharray ();
722- heading = "Time+Insertion Sort (Mul 4)" ;
723- timeSort (arrayController , 4 );
724-
725- chan .allNotesOff ();
726- refresharray ();
727- heading = "Gravity Sort" ;
728- gravitySort (arrayController );
729-
730- chan .allNotesOff ();
731- refresharray ();
732- heading = "Radix LSD Sort (Base 4)" ;
733- radixLSDsort (arrayController , 4 );
734-
735- chan .allNotesOff ();
736- refresharray ();
737- heading = "Radix MSD Sort (Base 4)" ;
738- radixMSDSort (arrayController , 4 );
739-
740- chan .allNotesOff ();
741- refresharray ();
742- heading = "Radix LSD In-Place Sort (Base 2)" ;
743- inPlaceRadixLSDSort (arrayController , 2 );
744-
745- chan .allNotesOff ();
746- refresharray ();
747- heading = "Radix LSD In-Place Sort (Base 10)" ;
748- inPlaceRadixLSDSort (arrayController , 10 );
749-
750- }catch (Exception e ){}
644+ for (Sort sort : new Sort []{
645+ new SelectionSort (),
646+ new BubbleSort (),
647+ new InsertionSort (),
648+ new CocktailShaker (),
649+ new ShellSort (),
650+ new MergeSortOOP (),
651+ new MergeSort (),
652+ new WeaveMerge (),
653+ new MaxHeapSort (),
654+ new QuickSort (),
655+ new CountingSort (),
656+ new TimeSort (4 ),
657+ new GravitySort (),
658+ new RadixLSD (4 ),
659+ new RadixMSD (4 ),
660+ new RadixLSDInPlace (2 ),
661+ new RadixLSDInPlace (10 )}
662+ )
663+ {
664+ chan .allNotesOff ();
665+ refresharray ();
666+ heading = sort .name ();
667+ sort .sort (arrayController );
668+ }
669+ }catch (Exception ex ){
670+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
671+ }
751672 SetSound (false );
752673 stoptime = System .nanoTime ();
753674 running = false ;
@@ -768,34 +689,44 @@ public static void ReportComparativeSort(int n){
768689 @ Override
769690 public void run (){
770691 try {
771-
772692 refresharray ();
773- heading = ComparativeSorts [num ]+" Sort" ;
774- switch (num ){
775- case 0 :
776- selectionSort (arrayController );break ;
777- case 1 :
778- bubbleSort (arrayController );break ;
779- case 2 :
780- insertionSort (arrayController );break ;
781- case 3 :
782- doubleSelectionSort (arrayController );break ;
783- case 4 :
784- cocktailShakerSort (arrayController );break ;
785- case 5 :
786- quickSort (arrayController , 0 , arrayController .length -1 );break ;
787- case 6 :
788- mergeSort (arrayController , 0 , arrayController .length -1 );break ;
789- case 7 :
790- mergeSortOP (arrayController );break ;
791- case 8 :
792- weaveMergeSort (arrayController , 0 , arrayController .length -1 );break ;
793- case 9 :
794- maxheapsort (arrayController );break ;
795- case 10 :
796- shellSort (arrayController , arrayController .length , 2 );break ;
693+ Sort sort ;
694+ switch (num )
695+ {
696+ case 0 :
697+ sort = new SelectionSort ();break ;
698+ case 1 :
699+ sort = new BubbleSort ();break ;
700+ case 2 :
701+ sort = new InsertionSort ();break ;
702+ case 3 :
703+ sort = new DoubleSelection ();break ;
704+ case 4 :
705+ sort = new CocktailShaker ();break ;
706+ case 5 :
707+ sort = new QuickSort ();break ;
708+ case 6 :
709+ sort = new MergeSort ();break ;
710+ case 7 :
711+ sort = new MergeSortOOP ();break ;
712+ case 8 :
713+ sort = new WeaveMerge ();break ;
714+ case 9 :
715+ sort = new MaxHeapSort ();break ;
716+ case 10 :
717+ sort = new ShellSort ();break ;
718+ default :
719+ sort = null ; break ;
720+ }
721+ if (sort != null )
722+ {
723+ heading = sort .name ();
724+ sort .sort (arrayController );
725+ }
726+ }catch (Exception ex )
727+ {
728+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
797729 }
798- }catch (Exception e ){e .printStackTrace ();}
799730 SetSound (false );
800731 stoptime = System .nanoTime ();
801732 running = false ;
@@ -810,9 +741,13 @@ public static void ReportDistributiveSort(int n){
810741 int bas = 10 ;
811742 if (n != 3 && n != 5 && n != 7 )
812743 if (n != 4 )
813- try {bas = Integer .parseInt (JOptionPane .showInputDialog (null , "Enter Base for Sort" ));}catch (Exception e ){}
744+ try {bas = Integer .parseInt (JOptionPane .showInputDialog (null , "Enter Base for Sort" ));}catch (Exception ex ){
745+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
746+ }
814747 else
815- try {bas = Integer .parseInt (JOptionPane .showInputDialog (null , "Enter Size of Partitions" ));}catch (Exception e ){}
748+ try {bas = Integer .parseInt (JOptionPane .showInputDialog (null , "Enter Size of Partitions" ));}catch (Exception ex ){
749+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
750+ }
816751
817752 final int base = Math .max (bas , 2 );
818753 final int num = n ;
@@ -822,26 +757,35 @@ public static void ReportDistributiveSort(int n){
822757 public void run (){
823758 try {
824759 refresharray ();
825- heading = DistributiveSorts [num ]+" Sort" ;
826- switch (num ){
827- case 0 :
828- radixLSDsort (arrayController , base );break ;
829- case 1 :
830- radixMSDSort (arrayController , base );break ;
831- case 2 :
832- RadixLSDInPlace .inPlaceRadixLSDSort (arrayController , base );break ;
833- case 3 :
834- gravitySort (arrayController );break ;
835- case 4 :
836- shatterSort (arrayController , base );break ;
837- case 5 :
838- countingSort (arrayController );break ;
839- case 6 :
840- timeSort (arrayController , base );break ;
841- case 7 :
842- bogoSort (arrayController );break ;
760+ Sort sort ;
761+ switch (num ) {
762+ case 0 :
763+ sort = new RadixLSD (base );break ;
764+ case 1 :
765+ sort = new RadixMSD (base );break ;
766+ case 2 :
767+ sort = new RadixLSDInPlace (base );break ;
768+ case 3 :
769+ sort = new GravitySort ();break ;
770+ case 4 :
771+ sort = new ShatterSorts (base );break ;
772+ case 5 :
773+ sort = new CountingSort ();break ;
774+ case 6 :
775+ sort = new TimeSort (base );break ;
776+ case 7 :
777+ sort = new BogoSort ();break ;
778+ default :
779+ sort = null ; break ;
780+ }
781+ if (sort != null )
782+ {
783+ heading = sort .name ();
784+ sort .sort (arrayController );
785+ }
786+ }catch (Exception ex ){
787+ Logger .getLogger (ArrayVisualizer .class .getName ()).log (Level .SEVERE , null , ex );
843788 }
844- }catch (Exception e ){}
845789 SetSound (false );
846790 stoptime = System .nanoTime ();
847791 running = false ;
0 commit comments