Skip to content

Commit da90a3d

Browse files
committed
Update sorts to inherit common interface
1 parent b8954a9 commit da90a3d

27 files changed

+465
-210
lines changed

src/array/visualizer/ArrayController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ArrayController(int length)
1919
comps = 0;
2020
}
2121

22-
void clearMarked()
22+
public void clearMarked()
2323
{
2424
for(int i = 0; i < length; i++)
2525
marked.set(i, -5);

src/array/visualizer/ArrayVisualizer.java

Lines changed: 105 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package array.visualizer;
22

3-
import array.visualizer.sort.RadixLSDInPlace;
3+
import array.visualizer.sort.*;
44

55
import java.awt.Color;
66
import java.awt.Font;
@@ -13,29 +13,9 @@
1313
import javax.swing.JFrame;
1414

1515
import 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.*;
3516
import java.awt.BasicStroke;
3617
import java.awt.Graphics2D;
3718
import java.awt.Polygon;
38-
import static java.lang.Thread.sleep;
3919
import java.util.logging.Level;
4020
import java.util.logging.Logger;
4121
import 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;

src/array/visualizer/UtilFrame.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import static array.visualizer.ArrayVisualizer.*;
88
import java.awt.Toolkit;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
911
import javax.swing.JFrame;
1012
import javax.swing.JOptionPane;
1113

src/array/visualizer/sort/BitonicSort.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
*
1111
* @author S630690
1212
*/
13-
public class BitonicSort {
13+
public class BitonicSort implements Sort
14+
{
1415
public static void bitonicSort(final ArrayController ac){
1516
bitonicMerge(ac, 0, ac.length, true);
1617
}
@@ -28,4 +29,16 @@ public static void bitonicMerge(final ArrayController ac, int start, int end, bo
2829
if(dir)
2930
for(int i = 0; i < end-start; i++);
3031
}
32+
33+
@Override
34+
public String name()
35+
{
36+
return "Bitonic Sort";
37+
}
38+
39+
@Override
40+
public void sort(ArrayController ac)
41+
{
42+
bitonicSort(ac);
43+
}
3144
}

src/array/visualizer/sort/BogoSort.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static array.visualizer.ArrayVisualizer.*;
66
import static array.visualizer.utils.Swaps.*;
77

8-
public class BogoSort {
8+
public class BogoSort implements Sort {
99
public static boolean bogoIsSorted(final ArrayController ac){
1010
for(int i = 1; i < ac.length; i++){
1111
ac.comps++;
@@ -31,4 +31,16 @@ public static void bogoSort(final ArrayController ac){
3131
public static void bogobogoSort(){
3232

3333
}
34+
35+
@Override
36+
public String name()
37+
{
38+
return "Bogo Sort";
39+
}
40+
41+
@Override
42+
public void sort(ArrayController ac)
43+
{
44+
bogoSort(ac);
45+
}
3446
}

0 commit comments

Comments
 (0)