-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellSortPanel.java
executable file
·95 lines (86 loc) · 3.16 KB
/
ShellSortPanel.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
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
package net.bohush.sorting;
import java.awt.Color;
import java.awt.Graphics;
public class ShellSortPanel extends SortPanel {
private static final long serialVersionUID = 1L;
private int redColumn = -1;
private int blueColumn = -1;
private int greenColumn = -1;
public ShellSortPanel(String name, int sleepTime, int width, int height) {
super(name, sleepTime, width, height);
}
@Override
public void reset() {
redColumn = -1;
blueColumn = -1;
greenColumn = -1;
}
@Override
public void run() {
try {
int increment = list.length / 2;
while (increment > 0) {
for (int i = increment; i < list.length; i++) {
redColumn = i;
int j = i;
int temp = list[i];
repaint();
Thread.sleep(3 * sleepTime);
while (j >= increment && list[j - increment] > temp) {
blueColumn = j - increment;
if(increment == 1) {
greenColumn = blueColumn - 1;
}
repaint();
Thread.sleep(4 * sleepTime);
list[j] = list[j - increment];
j = j - increment;
}
repaint();
Thread.sleep(2 * sleepTime);
list[j] = temp;
}
if (increment == 2) {
increment = 1;
} else {
increment *= (5.0 / 11);
}
}
redColumn = -1;
blueColumn = -1;
greenColumn = size - 1;
} catch (InterruptedException e) {
}
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) {
g.setColor(Color.WHITE);
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
g.setColor(Color.BLACK);
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
}
for (int i = 0; i <= greenColumn; i++) {
g.setColor(Color.GREEN);
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
g.setColor(Color.BLACK);
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
}
if(redColumn != -1) {
g.setColor(Color.RED);
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
g.setColor(Color.BLACK);
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
}
if(blueColumn != -1) {
g.setColor(Color.BLUE);
g.fillRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
g.setColor(Color.BLACK);
g.drawRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
}
}
}