-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBars.js
More file actions
389 lines (252 loc) · 9.71 KB
/
Copy pathBars.js
File metadata and controls
389 lines (252 loc) · 9.71 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
class Bars {
constructor(){
this.num = 25;
this.values = [];
this.colors = [];
this.inOrder = false;
this.numberSorted = 25;
this.counted = 0;
this.alreadyFinsihed = false;
this.firstPass = true;
this.Arr2 = [];
this.Arr1 = [];
this.osc = new p5.Oscillator('sine');
}
createBars(){
this.values = [];
//Create random values and sotre them in an array
for(let i = 0; i < this.num; i++){
//Give it value that fits inside the screen height
this.values[i] = Math.floor(Math.random() * windowHeight/1.15);
//Initialize the color to purple
this.colors[i] = 'p';
}
//Reset the variables since new array has been made
this.sorted = false;
this.counted = this.values.length;
this.firstPass = true;
}
drawBars(){
fill(150);
noStroke();
rect(0 , windowHeight/7.8, windowWidth, windowHeight/1.15);
stroke(0);
//Color the boxes purple
//Display the rectangles with their random height
for(let i = 0; i < this.values.length; i++){
//Fill the rectangles according to their colors
if(this.colors[i] == 'p'){
fill(100,100,255);
}else if(this.colors[i] == 'g'){
fill(0,255,0);
}else if(this.colors[i] == 'r'){
fill(255,0,0);
}
//Create rectangle that fits all of them in the screen
rect(windowWidth/this.num * i, windowHeight, windowWidth/this.num, - this.values[i]);
}
}
async radixSort() {
//Set alreadyFinished to false so that new array can not be made
this.alreadyFinsihed = false;
//Set up the oscillator
this.osc.start();
this.osc.amp(.5);
//Find the maximum value
const maxNum = Math.max(...this.values) * 10;
//Initialize the divisor
let divisor = 1;
//As long as every digit has not been accounted for
while (divisor < maxNum) {
//Srt up the ten buckets
let buckets = [[],[],[],[],[],[],[],[],[],[]];
//For every number in the array
for(let num in this.values){
//Grab the digit in a ceartain place
let val = Math.floor((this.values[num] / divisor) % 10);
//Organize the values by their digits in a ceatain place
buckets[val].push(this.values[num]);
}
//Create a clone
let bucketClone = [...buckets];
//Start a counter
let count = 0;
//For every value push it into the array based off where it is in the bucket array
for(let i = buckets.length - 1; i >= 0; i--){
for(let n in buckets[i]){
//As long as the array is not in order keep sorting
if(!this.inOrder){
this.colors[count] = 'g';
await this.sleep(25);
this.colors[count] = 'p';
this.values[count] = bucketClone[i][n];
this.osc.freq(bucketClone[i][n]);
count += 1;
}
}
}
//Increase the divisor to get the next digit place
divisor *= 10;
}
return;
}
async heapify(n , i){
//Initialize the values
let smallest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
//If the left is in bounds and smaller than the smallest term make it the largest
if(left < n && this.values[left] < this.values[smallest]){
smallest = left;
}
//If the right is in bounds and smaller than the smallest make it the largest
if(right < n && this.values[right] < this.values[smallest]){
smallest = right;
}
//If the smallest value changed from the begining, then swap it and keep doing that until it does not change
if(smallest != i){
this.osc.freq(this.values[i]);
await this.swap(i, smallest);
await this.heapify(n, smallest);
}
}
async heapSort() {
//Set alreadyFinished to false so that new array can not be made
this.alreadyFinsihed = false;
//Initialize the oscilattor
this.osc.start();
this.osc.amp(.5);
//Heap sort the whole array intially so smallest it at the left
for(let i = this.values.length - 1; i >= 0; i--){
await this.heapify(this.values.length, i);
}
//Move next smallest value to the left and swap it over into the array
for(let i = this.values.length - 1; i > 0; i--){
await this.swap(0, i);
await this.heapify(i, 0);
}
}
async bubbleSort() {
//Srt alreadyFinsihed to false so that new array can not be made
this.alreadyFinsihed = false;
//Start the oscillator
this.osc.start();
this.osc.amp(.5);
//Swap values if the one to the right is larger than the one to the left
for(let i = 0; i < this.values.length; i++){
for(let j = 0; j < this.numberSorted - 1; j ++){
if(this.values[j] < this.values[j+1]){
await this.swap(j, j+1);
this.osc.freq(this.values[j]);
}
}
//Decrease number sorted to improve efficency
this.numberSorted -= 1;
}
}
async quickSort(start, end){
//Set alreadyFinished to false so that a new array can not be made
this.alreadyFinsihed = false;
//If this is the first pass start the oscilattor
if(this.firstPass){
this.osc.start();
this.firstPass = false;
}
//Return if the start is the same or greater than the end
if(start >= end){
return;
}
this.checkIfSorted();
//As long as it is not in order then split the values into two groups and then resort those two groups
if(!this.inOrder){
let index = await this.partition(start, end);
await Promise.all([
await this.quickSort(start, index - 1),
await this.quickSort(index + 1, end)
]);
}
}
async partition(start, end){
//Initialize the values
let pivotIndex = start;
let pivot = this.values[end];
//Make values bigger than pivot go the left and smaller vlaues than the pivot to the left
for(let i = start; i < end; i++){
if(this.values[i] > pivot){
this.osc.amp(.5);
this.osc.freq(this.values[i]);
await this.swap(i, pivotIndex);
pivotIndex++;
}
}
//Swap the pivot index where it properly goes
await this.swap(end, pivotIndex);
return pivotIndex;
}
sleep(millisecondsDuration)
{
return new Promise(resolve => setTimeout(resolve, millisecondsDuration));
}
setNum(n){
//Create an array of size n
this.num = n;
this.numberSorted = n;
this.createBars();
}
async swap(val1, val2){
//Set values being swaped to green
this.colors[val1] = 'g';
this.colors[val2] = 'g';
//Wait for 25 milliseconds
await this.sleep(25);
//Swap values and reset colors to purple
let temp = this.values[val1];
this.values[val1] = this.values[val2];
this.values[val2] = temp;
this.colors[val1] = 'p';
this.colors[val2] = 'p';
}
checkIfSorted(){
//Assume it is sorted
this.inOrder = true;
//Check every value and its adjacent and see if they are all in order
for(let j = 0; j < this.values.length - 1; j++){
if(this.values[j] < this.values[j+1]){
this.inOrder = false;
}
}
//If it is sorted then run the finsihed animation
if(this.inOrder && !this.alreadyFinsihed){
this.finishAnim();
this.alreadyFinsihed = true;
}
//Return if it is in order
return this.inOrder;
}
returnNum(){
//Return the size of the array
return this.values.length;
}
async finishAnim() {
//As long as the animation has not been run
if(!alreadyFinished){
//Set up the amp
this.osc.amp(.5, .1);
//Display boxes in red and make another one red in 25 milliseconds
for(let i = this.values.length - 1; i >= 0; i--){
this.colors[i] = 'r'
await this.sleep(25);
this.osc.freq(this.values[i], .1);
}
//Say that the animation has run
this.alreadyFinsihed = true;
//Stop the oscillator
this.osc.stop();
return;
}
}
returnFinished(){
//Return if the array has been sorted
return this.alreadyFinsihed;
}
};