-
Notifications
You must be signed in to change notification settings - Fork 173
/
AlgoVisualizer.java
61 lines (43 loc) · 1.52 KB
/
AlgoVisualizer.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
import java.awt.*;
public class AlgoVisualizer {
private static int DELAY = 40;
private InsertionSortData data;
private AlgoFrame frame;
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, InsertionSortData.Type dataType){
// 初始化数据
data = new InsertionSortData(N, sceneHeight, dataType);
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Insertion Sort Visualization", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
this(sceneWidth, sceneHeight, N, InsertionSortData.Type.Default);
}
public void run(){
setData(0, -1);
for( int i = 0 ; i < data.N() ; i ++ ){
setData(i, i);
for(int j = i ; j > 0 && data.get(j) < data.get(j-1) ; j --){
data.swap(j,j-1);
setData(i+1, j-1);
}
}
this.setData(data.N(), -1);
}
private void setData(int orderedIndex, int currentIndex){
data.orderedIndex = orderedIndex;
data.currentIndex = currentIndex;
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, InsertionSortData.Type.NearlyOrdered);
}
}