-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortGui.java
131 lines (97 loc) · 2.39 KB
/
SortGui.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
* Used Rohit Chhatre as a reference
* Used Big JAVA Textbook
* @author
* Soham Dessai
*
*/
public class SortGui extends JComponent{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(800, 1200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SortGui component = new SortGui();
frame.add(component);
frame.setVisible(true);
component.AnimationStart();
}
private SelectionSort selectionSorter;
private BubbleSort bubbleSorter;
private InsertionSort insertionSorter;
public SortGui() {
//creates 3 viewers
int[] array1 = Randomizer();
int[] array2 = array1.clone();
int[] array3 = array1.clone();
bubbleSorter = new BubbleSort(array1, this);
selectionSorter = new SelectionSort(array3, this);
insertionSorter = new InsertionSort(array2, this);
}
public void paintComponent(Graphics g) {
insertionSorter.draw(g);
selectionSorter.draw(g);
bubbleSorter.draw(g);
}
//randomized the array list using shuffle
public int[] Randomizer() {
ArrayList<Integer> nums = new ArrayList<Integer>();
for(int i=1; i<=50; i++) {
nums.add(i);
}
Collections.shuffle(nums);
int[] arr = new int[50];
for(int x=0; x<arr.length; x++) {
arr[x] = nums.get(x);
}
return arr;
}
public void AnimationStart() {
//runs runnable thread for the sorts
class InsertionRunnable implements Runnable {
public void run() {
try {
insertionSorter.sort();
}
catch(InterruptedException e) {
}
}
}
//runs runnable thread for the sorts
class SelectionRunnable implements Runnable {
public void run() {
try {
selectionSorter.sort();
}
catch(InterruptedException e) {
}
}
}
//runs runnable thread for the sorts
class BubbleRunnable implements Runnable {
public void run() {
try {
bubbleSorter.sort();
} catch(InterruptedException e) {
}
}
}
//runnable
Runnable i = new InsertionRunnable();
Thread ti = new Thread(i);
ti.start();
//runnable
Runnable b = new BubbleRunnable();
Thread tb = new Thread(b);
tb.start();
//runnable
Runnable s = new SelectionRunnable();
Thread ts = new Thread(s);
ts.start();
}
}