Skip to content

Commit 7eaae51

Browse files
committed
add cocktail sort
1 parent 85f423e commit 7eaae51

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package net.bohush.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class CocktailSortPanel extends SortPanel {
7+
private static final long serialVersionUID = 1L;
8+
private int redColumn = -1;
9+
private int greenColumn1 = -1;
10+
private int greenColumn2 = -1;
11+
12+
public CocktailSortPanel(String name, int[] list, int sleepTime) {
13+
super(name, list, sleepTime);
14+
}
15+
16+
@Override
17+
public void run() {
18+
try {
19+
boolean swapped = true;
20+
int i = 0;
21+
int j = list.length - 1;
22+
while (i < j && swapped) {
23+
swapped = false;
24+
for (int k = i; k < j; k++) {
25+
redColumn = k;
26+
repaint();
27+
Thread.sleep(4 * sleepTime);
28+
if (list[k] > list[k + 1]) {
29+
repaint();
30+
Thread.sleep(4 * sleepTime);
31+
int temp = list[k];
32+
list[k] = list[k + 1];
33+
list[k + 1] = temp;
34+
swapped = true;
35+
}
36+
}
37+
greenColumn2 = j;
38+
j--;
39+
if (swapped) {
40+
swapped = false;
41+
for (int k = j; k > i; k--) {
42+
redColumn = k;
43+
repaint();
44+
Thread.sleep(4 * sleepTime);
45+
if (list[k] < list[k - 1]) {
46+
repaint();
47+
Thread.sleep(4 * sleepTime);
48+
int temp = list[k];
49+
list[k] = list[k - 1];
50+
list[k - 1] = temp;
51+
swapped = true;
52+
}
53+
}
54+
}
55+
i++;
56+
greenColumn1 = i;
57+
}
58+
redColumn = -1;
59+
greenColumn1 = greenColumn2;
60+
} catch (InterruptedException e) {
61+
}
62+
repaint();
63+
}
64+
65+
@Override
66+
protected void paintComponent(Graphics g) {
67+
super.paintComponent(g);
68+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
69+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
70+
for (int i = (greenColumn1 == -1 ? 0 : greenColumn1); i < (greenColumn2 == -1 ? list.length : greenColumn2); i++) {
71+
g.setColor(Color.WHITE);
72+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
73+
g.setColor(Color.BLACK);
74+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
75+
}
76+
if(greenColumn2 != -1) {
77+
for (int i = greenColumn2; i < list.length; i++) {
78+
g.setColor(Color.GREEN);
79+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
80+
g.setColor(Color.BLACK);
81+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
82+
}
83+
}
84+
if(greenColumn1 != -1) {
85+
for (int i = 0; i < greenColumn1; i++) {
86+
g.setColor(Color.GREEN);
87+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
88+
g.setColor(Color.BLACK);
89+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
90+
}
91+
}
92+
if(redColumn != -1) {
93+
g.setColor(Color.RED);
94+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
95+
g.setColor(Color.BLACK);
96+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
97+
}
98+
}
99+
100+
}

src/net/bohush/sorting/Main.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class Main extends JApplet {
88

99
private static final long serialVersionUID = 1L;
10-
private SortPanel[] sortPanels = new SortPanel[6];
10+
private SortPanel[] sortPanels = new SortPanel[9];
1111
private int size = 100;
1212
private int sleepTime = 1;
1313

@@ -17,7 +17,11 @@ public Main() {
1717
int[] list = new int[size];
1818
for (int i = 0; i < list.length; i++) {
1919
list[i] = i + 1;
20+
//list[i] = size - i;
21+
//list[i] = (size / 4) * (int)(1 + Math.random() * 4);
22+
//list[i] = (size / 2);
2023
}
24+
//list[(int)(Math.random() * size)] = size;
2125
//shuffle
2226
for (int i = 0; i < list.length; i++) {
2327
int index = (int) (Math.random() * list.length);
@@ -31,10 +35,10 @@ public Main() {
3135
sortPanels[3] = new QuickSortPanel(" Quick Sort ", list, sleepTime);
3236
sortPanels[4] = new MergeSortPanel(" Merge Sort ", list, sleepTime);
3337
sortPanels[5] = new HeapSortPanel(" Heap Sort ", list, sleepTime);
34-
/*sortPanels[6] = new QuickSortPanel(" Quick Sort ", list, sleepTime);
38+
sortPanels[6] = new CocktailSortPanel(" Cocktail sort ", list, sleepTime);
3539
sortPanels[7] = new QuickSortPanel(" Quick Sort ", list, sleepTime);
3640
sortPanels[8] = new QuickSortPanel(" Quick Sort ", list, sleepTime);
37-
*/
41+
3842

3943
for (int i = 0; i < sortPanels.length; i++) {
4044
add(sortPanels[i]);

src/net/bohush/sorting/MergeSortPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void merge(int start1, int fin1, int start2, int fin2) throws Interrupted
4444
int current1 = 0;
4545
redColumn = start1 + current1;
4646
int current2 = 0;
47-
blueColumn = start2 + current2 - 1;
47+
blueColumn = start2 + current2;
4848
int current3 = 0;
4949

5050
while (current1 < list1.length && current2 < list2.length) {

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, 540);
14+
private static final Dimension PREFFERED_DIMENSION = new Dimension(640, 360);
1515
protected int size;
1616
protected int[] list;
1717
protected int sleepTime;

0 commit comments

Comments
 (0)