Skip to content

Commit 8c85dbf

Browse files
committed
InsertionSort example added
1 parent 1a3ebe7 commit 8c85dbf

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

InsertionSort.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.awt.Color;
2+
import java.awt.Dimension;
3+
import java.awt.Graphics;
4+
import java.awt.Graphics2D;
5+
import java.awt.geom.Rectangle2D;
6+
import java.util.Random;
7+
8+
import javax.swing.JFrame;
9+
import javax.swing.JPanel;
10+
import javax.swing.SwingUtilities;
11+
import javax.swing.SwingWorker;
12+
13+
public class InsertionSort extends JPanel {
14+
private final int WIDTH = 1000, HEIGHT = WIDTH * 9 /16;
15+
private final int SIZE = 200; // the number if sorting elements
16+
private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width
17+
private float[] bar_height = new float[SIZE]; // height of bars
18+
private SwingWorker<Void, Void> shuffler, sorter;
19+
private int current_index, traversing_index;
20+
21+
private InsertionSort() {
22+
setBackground(Color.BLACK);
23+
setPreferredSize(new Dimension(WIDTH, HEIGHT));
24+
initBarHeight(); // initialize the height of each bar
25+
initSorter();
26+
initShuffler();
27+
}
28+
29+
@Override
30+
public void paintComponent(Graphics g) {
31+
super.paintComponent(g);
32+
33+
Graphics2D g2d = (Graphics2D)g;
34+
g2d.setColor(Color.CYAN);
35+
Rectangle2D.Float bar;
36+
for(int i = 0; i < SIZE; i++ ) {
37+
bar = new Rectangle2D.Float(i * BAR_WIDTH, 0, BAR_WIDTH, bar_height[i]);
38+
g2d.fill(bar); // g2d.draw(bar);
39+
}
40+
41+
g2d.setColor(Color.RED);
42+
bar = new Rectangle2D.Float(current_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[current_index]);
43+
g2d.fill(bar);
44+
45+
g2d.setColor(Color.GREEN);
46+
bar = new Rectangle2D.Float(traversing_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[traversing_index]);
47+
g2d.fill(bar);
48+
}
49+
50+
public void initBarHeight() {
51+
float interval = (float)HEIGHT / SIZE;
52+
for(int i = 0; i < SIZE; i++) {
53+
bar_height[i] = i * interval;
54+
}
55+
}
56+
57+
private void initSorter() {
58+
sorter = new SwingWorker<>() {
59+
@Override
60+
public Void doInBackground() throws InterruptedException {
61+
for(current_index = 1; current_index < SIZE; current_index++) {
62+
traversing_index = current_index;
63+
while(traversing_index > 0 && bar_height[traversing_index] < bar_height[traversing_index - 1]) {
64+
swap(traversing_index, traversing_index - 1);
65+
traversing_index--;
66+
67+
Thread.sleep(1);
68+
repaint();
69+
}
70+
}
71+
current_index = 0;
72+
traversing_index = 0;
73+
74+
return null;
75+
}
76+
};
77+
}
78+
79+
private void initShuffler() {
80+
shuffler = new SwingWorker<>() {
81+
@Override
82+
public Void doInBackground() throws InterruptedException {
83+
int middle = SIZE / 2;
84+
for (int i = 0, j = middle; i < middle; i++, j++) {
85+
int randow_index = new Random().nextInt(SIZE);
86+
swap(i, randow_index);
87+
88+
randow_index = new Random().nextInt(SIZE);
89+
swap(j, randow_index);
90+
91+
Thread.sleep(10);
92+
repaint();
93+
}
94+
return null;
95+
}
96+
97+
@Override
98+
public void done() {
99+
super.done();
100+
sorter.execute();
101+
}
102+
};
103+
shuffler.execute();
104+
}
105+
106+
private void swap(int indexA, int indexB) {
107+
float temp = bar_height[indexA];
108+
bar_height[indexA] = bar_height[indexB];
109+
bar_height[indexB] = temp;
110+
}
111+
112+
public static void main(String args[]) {
113+
SwingUtilities.invokeLater(() -> {
114+
JFrame frame = new JFrame("Insertion Sort");
115+
frame.setResizable(false);
116+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
117+
frame.setContentPane(new InsertionSort());
118+
frame.validate();
119+
frame.pack();
120+
frame.setLocationRelativeTo(null);
121+
frame.setVisible(true);
122+
});
123+
}
124+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)