Skip to content

Commit 4401c8b

Browse files
committed
Added Radix and Gnome sort
1 parent a200d84 commit 4401c8b

File tree

5 files changed

+215
-5
lines changed

5 files changed

+215
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Sorting Algorithms
33

44
![](screenshot.png)
55

6-
Visualization and comparison of 9 different sorting algorithms:
6+
Visualization and comparison of 11 different sorting algorithms:
77
- selection sort
88
- shell sort
99
- insertion sort
@@ -13,6 +13,8 @@ Visualization and comparison of 9 different sorting algorithms:
1313
- bubble sort
1414
- comb sort
1515
- cocktail sort
16+
- gnome sort
17+
- radix sort
1618

1719
The algorithms are run using 4 types of input data:
1820
- random

screenshot.png

-153 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package net.mechanika.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class GnomeSortPanel extends SortPanel {
7+
private static final long serialVersionUID = 1L;
8+
private int redColumn = -1;
9+
private int greenColumn = -1;
10+
11+
public GnomeSortPanel(String name, int sleepTime, int width, int height) {
12+
super(name, sleepTime, width, height);
13+
}
14+
15+
@Override
16+
public void reset() {
17+
redColumn = -1;
18+
greenColumn = 1;
19+
}
20+
21+
@Override
22+
public void run() {
23+
try {
24+
for (int i = 1; i < list.length;) {
25+
if (list[i - 1] <= list[i]) {
26+
if (greenColumn <= ++i) {
27+
greenColumn = (i < list.length) ? i : i - 1;
28+
redColumn = -1;
29+
repaint();
30+
}
31+
} else {
32+
Thread.sleep(3 * sleepTime);
33+
repaint();
34+
redColumn = i;
35+
repaint();
36+
Thread.sleep(4 * sleepTime);
37+
int tempVal = list[i];
38+
list[i] = list[i - 1];
39+
list[i - 1] = tempVal;
40+
--i;
41+
if (i == 0) {
42+
redColumn = -1;
43+
i = 1;
44+
}
45+
}
46+
repaint();
47+
}
48+
redColumn = -1;
49+
} catch (InterruptedException e) {
50+
}
51+
repaint();
52+
}
53+
54+
@Override
55+
protected void paintComponent(Graphics g) {
56+
super.paintComponent(g);
57+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
58+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
59+
for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) {
60+
g.setColor(Color.WHITE);
61+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
62+
g.setColor(Color.BLACK);
63+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
64+
}
65+
for (int i = 0; i <= greenColumn; i++) {
66+
g.setColor(Color.GREEN);
67+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
68+
g.setColor(Color.BLACK);
69+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
70+
}
71+
if(redColumn != -1) {
72+
g.setColor(Color.RED);
73+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
74+
g.setColor(Color.BLACK);
75+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
76+
}
77+
}
78+
}

src/net/mechanika/sorting/Main.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
public class Main extends JApplet {
1111

1212
private static final long serialVersionUID = 1L;
13-
private SortPanel[] sortPanels = new SortPanel[9];
14-
private static int size = 100;
13+
private SortPanel[] sortPanels = new SortPanel[11];
14+
private static int size = 200;
1515
private int sleepTime = 2;
1616
private String animationName = "";
1717

1818
public Main() {
1919
setLayout(new GridLayout(1, 1, 0, 0));
2020
SortPanelsHolder sortPanelHolder = new SortPanelsHolder();
21-
sortPanelHolder.setLayout(new GridLayout(0, 3, 0, 0));
21+
sortPanelHolder.setLayout(new GridLayout(0, 4, 0, 0));
2222
sortPanelHolder.setBackground(Color.BLACK);
2323
sortPanelHolder.setForeground(Color.BLACK);
2424
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
25-
int width = screenSize.width / 3;
25+
int width = screenSize.width / 4;
2626
int height = screenSize.height / 3;
2727
sortPanels[0] = new SelectionSortPanel(" Selection Sort ", sleepTime, width, height);
2828
sortPanels[1] = new ShellSortPanel(" Shell Sort ", sleepTime, width, height);
@@ -33,6 +33,8 @@ public Main() {
3333
sortPanels[6] = new BubbleSortPanel(" Bubble Sort ", sleepTime, width, height);
3434
sortPanels[7] = new CombSortPanel(" Comb Sort ", sleepTime, width, height);
3535
sortPanels[8] = new CocktailSortPanel(" Cocktail Sort ", sleepTime, width, height);
36+
sortPanels[9] = new RadixSortPanel(" Radix Sort ", sleepTime, width, height);
37+
sortPanels[10] = new GnomeSortPanel(" Gnome Sort ", sleepTime, width, height);
3638

3739
for (int i = 0; i < sortPanels.length; i++) {
3840
sortPanels[i].setVisible(false);
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package net.mechanika.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class RadixSortPanel extends SortPanel {
9+
private static final long serialVersionUID = 1L;
10+
private int redColumn = -1;
11+
private int blueColumn = -1;
12+
private int cyanColumn = -1;
13+
private int greenColumn = -1;
14+
final int RADIX = 10;
15+
16+
public RadixSortPanel(String name, int sleepTime, int width, int height) {
17+
super(name, sleepTime, width, height);
18+
}
19+
20+
@Override
21+
public void reset() {
22+
redColumn = -1;
23+
blueColumn = -1;
24+
greenColumn = -1;
25+
cyanColumn = -1;
26+
}
27+
28+
@Override
29+
public void run() {
30+
try {
31+
32+
boolean maxLength = false;
33+
int tmp = -1, placement = 1, digit;
34+
35+
List<Integer>[] bucket = new ArrayList[RADIX];
36+
for (int i = 0; i < bucket.length; i++) {
37+
bucket[i] = new ArrayList<Integer>();
38+
}
39+
40+
while (!maxLength) {
41+
maxLength = true;
42+
43+
for (int i = 0; i < list.length; i++) {
44+
tmp = list[i] / placement;
45+
digit = tmp % 10;
46+
bucket[digit].add(list[i]);
47+
if (maxLength && tmp > 0) {
48+
maxLength = false;
49+
}
50+
// cyanColumn = (i % 10) == 0 ? i : -1;
51+
// repaint();
52+
// Thread.sleep(2 * sleepTime);
53+
}
54+
55+
// System.out.println("second loop");
56+
57+
int a = 0;
58+
for (int b = 0; b < RADIX; b++) {
59+
for (Integer i : bucket[b]) {
60+
// list[a++] = count[b][i];
61+
redColumn = a;
62+
if (maxLength) {
63+
greenColumn = a;
64+
}
65+
list[a++] = i;
66+
repaint();
67+
Thread.sleep(2 * sleepTime);
68+
}
69+
bucket[b].clear();
70+
}
71+
// greenColumn = a;
72+
// greenColumn--;
73+
redColumn = -1;
74+
repaint();
75+
Thread.sleep(4 * sleepTime);
76+
77+
placement *= RADIX;
78+
}
79+
80+
// for (int i = 0; i < list.length; i++) {
81+
// greenColumn = i;
82+
// repaint();
83+
// Thread.sleep(sleepTime);
84+
// }
85+
redColumn = -1;
86+
cyanColumn = -1;
87+
} catch (InterruptedException e) {
88+
}
89+
repaint();
90+
}
91+
92+
@Override
93+
protected void paintComponent(Graphics g) {
94+
super.paintComponent(g);
95+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
96+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
97+
for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) {
98+
g.setColor(Color.WHITE);
99+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
100+
g.setColor(Color.BLACK);
101+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
102+
}
103+
for (int i = 0; i <= greenColumn; i++) {
104+
g.setColor(Color.GREEN);
105+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
106+
g.setColor(Color.BLACK);
107+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
108+
}
109+
if(redColumn != -1) {
110+
g.setColor(Color.RED);
111+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
112+
g.setColor(Color.BLACK);
113+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
114+
}
115+
if(blueColumn != -1) {
116+
g.setColor(Color.BLUE);
117+
g.fillRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
118+
g.setColor(Color.BLACK);
119+
g.drawRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
120+
}
121+
if(cyanColumn != -1) {
122+
g.setColor(Color.CYAN);
123+
g.fillRect(2 * BORDER_WIDTH + columnWidth * cyanColumn, getHeight() - list[cyanColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[cyanColumn] * columnHeight);
124+
g.setColor(Color.BLACK);
125+
g.drawRect(2 * BORDER_WIDTH + columnWidth * cyanColumn, getHeight() - list[cyanColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[cyanColumn] * columnHeight);
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)