forked from Rustam-Z/algorithm-visualizer-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.txt
More file actions
128 lines (109 loc) · 3.93 KB
/
Copy pathInsertionSort.txt
File metadata and controls
128 lines (109 loc) · 3.93 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
package examples;
// Insertion sort visualizer
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class InsertionSort extends JPanel {
private final int WIDTH = 800, HEIGHT = WIDTH * 9 /16;
private final int SIZE = 200; // the number if sorting elements
private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width
private float[] bar_height = new float[SIZE]; // height of bars
private SwingWorker<Void, Void> shuffler, sorter;
private int current_index, traversing_index;
InsertionSort() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initBarHeight(); // initialize the height of each bar
initSorter();
initShuffler();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.CYAN);
Rectangle2D.Float bar;
for(int i = 0; i < SIZE; i++ ) {
bar = new Rectangle2D.Float(i * BAR_WIDTH, 0, BAR_WIDTH, bar_height[i]);
g2d.fill(bar); // g2d.draw(bar);
}
g2d.setColor(Color.RED);
bar = new Rectangle2D.Float(current_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[current_index]);
g2d.fill(bar);
g2d.setColor(Color.GREEN);
bar = new Rectangle2D.Float(traversing_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[traversing_index]);
g2d.fill(bar);
}
public void initBarHeight() {
float interval = (float)HEIGHT / SIZE;
for(int i = 0; i < SIZE; i++) {
bar_height[i] = i * interval;
}
}
public void initSorter() {
sorter = new SwingWorker<>() {
@Override
public Void doInBackground() throws InterruptedException {
for(current_index = 1; current_index < SIZE; current_index++) {
traversing_index = current_index;
while(traversing_index > 0 && bar_height[traversing_index] < bar_height[traversing_index - 1]) {
swap(traversing_index, traversing_index - 1);
traversing_index--;
Thread.sleep(1);
repaint();
}
}
current_index = 0;
traversing_index = 0;
return null;
}
};
}
public void initShuffler() {
shuffler = new SwingWorker<>() {
@Override
public Void doInBackground() throws InterruptedException {
int middle = SIZE / 2;
for (int i = 0, j = middle; i < middle; i++, j++) {
int randow_index = new Random().nextInt(SIZE);
swap(i, randow_index);
randow_index = new Random().nextInt(SIZE);
swap(j, randow_index);
Thread.sleep(10);
repaint();
}
return null;
}
@Override
public void done() {
super.done();
sorter.execute();
}
};
shuffler.execute();
}
public void swap(int indexA, int indexB) {
float temp = bar_height[indexA];
bar_height[indexA] = bar_height[indexB];
bar_height[indexB] = temp;
}
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Insertion Sort");
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new InsertionSort());
frame.validate();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}