Skip to content

Commit e8d11c4

Browse files
committed
Initial commit
1 parent 8ca3e57 commit e8d11c4

14 files changed

+1244
-0
lines changed

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff:
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/dictionaries
8+
9+
# Sensitive or high-churn files:
10+
.idea/**/dataSources/
11+
.idea/**/dataSources.ids
12+
.idea/**/dataSources.xml
13+
.idea/**/dataSources.local.xml
14+
.idea/**/sqlDataSources.xml
15+
.idea/**/dynamic.xml
16+
.idea/**/uiDesigner.xml
17+
18+
# Gradle:
19+
.idea/**/gradle.xml
20+
.idea/**/libraries
21+
22+
# CMake
23+
cmake-build-debug/
24+
25+
# Mongo Explorer plugin:
26+
.idea/**/mongoSettings.xml
27+
28+
## File-based project format:
29+
*.iws
30+
31+
## Plugin-specific files:
32+
33+
# IntelliJ
34+
out/
35+
.idea/
36+
*.iml
37+
38+
# mpeltonen/sbt-idea plugin
39+
.idea_modules/
40+
41+
# JIRA plugin
42+
atlassian-ide-plugin.xml
43+
44+
# Cursive Clojure plugin
45+
.idea/replstate.xml
46+
47+
# Crashlytics plugin (for Android Studio and IntelliJ)
48+
com_crashlytics_export_strings.xml
49+
crashlytics.properties
50+
crashlytics-build.properties
51+
fabric.properties

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Warren Galyen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Sorting Algorithms
2+
==========================
3+
4+
5+
6+
Visualization and comparison of 9 different sorting algorithms:
7+
- selection sort
8+
- shell sort
9+
- insertion sort
10+
- merge sort
11+
- quick sort
12+
- heap sort
13+
- bubble sort
14+
- comb sort
15+
- cocktail sort
16+
17+
The algorithms are run using 4 types of input data:
18+
- random
19+
- few unique
20+
- reversed
21+
- almost sorted
22+
23+
## License
24+
25+
See the [LICENSE](LICENSE.txt) file for license rights and limitations (MIT).
26+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package net.mechanika.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class BubbleSortPanel extends SortPanel {
7+
private static final long serialVersionUID = 1L;
8+
private int redColumn = -1;
9+
private int greenColumn = -1;
10+
11+
public BubbleSortPanel(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+
boolean needNextPass = true;
25+
for (int i = 1; i < list.length && needNextPass; i++) {
26+
needNextPass = false;
27+
for (int j = 0; i < list.length - i; i++) {
28+
redColumn = j;
29+
repaint();
30+
Thread.sleep(4 * sleepTime);
31+
if (list[j] > list[j + 1]) {
32+
redColumn = j + 1;
33+
int temp = list[j];
34+
list[j] = list[j + 1];
35+
list[j + 1] = temp;
36+
repaint();
37+
Thread.sleep(4 * sleepTime);
38+
needNextPass = true;
39+
}
40+
}
41+
greenColumn = size - i;
42+
}
43+
greenColumn = 0;
44+
redColumn = -1;
45+
} catch (InterruptedException e) {
46+
}
47+
repaint();
48+
}
49+
50+
@Override
51+
protected void paintComponent(Graphics g) {
52+
super.paintComponent(g);
53+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
54+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
55+
for (int i = 0; i < (greenColumn == -1 ? list.length : greenColumn); i++) {
56+
g.setColor(Color.WHITE);
57+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
58+
g.setColor(Color.BLACK);
59+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
60+
}
61+
if (greenColumn != -1) {
62+
for (int i = greenColumn; i < list.length; i++) {
63+
g.setColor(Color.GREEN);
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+
}
69+
if (redColumn != -1) {
70+
g.setColor(Color.RED);
71+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
72+
g.setColor(Color.BLACK);
73+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
74+
}
75+
}
76+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package net.mechanika.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 sleepTime, int width, int height) {
13+
super(name, sleepTime, width, height);
14+
}
15+
16+
@Override
17+
public void reset() {
18+
redColumn = -1;
19+
greenColumn1 = -1;
20+
greenColumn2 = -1;
21+
}
22+
23+
@Override
24+
public void run() {
25+
try {
26+
boolean swapped = true;
27+
int i = 0;
28+
int j = list.length - 1;
29+
while (i < j && swapped) {
30+
swapped = false;
31+
for (int k = i; k < j; k++) {
32+
redColumn = k;
33+
repaint();
34+
Thread.sleep(4 * sleepTime);
35+
if (list[k] > list[k + 1]) {
36+
repaint();
37+
Thread.sleep(4 * sleepTime);
38+
int temp = list[k];
39+
list[k] = list[k + 1];
40+
list[k + 1] = temp;
41+
swapped = true;
42+
}
43+
}
44+
greenColumn2 = j;
45+
j--;
46+
if (swapped) {
47+
swapped = false;
48+
for (int k = j; k > i; k--) {
49+
redColumn = k;
50+
repaint();
51+
Thread.sleep(4 * sleepTime);
52+
if (list[k] < list[k - 1]) {
53+
repaint();
54+
Thread.sleep(4 * sleepTime);
55+
int temp = list[k];
56+
list[k] = list[k - 1];
57+
list[k - 1] = temp;
58+
swapped = true;
59+
}
60+
}
61+
}
62+
i++;
63+
greenColumn1 = i;
64+
}
65+
redColumn = -1;
66+
greenColumn1 = greenColumn2;
67+
} catch (InterruptedException e) {
68+
}
69+
repaint();
70+
}
71+
72+
@Override
73+
protected void paintComponent(Graphics g) {
74+
super.paintComponent(g);
75+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
76+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
77+
for (int i = (greenColumn1 == -1 ? 0 : greenColumn1); i < (greenColumn2 == -1 ? list.length : greenColumn2); i++) {
78+
g.setColor(Color.WHITE);
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+
if(greenColumn2 != -1) {
84+
for (int i = greenColumn2; i < list.length; i++) {
85+
g.setColor(Color.GREEN);
86+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
87+
g.setColor(Color.BLACK);
88+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
89+
}
90+
}
91+
if(greenColumn1 != -1) {
92+
for (int i = 0; i < greenColumn1; i++) {
93+
g.setColor(Color.GREEN);
94+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
95+
g.setColor(Color.BLACK);
96+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
97+
}
98+
}
99+
if(redColumn != -1) {
100+
g.setColor(Color.RED);
101+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
102+
g.setColor(Color.BLACK);
103+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
104+
}
105+
}
106+
107+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package net.mechanika.sorting;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class CombSortPanel 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 CombSortPanel(String name, int sleepTime, int width, int height) {
13+
super(name, sleepTime, width, height);
14+
}
15+
16+
@Override
17+
public void reset() {
18+
redColumn = -1;
19+
blueColumn = -1;
20+
greenColumn = -1;
21+
}
22+
23+
@Override
24+
public void run() {
25+
try {
26+
27+
int gap = list.length;
28+
boolean swapped = true;
29+
boolean sorted = true;
30+
while (gap > 1 || swapped) {
31+
if (gap > 1) {
32+
gap = (int) (gap / 1.3);
33+
}
34+
swapped = false;
35+
sorted = true;
36+
for (int i = 0; i + gap < list.length; i++) {
37+
redColumn = i;
38+
blueColumn = i + gap;
39+
Thread.sleep(4 * sleepTime);
40+
if (list[i] > list[i + gap]) {
41+
int t = list[i];
42+
list[i] = list[i + gap];
43+
list[i + gap] = t;
44+
repaint();
45+
Thread.sleep(4 * sleepTime);
46+
swapped = true;
47+
}
48+
if((sorted) && (i > 0)) {
49+
if (list[i] > list[i - 1]) {
50+
greenColumn = i;
51+
} else {
52+
greenColumn = -1;
53+
sorted = false;
54+
}
55+
}
56+
repaint();
57+
}
58+
}
59+
redColumn = -1;
60+
blueColumn = -1;
61+
greenColumn = size - 1;
62+
} catch (InterruptedException e) {
63+
}
64+
repaint();
65+
}
66+
67+
@Override
68+
protected void paintComponent(Graphics g) {
69+
super.paintComponent(g);
70+
int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size;
71+
int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size;
72+
for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) {
73+
g.setColor(Color.WHITE);
74+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
75+
g.setColor(Color.BLACK);
76+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
77+
}
78+
for (int i = 0; i <= greenColumn; i++) {
79+
g.setColor(Color.GREEN);
80+
g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
81+
g.setColor(Color.BLACK);
82+
g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight);
83+
}
84+
if(redColumn != -1) {
85+
g.setColor(Color.RED);
86+
g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
87+
g.setColor(Color.BLACK);
88+
g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight);
89+
}
90+
if(blueColumn != -1) {
91+
g.setColor(Color.BLUE);
92+
g.fillRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
93+
g.setColor(Color.BLACK);
94+
g.drawRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight);
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)