- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSortingAlgorithm.java
More file actions
Latest commit
221 lines (188 loc) · 7.9 KB
/
Copy pathSortingAlgorithm.java
File metadata and controls
221 lines (188 loc) · 7.9 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Implementation of Sorting algorithms
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.geom.Rectangle2D;
importjava.util.Random;
importjavax.swing.JPanel;
importjavax.swing.SwingWorker;
publicclassSortingAlgorithmextendsJPanel {
privatefinalintWIDTH = 800, HEIGHT = WIDTH * 9 / 16;
publicintSIZE = 100; // the number if sorting elements
publicfloatBAR_WIDTH = (float)WIDTH / SIZE; // bar width
privatefloat[] bar_height = newfloat[SIZE]; // height of bars
privateSwingWorker<Void, Void> shuffler, sorter;
privateintcurrent_index, traversing_index; // needed for following colloring the items
SortingAlgorithm() {
setBackground(Color.BLACK);
setPreferredSize(newDimension(WIDTH, HEIGHT));
initBarHeight(); // initialize the height of each bar
// initShuffler(); // shuffle each bar
}
// setter for SIZE
publicvoidsetSIZE(intSIZE) {
this.SIZE = SIZE;
}
// getter for SIZE
intgetSIZE() {
returnSIZE;
}
// repaint() will automaticly call this function
// needed for coloring
@Override
publicvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
// Create randomizer
Randomrandom = newRandom();
// Drawing the rectangles
Graphics2Dg2d = (Graphics2D)g;
g2d.setColor(Color.CYAN);
Rectangle2D.Floatbar;
for(inti = 0; i < getSIZE(); i++ ) {
// random colors
// final float hue = random.nextFloat();
// final float saturation = 0.9f; //1.0 for brilliant, 0.0 for dull
// final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
// g2d.setColor(Color.getHSBColor(hue, saturation, luminance));
bar = newRectangle2D.Float(i * BAR_WIDTH, 0, BAR_WIDTH-1, bar_height[i]);
g2d.fill(bar); // g2d.draw(bar);
}
// Color setter for the current object
g2d.setColor(Color.RED);
bar = newRectangle2D.Float(current_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[current_index]);
g2d.fill(bar);
// Color setter for the traversing object
g2d.setColor(Color.YELLOW);
bar = newRectangle2D.Float(traversing_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[traversing_index]);
g2d.fill(bar);
}
publicvoidinsertionSort() {
/*Insertion sort algorithm*/
// Multithreading used for hadling the sorting
sorter = newSwingWorker<>() {
@Override
publicVoiddoInBackground() throwsInterruptedException { // function for calling multithreading
// Insertion sort algorithm starts
for(current_index = 1; current_index < getSIZE(); current_index++) {
traversing_index = current_index;
while(traversing_index > 0 && bar_height[traversing_index] < bar_height[traversing_index - 1]) {
swap(traversing_index, traversing_index - 1);
traversing_index--;
Thread.sleep(10); // controls the speed
repaint(); // we need it because we ofter replace the contents of a JPanel
}
}
current_index = 0;
traversing_index = 0;
returnnull;
}
};
}
publicvoidbubbleSort() {
/*Bubble sorting algorithm*/
sorter = newSwingWorker<>() {
@Override
publicVoiddoInBackground() throwsInterruptedException {
for(current_index = 0; current_index < getSIZE(); current_index++) {
for(traversing_index = 1; traversing_index < (getSIZE() - current_index); traversing_index++) {
if(bar_height[traversing_index - 1] > bar_height[traversing_index]) {
swap(traversing_index, traversing_index - 1);
traversing_index--; // just for annimation
Thread.sleep(10); // controls the speed
repaint(); // we need it because we ofter replace the contents of a JPanel
}
}
}
current_index = 0;
traversing_index = 0;
returnnull;
}
};
}
publicvoidmergeSort() {
/*Merge sorting algorithm*/
// Change code from bubbleSort to mergeSort
// TODO
sorter = newSwingWorker<>() {
@Override
publicVoiddoInBackground() throwsInterruptedException {
for(current_index = 0; current_index < getSIZE(); current_index++) {
for(traversing_index = 1; traversing_index < (getSIZE() - current_index); traversing_index++) {
if(bar_height[traversing_index - 1] > bar_height[traversing_index]) {
swap(traversing_index, traversing_index - 1);
traversing_index--; // just for annimation
Thread.sleep(10); // controls the speed
repaint(); // we need it because we ofter replace the contents of a JPanel
}
}
}
current_index = 0;
traversing_index = 0;
returnnull;
}
};
}
publicvoidselectionSort() {
/*Merge sorting algorithm*/
// Change code from bubbleSort to mergeSort
// TODO
sorter = newSwingWorker<>() {
@Override
publicVoiddoInBackground() throwsInterruptedException {
for(current_index = 0; current_index < getSIZE() - 1; current_index++) {
intmin_index = current_index;
for(inttraversing_index = current_index + 1; traversing_index < getSIZE(); traversing_index++) {
if (bar_height[traversing_index] < bar_height[min_index]) {
min_index = traversing_index;
}
}
swap(current_index, min_index);
Thread.sleep(10); // controls the speed
repaint(); // we need it because we ofter replace the contents of a JPanel
}
current_index = 0;
traversing_index = 0;
returnnull;
}
};
}
publicvoidinitShuffler() {
/*Shuffles each bar*/
shuffler = newSwingWorker<>() {
@Override
publicVoiddoInBackground() throwsInterruptedException {
intmiddle = getSIZE() / 2;
for (inti = 0, j = middle; i < middle; i++, j++) {
intrandow_index = newRandom().nextInt(getSIZE());
swap(i, randow_index);
randow_index = newRandom().nextInt(getSIZE());
swap(j, randow_index);
Thread.sleep(10); // controls the speed
repaint(); // we need it because we ofter replace the contents of a JPanel
}
returnnull;
}
// after finishing the process
@Override
publicvoiddone() {
super.done();
sorter.execute();
}
};
shuffler.execute();
}
publicvoidinitBarHeight() {
/*Initialize the height of each bar*/
floatinterval = (float)HEIGHT / getSIZE();
for(inti = 0; i < getSIZE(); i++) {
bar_height[i] = i * interval;
}
}
publicvoidswap(intindexA, intindexB) {
/*Swaps the elements*/
floattemp = bar_height[indexA];
bar_height[indexA] = bar_height[indexB];
bar_height[indexB] = temp;
}
}