Skip to content

Commit 4e2cb09

Browse files
committed
add shell sort
1 parent 53338c3 commit 4e2cb09

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

src/net/bohush/sorting/CombSortPanel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public void run() {
2626
}
2727
swapped = false;
2828
sorted = true;
29-
System.out.println(gap);
3029
for (int i = 0; i + gap < list.length; i++) {
3130
redColumn = i;
3231
blueColumn = i + gap;

src/net/bohush/sorting/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Main extends JApplet {
99
private static final long serialVersionUID = 1L;
1010
private SortPanel[] sortPanels = new SortPanel[9];
1111
private int size = 100;
12-
private int sleepTime = 1;
12+
private int sleepTime = 2;
1313

1414

1515
public Main() {
@@ -37,7 +37,7 @@ public Main() {
3737
sortPanels[5] = new HeapSortPanel(" Heap Sort ", list, sleepTime);
3838
sortPanels[6] = new CocktailSortPanel(" Cocktail Sort ", list, sleepTime);
3939
sortPanels[7] = new CombSortPanel(" Comb Sort ", list, sleepTime);
40-
sortPanels[8] = new QuickSortPanel(" Quick Sort ", list, sleepTime);
40+
sortPanels[8] = new ShellSortPanel(" Shell Sort ", list, sleepTime);
4141

4242

4343
for (int i = 0; i < sortPanels.length; i++) {
@@ -50,7 +50,7 @@ public static void main(String[] args) {
5050
JFrame frame = new JFrame("Sorting Algorithm Animations");
5151
JApplet applet = new Main();
5252
frame.add(applet);
53-
frame.setUndecorated(true);
53+
//frame.setUndecorated(true);
5454
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5555
frame.pack();
5656
frame.setLocationRelativeTo(null);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package net.bohush.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class ShellSortPanel extends SortPanel {
7+
private static final long serialVersionUID = 1L;
8+
private int redColumn = -1;
9+
private int blueColumn = -1;
10+
private int greenColumn = -1;
11+
12+
public ShellSortPanel(String name, int[] list, int sleepTime) {
13+
super(name, list, sleepTime);
14+
}
15+
16+
@Override
17+
public void run() {
18+
try {
19+
20+
int increment = list.length / 2;
21+
while (increment > 0) {
22+
for (int i = increment; i < list.length; i++) {
23+
redColumn = i;
24+
int j = i;
25+
int temp = list[i];
26+
repaint();
27+
Thread.sleep(3 * sleepTime);
28+
while (j >= increment && list[j - increment] > temp) {
29+
blueColumn = j - increment;
30+
if(increment == 1) {
31+
greenColumn = blueColumn - 1;
32+
}
33+
repaint();
34+
Thread.sleep(4 * sleepTime);
35+
list[j] = list[j - increment];
36+
j = j - increment;
37+
}
38+
repaint();
39+
Thread.sleep(2 * sleepTime);
40+
list[j] = temp;
41+
}
42+
if (increment == 2) {
43+
increment = 1;
44+
} else {
45+
increment *= (5.0 / 11);
46+
}
47+
48+
}
49+
redColumn = -1;
50+
blueColumn = -1;
51+
greenColumn = size - 1;
52+
} catch (InterruptedException e) {
53+
}
54+
repaint();
55+
}
56+
57+
@Override
58+
protected void paintComponent(Graphics g) {
59+
super.paintComponent(g);
60+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
61+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
62+
for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) {
63+
g.setColor(Color.WHITE);
64+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
65+
g.setColor(Color.BLACK);
66+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
67+
}
68+
for (int i = 0; i <= greenColumn; i++) {
69+
g.setColor(Color.GREEN);
70+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
71+
g.setColor(Color.BLACK);
72+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
73+
}
74+
if(redColumn != -1) {
75+
g.setColor(Color.RED);
76+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
77+
g.setColor(Color.BLACK);
78+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
79+
}
80+
if(blueColumn != -1) {
81+
g.setColor(Color.BLUE);
82+
g.fillRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
83+
g.setColor(Color.BLACK);
84+
g.drawRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
85+
}
86+
}
87+
88+
}

src/net/bohush/sorting/SortPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public abstract class SortPanel extends JPanel implements Runnable {
1212
private static final long serialVersionUID = 1L;
1313
protected static final int BORDER_WIDTH = 10;
14-
private static final Dimension PREFFERED_DIMENSION = new Dimension(640, 360);
14+
private static final Dimension PREFFERED_DIMENSION = new Dimension(440, 340);
1515
protected int size;
1616
protected int[] list;
1717
protected int sleepTime;

0 commit comments

Comments
 (0)