-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortVisualizer.java
More file actions
250 lines (218 loc) · 5.08 KB
/
SortVisualizer.java
File metadata and controls
250 lines (218 loc) · 5.08 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SortVisualizer extends JFrame
{
static SortVisualizer s;
static double[] arr;
static int width = 10;
static int size = 51;
static int maxHeight = 20;
static int padding = 50;
/* in milliseconds */
static int delayBetweenSwap = 26;
/* to show visually, which elements are being inspected */
static int comparingLeft = 0, comparingRight = 0;
static boolean isSorting = false;
static long startSortingTime = 0;
static int sortPerformance = 0;
static Thread t;
SortVisualizer()
{
setLayout(new FlowLayout());
JPanel p = new JPanel();
JButton b1 = new JButton("Shuffle Array");
p.add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
fillArray();
reDraw();
}
});
add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
}
public static void main(String[] args)
{
s = new SortVisualizer();
JPanel sortButtons = new JPanel();
JButton bubbleSort = new JButton("Bubble Sort");
sortButtons.add(bubbleSort);
bubbleSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
t = new Thread()
{
public void run()
{
sortingStarted();
Algorithms.BubbleSort(arr);
comparingLeft = comparingRight = 0;
sortingEnded();
reDraw();
}
};
t.start();
}
});
JButton selectionSort = new JButton("Selection Sort");
sortButtons.add(selectionSort);
selectionSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
t = new Thread()
{
public void run()
{
sortingStarted();
Algorithms.SelectionSort(arr);
comparingLeft = comparingRight = 0;
sortingEnded();
reDraw();
}
};
t.start();
}
});
JButton insertionSort = new JButton("Insertion Sort");
sortButtons.add(insertionSort);
insertionSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
t = new Thread()
{
public void run()
{
sortingStarted();
Algorithms.InsertionSort(arr);
comparingLeft = comparingRight = 0;
sortingEnded();
reDraw();
}
};
t.start();
}
});
JButton mergeSort = new JButton("Merge Sort");
sortButtons.add(mergeSort);
mergeSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
t = new Thread()
{
public void run()
{
sortingStarted();
Algorithms.MergeSort(arr, 0, arr.length - 1);
comparingLeft = comparingRight = 0;
sortingEnded();
reDraw();
}
};
t.start();
}
});
JButton quickSort = new JButton("Quick Sort");
sortButtons.add(quickSort);
quickSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(t != null && t.isAlive())
return;
t = new Thread()
{
public void run()
{
sortingStarted();
Algorithms.QuickSort(arr, 0, arr.length - 1);
comparingLeft = comparingRight = 0;
sortingEnded();
reDraw();
}
};
t.start();
}
});
s.add(sortButtons);
s.setVisible(true);
fillArray();
}
private static void fillArray()
{
arr = new double[size];
for(int i = 0; i < arr.length; i++)
arr[i] = Math.random() * maxHeight;
}
private static void sortingStarted()
{
isSorting = true;
startSortingTime = System.currentTimeMillis();
}
private static void sortingEnded()
{
isSorting = false;
startSortingTime = 0;
}
synchronized static void reDrawWithSleep()
{
reDraw();
sleepMilliseconds(delayBetweenSwap);
}
synchronized static void reDrawWithSleep(int delay)
{
reDraw();
sleepMilliseconds(delay);
}
synchronized static void setActiveElements(int left, int right)
{
comparingLeft = left;
comparingRight = right;
}
synchronized private static void sleepMilliseconds(int delay)
{
long timeStart = System.currentTimeMillis();
while(System.currentTimeMillis() - timeStart < delay);
}
synchronized static void reDraw()
{
Thread t = new Thread()
{
public void run()
{
s.paint(s.getGraphics());
}
};
t.start();
}
public void paint(Graphics g)
{
super.paint(g);
for(int i = 1; i < arr.length; i++)
{
if(i == comparingLeft && comparingLeft != 0)
g.setColor(Color.red);
else if(i == comparingRight && comparingRight != 0)
g.setColor(Color.blue);
else
g.setColor(Color.black);
g.fillRect(padding + i * width - 10, (int)(475 - 20 * arr[i]) + padding, width, (int)(20 * arr[i]));
}
if(isSorting)
sortPerformance = Math.toIntExact(System.currentTimeMillis() - startSortingTime);
g.setFont(new Font("Monospaced", Font.BOLD, 18));
g.setColor(Color.black);
g.drawString("Performance: " + sortPerformance + " milliseconds", 150, 550);
}
}