-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathAlgoVisualizer.java
More file actions
59 lines (44 loc) · 1.47 KB
/
Copy pathAlgoVisualizer.java
File metadata and controls
59 lines (44 loc) · 1.47 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
import java.awt.*;
public class AlgoVisualizer {
private static int DELAY = 10;
private SelectionSortData data;
private AlgoFrame frame;
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
data = new SelectionSortData(N, sceneHeight);
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Selection Sort Visualization", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
private void run(){
frame.render(data);
AlgoVisHelper.pause(DELAY);
for( int i = 0 ; i < data.N() ; i ++ ){
// 寻找[i, n)区间里的最小值的索引
int minIndex = i;
for( int j = i + 1 ; j < data.N() ; j ++ ){
frame.render(data);
AlgoVisHelper.pause(DELAY);
if( data.get(j) < data.get(minIndex) ){
minIndex = j;
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
}
data.swap(i , minIndex);
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
public static void main(String[] args) {
int sceneWidth = 800;
int sceneHeight = 800;
int N = 100;
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
}
}