-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
296 lines (256 loc) · 7.62 KB
/
sketch.js
File metadata and controls
296 lines (256 loc) · 7.62 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const buttons = document.getElementsByClassName("buttons");
let w = 15;
let values = [];
let states = []; //her çubuğun durumunu saklamak ve renk değiştirmek için
let controller = true;
function setup() {
if (controller) {
controller =false;
createCanvas(600, 300);
values = new Array(floor(width / w));
for (let i = 0; i < values.length; i++) {
values[i] = Math.floor(float(random(height)));
states[i] = -1;
print(values);
}
print("Unsorted Array:" + values); // To print values to Browser's Console
controller = true;
} else {
showAlert("danger","Please wait for the sorting to be completed!");
}
}
draw = () => {
background("white");
for (let i = 0; i < values.length; i++) {
stroke(0);
fill("lightblue");
rect(i * w, height - values[i], w, values[i]);
}
}
const showAlert = (type,message) => {
console.log(buttons[0]);
const alert = document.createElement("div");
alert.className = `alert alert-${type}`;
alert.textContent = message;
buttons[0].appendChild(alert);
setTimeout(function(){
alert.remove();
},2000);
}
const BubbleSort = async () => {
if (controller) {
controller = false;
for (var i = 0; i < values.length - 1; i++) {
for (var j = 0; j < values.length - i - 1; j++) {
if (values[j] >= values[j + 1]) {
await swap(values, j, j + 1);
}
}
}
print("Bubble Sorted Array:" + values);
showAlert("success","Well done! Sorting completed.");
controller = true;
return values;
} else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
const QuickSort = async () => {
if (controller) {
quickSort(values, 0, values.length);
} else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
async function quickSort(arr, start, end) {
controller = false;
if (start >= end) {
return;
}
let index = await partition(arr, start, end);
states[index] = -1;
await Promise.all([
quickSort(arr, start, index - 1),
quickSort(arr, index + 1, end),
]);
async function partition(arr, start, end) {
for (let i = start; i < end; i++) {
states[i] = 1;
}
let pivotIndex = start;
let pivotValue = arr[end];
states[pivotIndex] = 0;
for (let i = start; i < end; i++) {
if (arr[i] < pivotValue) {
await swap(arr, i, pivotIndex);
states[pivotIndex] = -1;
pivotIndex++;
states[pivotIndex] = 0;
}
}
await swap(arr, pivotIndex, end);
for (let i = start; i < end; i++) {
states[i] = -1;
}
return pivotIndex;
}
controller = true;
}
const InsertionSort = async () => {
if (controller) {
controller = false;
let length = values.length;
for (let i = 1; i < length; i++) {
let key = values[i];
let j = i - 1;
while (j >= 0 && values[j] > key) {
values[j + 1] = values[j];
j = j - 1;
await sleep(100);
}
values[j + 1] = key;
}
print("Insertion Sorted Array:" + values);
showAlert("success","Well done! Sorting completed.");
controller = true;
}
else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
const SelectionSort = async () => {
if (controller) {
controller = false;
// Clone original array to prevent its modification.
for (let i = 0; i < values.length - 1; i += 1) {
let minIndex = i;
// Find minimum element in the rest of array.
for (let j = i + 1; j < values.length; j += 1) {
if (values[j] < values[minIndex]) {
minIndex = j;
}
}
// If new minimum element has been found then swap it with current i-th element.
if (minIndex !== i) {
await swap(values, minIndex, i);
}
await sleep(100);
print("Selection Sorted Array:" + values);
}
showAlert("success","Well done! Sorting completed.");
controller = true;
}
else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
const CountingSort = async () => {
if (controller) {
controller = false;
const min = Math.min(...values);
const max = Math.max(...values);
let i;
let z = 0;
const count = [];
for (i = min; i <= max; i++) {
count[i] = 0;
}
for (i = 0; i < values.length; i++) {
count[values[i]]++;
}
for (i = min; i <= max; i++) {
while (count[i]-- > 0) {
values[z++] = i;
await sleep(100);
}
}
print("Counting Sorted Array:" + values);
showAlert("success","Well done! Sorting completed.");
controller = true;
}
else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
function MergeSort() {
if (controller) {
controller = false;
copy = values.slice();
mergeSortSlice(copy, 0, copy.length);
return;
}
else {
showAlert("danger","Please wait for the sorting to be completed!");
}
controller = true;
}
async function mergeSortSlice(a, start, end) {
controller = false;
if (end - start <= 1)
return;
var mid = Math.round((end + start) / 2);
// wait till divides are sort
await mergeSortSlice(a, start, mid);
await mergeSortSlice(a, mid, end);
// merge divides
let i = start,
j = mid;
while (i < end && j < end) {
if (a[i] > a[j]) {
let t = a[j];
a.splice(j, 1);
a.splice(i, 0, t);
j++;
}
i++;
if (i == j) j++;
// copy back the current state of the sorting
values = a.slice();
// slow down
await sleep(50);
}
// restart
if (start == 0 && end == a.length) {
await sleep(50);
startSort = true;
print("Merge Sorted Array:" + values);
showAlert("success","Well done! Sorting completed.");
}
}
const ShellSort = async () => {
if (controller) {
controller = false;
var increment = values.length / 2;
while (increment > 0) {
for (i = increment; i < values.length; i++) {
var j = i;
var temp = values[i];
while (j >= increment && values[j - increment] > temp) {
values[j] = values[j - increment];
j = j - increment;
}
await sleep(100);
values[j] = temp;
}
if (increment == 2) {
increment = 1;
} else {
increment = parseInt((increment * 5) / 11);
}
}
print("Shell Sorted Array:" + values);
showAlert("success","Well done! Sorting completed.");
controller = true;
} else {
showAlert("danger","Please wait for the sorting to be completed!");
}
};
const swap = async (arr, a, b) => {
await sleep(100);
let t = arr[a];
arr[a] = arr[b];
arr[b] = t;
};
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};