-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingAlgorithms.js
More file actions
465 lines (422 loc) · 17.5 KB
/
sortingAlgorithms.js
File metadata and controls
465 lines (422 loc) · 17.5 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
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
//Sorting Algorithms Visualization JS implementation
//Animation speed
const ANIMATION_SPEED = 2;
//Creating new DOM element for the each Array bar
let newElement = document.createElement('barGraph');
document.body.appendChild(newElement)
//Creating new DOM element for the buttons
var button = document.createElement("buttons");
document.body.appendChild(button);
//Create a button for each type of sorting algorithm
var body = document.getElementsByTagName("buttons")[0];
var reset = document.createElement("button");
reset.innerHTML = "Create New Array";
reset.style.backgroundColor = '#e75480';
var bubble = document.createElement("button");
bubble.innerHTML = "Bubble Sort";
var insertion = document.createElement("button");
insertion.innerHTML = "Insertion Sort";
var heap = document.createElement("button");
heap.innerHTML = "Heap Sort";
var merge = document.createElement("button");
merge.innerHTML = "Merge Sort";
var selection = document.createElement("button");
selection.innerHTML = "Selection Sort";
//Add each button to the DOM
body.appendChild(reset);
body.appendChild(bubble);
body.appendChild(insertion);
body.appendChild(heap);
body.appendChild(merge);
body.appendChild(selection);
//global array, array animation, and screen size values
var array = [];
var animationArray = [];
let screenWidth = window.innerWidth;
let screenHeight = window.innerHeight;
//createArray function
//Does: Creates an array of random values, shuffles the values, and creates
// new div elements for each randomly generated number
function createArray() {
let screenWidth = window.innerWidth;
let screenHeight = window.innerHeight;
for (let i = 0; i < Math.floor(screenWidth/17); ++i) {
array.push(randomIntOnInterval(screenHeight*0.05, screenHeight*0.9));
}
function shuffleArray(inputArray) {
for (var i = inputArray.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = inputArray[i];
inputArray[i] = inputArray[j];
inputArray[j] = temp;
}
}
shuffleArray(array);
let temp = document.getElementsByTagName('barGraph');
for (let c in array) {
let newElement1 = document.createElement('div');
newElement1.id = array[c];
newElement1.className = "arrayBar";
newElement1.style.height = array[c] + "px";
newElement1.style.backgroundColor = 'pink';
temp[0].appendChild(newElement1);
}
}
//randomIntOnInterval function
//Does: Returns a random number on a given interval
function randomIntOnInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
createArray();
var bars = document.getElementsByTagName("barGraph");
var divs = bars[0].childNodes;
//Reset Button Event Listener
reset.addEventListener("click", () => {
enableButtons();
clearAnimations();
while(array.length) {
array.pop();
}
let bars = document.getElementsByTagName("barGraph");
while (bars[0].firstChild) {
bars[0].removeChild(bars[0].firstChild);
}
createArray();
});
//clearAnimations function
//Does: Clears all animations stored in all animation arrays
function clearAnimations() {
for (let i = 0; i < animationArray.length; i++) {
clearTimeout(animationArray[i]);
}
for (let i = 0; i < heapAnimation.length; i++) {
bubbleAnimation.pop();
}
for (let i = 0; i < heapAnimation.length; i++) {
insertionAnimation.pop();
}
for (let i = 0; i < heapAnimation.length; i++) {
heapAnimation.pop();
}
for(let i = 0; i < mergeAnimation.length; i++) {
mergeAnimation.pop();
}
for (let i = 0; i < selectionAnimation.length; i++) {
selectionAnimation.pop();
}
animationArray = [];
bubbleAnimation = [];
insertionAnimation = [];
heapAnimation = [];
mergeAnimation = [];
selectionAnimation = [];
}
//Bubble Sort Event Listener
bubble.addEventListener("click", () => {
disableButtons();
getBubbleAnimation(array, divs);
})
//Insertion Sort Event Listener
insertion.addEventListener("click", () => {
disableButtons();
getInsertionAnimation(array, divs);
})
//Heap Sort Event Listener
heap.addEventListener("click", () => {
disableButtons();
heapAnimationFunc(array, divs);
})
//Merge Sort Event Listener
merge.addEventListener("click", () => {
disableButtons();
getMergeAnimations(array, divs);
})
//Selection Sort Event Listener
selection.addEventListener("click", () => {
disableButtons();
getSelectionAnimation(array, divs);
console.log(array);
})
//disableButtons Function
//Does: Disables all sorting buttons until a new array is array is created
function disableButtons() {
let button_divs = document.getElementsByTagName('buttons');
let buttons_array = button_divs[0].childNodes;
for (let i = 1; i < buttons_array.length; i++) {
buttons_array[i].disabled = true;
}
}
//enableButtons Function
//Does: Enables all sorting buttons after a new array is created
function enableButtons() {
let button_divs = document.getElementsByTagName('buttons');
let buttons_array = button_divs[0].childNodes;
for (let i = 1; i < buttons_array.length; i++) {
buttons_array[i].disabled = false;
}
}
//typeCheck function
//Does: Checks the type of animation being requested (arrayBar swapping or color changing)
function typeCheck(firstIndex, secondIndex, typeChange, instruction, bars, i) {
if (typeChange === 'color') {
colorSwap(firstIndex, secondIndex, instruction, bars, i);
} else {
barSwap(firstIndex, secondIndex, bars, i);
}
}
//colorSwap function
//Does: Changes the colors of two array bars being checked. If the
// bars are first being checked, they are turned red, if they
// are being reverted, they are turned pink.
function colorSwap(firstIndex, secondIndex, instruction, bars, i) {
let firstBar = bars[firstIndex].style;
let secondBar = bars[secondIndex].style;
let changeColor = instruction === 'change' ? 'red' : 'pink';
animationArray.push(setTimeout( () => {
firstBar.backgroundColor = changeColor;
secondBar.backgroundColor = changeColor;
}, i*ANIMATION_SPEED));
}
//barSwap function
//Does: Swaps the heights of two array bars given the first and
// second indices.
function barSwap(firstVal, secondVal, bars, i) {
animationArray.push(setTimeout( () => {
let temp = bars[firstVal].style.height;
bars[firstVal].style.height = bars[secondVal].style.height;
bars[secondVal].style.height = temp;
}, i*ANIMATION_SPEED));
}
//Bubble Sort Animations Array
var bubbleAnimation = [];
//getBubbleAnimation function
//Does: Sorts and performs the visualization of a Bubble Sort
function getBubbleAnimation(input, bars) {
bubbleSort(input);
for (let i = 0; i < bubbleAnimation.length; i++) {
let [firstIndex, secondIndex, typeChange, instruction] = bubbleAnimation[i];
if (typeChange === 'color') {
colorSwap(firstIndex, secondIndex, instruction, bars, i);
} else {
barSwap(firstIndex, secondIndex, bars, i);
}
}
}
//bubbleSort function
//Does: Performs a bubble sort on the input array and pushes animations to
// the bubbleAnimation array.
function bubbleSort(input) {
let length = input.length;
let isSwapped;
do {
isSwapped = false;
for (let i = 0; i < length-1; i++) {
if (input[i] > input[i + 1]) {
//Pushes indices to bubbleAnimation to change the color indicating
//the bars are being checked
bubbleAnimation.push([i, i+1, 'color', 'change']);
//Pushes indices to bubbleAnimation to revert the color
bubbleAnimation.push([i, i+1, 'color', 'revert']);
//Pushes indices to bubbleAnimation to change the height of
//the array bars
bubbleAnimation.push([i, i+1, 'height']);
let temp = input[i];
input[i] = input[i + 1];
input[i + 1] = temp;
isSwapped = true;
}
}
} while (isSwapped);
};
//Insertion Sort Animations Array
var insertionAnimation = [];
//getInsertionAnimation function
//Does: Sorts and performs the visualization of an insertion sort
function getInsertionAnimation(input, bars) {
insertionSort(input);
for (let i = 0; i < insertionAnimation.length; i++) {
let [firstIndex, secondIndex, typeChange, instruction] = insertionAnimation[i];
typeCheck(firstIndex, secondIndex, typeChange, instruction, bars, i);
}
}
//insertionSort function
//Does: Performs an insertion sort on the input array and pushes animations
// to the insertionAnimation array
function insertionSort(input) {
let length = input.length;
for (let i = 1; i < length; i++) {
let value = input[i];
let j = i - 1;
while (j >= 0 && input[j] > value) {
//Pushes indices to insertionAnimation to change the color
insertionAnimation.push([j, j+1, 'color', 'change']);
//Pushes indices to insertionAnimation to revert the color
insertionAnimation.push([j, j+1, 'color', 'revert']);
//Pushes indices to insertionAnimation to swap array bars
insertionAnimation.push([j, j+1, 'height']);
input[j + 1] = input[j];
j--;
}
input[j + 1] = value;
}
}
//Heap Sort Animations Array
var heapAnimation = [];
//heapAnimationFunc function
//Does: Sorts and visualizes a heap sort
function heapAnimationFunc(input, bars) {
heapSort(input);
for(let i = 0; i < heapAnimation.length; i++) {
let [firstIndex, secondIndex, typeChange, instruction] = heapAnimation[i];
typeCheck(firstIndex, secondIndex, typeChange, instruction, bars, i);
}
}
//heapSort function
//Does: Performs a heap sort and pushes animations to the heapAnimation
// array
function heapSort(input) {
for (let i = Math.floor(input.length / 2 - 1); i >= 0; i--) {
heapify(input, input.length, i);
}
for (let i = input.length - 1; i >= 0; i--) {
heapSwap(input, 0, i);
heapify(input, i, 0);
}
}
//heapify function
//Does: Creates and maintains the heap. Also pushes animations to the
// heapAnimation array when necessary
function heapify(input, size, i) {
let max = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < size && input[left] > input[max]) {
max = left;
}
if (right < size && input[right] > input[max]) {
max = right;
}
//Pushes indices to the heapAnimation array to change color
heapAnimation.push([i, max, 'color', 'change']);
//Pushes indices to the heapAniamtion array to revert color
heapAnimation.push([i, max, 'color', 'revert']);
if (max != i) {
heapSwap(input, i, max);
heapify(input, size, max);
}
}
//heapSwap function
//Does: Swaps the values stored at the given indices in the input
// array
function heapSwap(input, first, second) {
//Pushes indices to the heapAnimation array to swap array bars
heapAnimation.push([first, second, 'height']);
let temp = input[first];
input[first] = input[second];
input[second] = temp;
}
//Merge Sort Animation Array
var mergeAnimation = [];
//getMergeAnimations function
//Does: Sorts and visualizes of a Merge sort
function getMergeAnimations(input, bars) {
let tempArray = Array.from(array);
mergeSortRecur(input, 0, input.length - 1, tempArray);
for (let i = 0; i < mergeAnimation.length; i++) {
let [firstIndex, secondIndex, typeChange, instruction] = mergeAnimation[i];
if (typeChange === 'color') {
colorSwap(firstIndex, secondIndex, instruction, bars, i);
} else {
animationArray.push(setTimeout( () => {
const [index, heightVal] = mergeAnimation[i];
bars[index].style.height = heightVal + 'px';
}, i*ANIMATION_SPEED));
}
}
}
//mergeSortRecur function
//Does: Recursive merge sort helper function
function mergeSortRecur(input, startIndex, endIndex, tempArray) {
if (startIndex === endIndex) {
return;
}
let middleIndex = Math.floor((startIndex + endIndex) / 2);
mergeSortRecur(tempArray, startIndex, middleIndex, input);
mergeSortRecur(tempArray, middleIndex + 1, endIndex, input);
mergeSortHelp(input, startIndex, middleIndex, endIndex, tempArray);
}
//mergeSortHelp function
//Does: Performs an iterative merge sort on a portion of the array and
// pushes animations to the mergeAnimations array
function mergeSortHelp(input, startIndex, middleIndex, endIndex, tempArray) {
let x = startIndex, y = startIndex, z = middleIndex + 1;
while (y <= middleIndex && z <= endIndex) {
//Pushes indices to the mergeAnimation array to change color
mergeAnimation.push([y, z, 'color', 'change']);
//Pushes indices to the mergeAnimation array to revert color
mergeAnimation.push([y, z, 'color', 'revert']);
if (tempArray[y] <= tempArray[z]) {
//Pushes indices to the mergeAnimation array to swap array bars
mergeAnimation.push([x, tempArray[y], 'height']);
input[x++] = tempArray[y++];
} else {
//Pushes indices to the mergeAnimation array to swap array bars
mergeAnimation.push([x, tempArray[z], 'height']);
input[x++] = tempArray[z++];
}
}
while (y <= middleIndex) {
//Pushes indices to the mergeAnimation array to change color
mergeAnimation.push([y, y, 'color', 'change']);
//Pushes indices to the mergeAnimation array to revert color
mergeAnimation.push([y, y, 'color', 'revert']);
//Pushes indices to the mergeAnimation array to swap array bars
mergeAnimation.push([x, tempArray[y], 'height']);
input[x++] = tempArray[y++];
}
while (z <= endIndex) {
//Pushes indices to the mergeAnimation array to change color
mergeAnimation.push([z, z, 'color', 'change']);
//Pushes indices to the mergeAnimation array to revert color
mergeAnimation.push([z, z, 'color', 'revert']);
//Pushes indices to the mergeAnimation array to swap array bars
mergeAnimation.push([x, tempArray[z], 'height']);
input[x++] = tempArray[z++];
}
}
//Selection Sort Animation Array
selectionAnimation = [];
//getSelectionAnimation function
//Does: Sorts and visualizes a selection sort
function getSelectionAnimation(input, bars) {
selectionSort(input);
for (let i = 0; i < selectionAnimation.length; i++) {
let [firstIndex, secondIndex, typeChange, instruction] = selectionAnimation[i];
typeCheck(firstIndex, secondIndex, typeChange, instruction, bars, i);
}
}
//selectionSort function
//Does: Performs a selection sort and pushes animations to
// the selectionAnimation function
function selectionSort(input) {
let length = input.length;
for (let i = 0; i < length; i++) {
let minimum = i;
for (let j = i + 1; j < length; j++) {
//Pushes indices to the selectionAnimation array to change colors
selectionAnimation.push([j, minimum, 'color', 'change']);
//Pushes indices to the selectionAnimation array to revert colors
selectionAnimation.push([j, minimum, 'color', 'revert']);
if (input[minimum] > input[j]) {
minimum = j;
}
}
if (minimum !== i) {
//Pushes indices to the selectionAnimation array to swap array bars
selectionAnimation.push([i, minimum, 'height']);
let temp = input[i];
input[i] = input[minimum];
input[minimum] = temp;
}
}
}