-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (106 loc) · 3.25 KB
/
script.js
File metadata and controls
122 lines (106 loc) · 3.25 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
import { bubbleSort, selectionSort, insertionSort } from './utils/algorithms.js';
// DOM
const arrayContainer = document.getElementById("array");
const sortButton = document.getElementById("sort");
const resetButton = document.getElementById('reset');
const algoTypeInput = document.getElementById('algoType');
const arrayStyleInput = document.getElementById("arrayStyle");
const lengthSlider = document.getElementById("lengthSlider");
const delaySlider = document.getElementById("delaySlider");
let array = [];
let arrayList = [];
let swapList = [];
let len = 20;
let delay = 100;
let arrayStyle = "bars";
let algoType = "bubble";
// let isSorting = false;
const setArray = (len) => {
array = [];
for (let i = 0; i < len; i++) {
array[i] = Math.floor(Math.random() * 100);
}
}
const addBlocks = (arr, swapPair) => {
arr.forEach((el, i) => {
const element = document.createElement('div');
element.classList.add('array__element');
if (i == swapPair[0] || i == swapPair[1]) element.classList.add('red');
element.innerHTML = el;
arrayContainer.appendChild(element);
})
}
const addBars = (arr, swapPair) => {
arr.forEach((el, i) => {
const color = (i == swapPair[0] || i == swapPair[1]) ? 'red' : 'black';
const element = document.createElement('div');
element.classList.add('array__bar');
element.style.height = `${el}px`;
element.style.backgroundColor = color;
arrayContainer.appendChild(element);
});
}
const addArrayDOM = (arr, swapPair, arrayStyle) => {
arrayContainer.innerHTML = "";
arrayStyle == "bars" ? addBars(arr, swapPair) : addBlocks(arr, swapPair);
}
const sortDOM = async (arrayStyle, algoType) => {
arrayList = [];
swapList = [];
switch (algoType) {
case "bubble":
bubbleSort(array, arrayList, swapList);
break;
case "selection":
selectionSort(array, arrayList, swapList);
break;
case "insertion":
insertionSort(array, arrayList, swapList);
break;
case "merge":
mergeSort(array, 0, array.length - 1);
break;
default:
bubbleSort(array);
}
arrayList.forEach((arr, i) => {
setTimeout(() => addArrayDOM(arr, swapList[i], arrayStyle), i * delay);
});
}
const displayArray = (array, arrayStyle) => {
arrayContainer.innerHTML = "";
arrayStyle == "bars" ? addBars(array, [-1, -1]) : addBlocks(array, [-1, -1]);
}
algoTypeInput.addEventListener('change', (e) => {
algoType = e.target.value;
})
arrayStyleInput.addEventListener('change', (e) => {
arrayStyle = e.target.value;
displayArray(array, arrayStyle);
});
lengthSlider.addEventListener("change", (e) => {
len = e.target.value;
init();
})
delaySlider.addEventListener("change", e => {
delay = e.target.value;
init();
})
sortButton.addEventListener('click', () => {
sortButton.disabled = true;
resetButton.disabled = true;
sortDOM(arrayStyle, algoType);
setTimeout(() => {
sortButton.disabled = false;
resetButton.disabled = false;
}, arrayList.length * delay);
});
resetButton.addEventListener('click', () => {
init();
});
function init() {
arrayContainer.innerHTML = "";
setArray(len);
displayArray(array, arrayStyle);
}
init();