-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
532 lines (359 loc) · 10.1 KB
/
app.js
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//buttons variables
let random_array = $("#random_array");
let bubblesort = $("#bubblesort");
let heapsort = $("#heapsort");
let quicksort = $("#quicksort");
let insertionsort = $("#insertionsort");
let stopsorting = $("#stopsorting");
let bars_container = $("#bars_container");
//bar properties variables
let minRange = 1;
let maxRange = 100;
let numOfBars = 100;
let heightFactor = 7;
let unsorted_array = new Array(numOfBars);
//helper variables
let isSorting = true;
let sleepTime = 30;
//timer variables
var minutesLabel = $("#minutes");
var secondsLabel = $("#seconds");
var totalSeconds = 0;
/**
* Helper function for timer.
* Adds minutes, seconds to
* their labels.
*/
function setTime() {
++totalSeconds;
secondsLabel.html(pad(totalSeconds % 60));
minutesLabel.html(pad(parseInt(totalSeconds / 60)));
}
/**
* Functio for displaying
* the timer correctly.
* @param {*} val - current secs or mins.
* @returns correct display of time
*/
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
/**
* Function to get a random nunber
* in an interval.
* @param {*} min - left side of interval.
* @param {*} max - right side of interval.
* @returns random number between min and max.
*/
function random_num(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Creates a random array using
* a random number generator.
*/
function createRandomArray(){
for(let i = 0; i < numOfBars; i++){
unsorted_array[i] = random_num(minRange, maxRange);
}
}
/**
* Renders the array numbers as bars.
* @param {*} array - array to be rendered.
*/
function renderBars(array){
for(let i = 0; i < array.length; i++){
let bar = $("<div></div>");
bar.addClass("bar");
bar.css("height", array[i] * heightFactor + "px");
bars_container.append(bar);
}
}
/**
* Is waiting for the webpage to be loaded
* before randomizing and displaying bars.
*/
document.addEventListener("DOMContentLoaded", function(){
createRandomArray();
renderBars(unsorted_array);
});
/**
* Event listener for random array button.
*/
random_array.click(function(){
createRandomArray();
bars_container.html("");
renderBars(unsorted_array);
});
/**
* Event listener for bubble sort algorithm.
*/
bubblesort.click(function(){
isSorting = true;
bubbleSort(unsorted_array);
});
/**
* Event listener for insertion sort algorithm.
*/
insertionsort.click(function(){
isSorting = true;
insertionSort(unsorted_array);
})
/**
* Event listener for heap sort algorithm.
*/
heapsort.click(function(){
isSorting = true;
HeapSort(unsorted_array);
})
/**
* Event listener for quick sort algorithm.
*/
quicksort.click(function(){
isSorting = true;
quickSort(unsorted_array, 0, unsorted_array.length - 1);
})
/**
* Event listener for stopping the sort.
*/
stopsorting.click(function(){
isSorting = false;
});
/**
* Sleeping function for animation delay.
* @param {*} ms - how many ms to sleep
* @returns delaying an action for ms miliseconds
*/
function sleep(ms){
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Bubble sort of an aray.
* @param {*} array - array to be sorted.
* @returns a sorted array.
*/
async function bubbleSort(array){
var timer = setInterval(setTime, 1000);
let bars = document.getElementsByClassName("bar");
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array.length -1 ; j++){
if(array[j] > array[j+1]){
for(let k = 0; k < bars.length; k++){
if(k!==j && k!==j + 1){
bars[k].style.backgroundColor = "aqua";
}
}
var temp = array[j];
array[j] = array[j + 1];
array[j+1] = temp;
bars[j].style.height = array[j] * heightFactor + "px";
bars[j].style.backgroundColor = "magenta";
bars[j+1].style.height = array[j+1] * heightFactor + "px";
bars[j+1].style.backgroundColor = "magenta";
await sleep(sleepTime);
if(isSorting == false){
break;
}
}
}
if(isSorting == false){
break;
}
await sleep(sleepTime);
}
clearInterval(timer);
clearTimer();
return array;
}
/**
* Heap sort of an array.
* @param {*} array - array to be sorted.
* @returns - sorted array.
*/
async function HeapSort(array) {
var timer = setInterval(setTime, 1000);
let bars = document.getElementsByClassName("bar");
for (let i = Math.floor(array.length / 2); i >= 0; i--) {
if(isSorting == false){
break;
}
await heapify(array, array.length, i);
}
for (let i = array.length - 1; i >= 0; i--) {
if(isSorting == false){
break;
}
await swap(array, 0, i, bars);
await heapify(array, i, 0);
}
for (let k = 0; k < bars.length; k++) {
if(isSorting == false){
break;
}
bars[k].style.backgroundColor = "aqua";
await sleep(sleepTime);
}
clearInterval(timer);
clearTimer();
return array;
}
/**
* Process of creating a heap data.
* @param {*} array - array to be sorted.
* @param {*} n - array length
* @param {*} i - position to heapify.
*/
async function heapify(array, n, i) {
let bars = document.getElementsByClassName("bar");
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && array[left] > array[largest]) {
largest = left;
}
if (right < n && array[right] > array[largest]) {
largest = right;
}
if (largest != i) {
await swap(array, i, largest, bars);
await heapify(array, n, largest);
}
}
/**
* Swapping two elements in an array.
* @param {*} array - array
* @param {*} i - position to be swapped.
* @param {*} j - position to be swapped.
* @param {*} bars - rendered bars on the screen.
* @returns
*/
async function swap(array, i, j, bars) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
bars[i].style.height = array[i] * heightFactor + "px";
bars[j].style.height = array[j] * heightFactor + "px";
bars[i].style.backgroundColor = "magenta";
bars[j].style.backgroundColor = "magenta";
await sleep(sleepTime);
for (let k = 0; k < bars.length; k++) {
if (k != i && k != j) {
bars[k].style.backgroundColor = "aqua";
}
}
return array;
}
/**
* Quick sort of an array.
* @param {*} array - array to be sorted.
* @param {*} left - starting position of the array.
* @param {*} right - eneding position of the array.
* @returns - sorted array.
*/
async function quickSort(array, left, right) {
var index;
let bars = document.getElementsByClassName("bar");
if(isSorting == false){
return;
}
if (array.length > 1) {
index = await partition(array, left, right);
if (left < index - 1) {
await quickSort(array, left, index - 1);
}
if (index < right) {
await quickSort(array, index, right);
}
}
for (let i = 0; i < bars.length; i++) {
bars[i].style.backgroundColor = "aqua";
}
return array;
}
/**
* Partitioning of an array based on
* its boundaries.
* @param {*} array - array to be partitioned.
* @param {*} left - starting position of the array.
* @param {*} right - ending position of the array.
* @returns index for quick sort.
*/
async function partition(array, left, right) {
let bars = document.getElementsByClassName("bar");
let pivotIndex = Math.floor((right + left) / 2);
var pivot = array[pivotIndex];
bars[pivotIndex].style.backgroundColor = "magenta";
for (let i = 0; i < bars.length; i++) {
if (i != pivotIndex) {
bars[i].style.backgroundColor = "aqua";
}
}
(i = left), (j = right);
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
await swap(array, i, j, bars);
i++;
j--;
}
}
return i;
}
/**
* Insertion sort of an array.
* @param {*} array - array to be sorted.
* @returns sorted array
*/
async function insertionSort(array) {
var timer = setInterval(setTime, 1000);
let bars = document.getElementsByClassName("bar");
for (let i = 1; i < array.length; i++) {
if(isSorting == false){
break;
}
let key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
bars[j + 1].style.height = array[j + 1] * heightFactor + "px";
bars[j + 1].style.backgroundColor = "magenta";
await sleep(sleepTime);
for (let k = 0; k < bars.length; k++) {
if (k != j + 1) {
bars[k].style.backgroundColor = "aqua";
}
}
j = j - 1;
}
array[j + 1] = key;
bars[j + 1].style.height = array[j + 1] * heightFactor + "px";
bars[j + 1].style.backgroundColor = "magenta";
await sleep(sleepTime);
}
for (let k = 0; k < bars.length; k++) {
bars[k].style.backgroundColor = "aqua";
}
clearInterval(timer);
clearTimer();
return array;
}
/**
* After the stop button has been pressed,
* clear the displayed timer.
*/
async function clearTimer(){
await sleep(1500);
minutesLabel.html("00");
secondsLabel.html("00");
totalSeconds = 0;
}