-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
249 lines (231 loc) · 5.85 KB
/
script.js
File metadata and controls
249 lines (231 loc) · 5.85 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
const btnFill = document.querySelector("#btnFill");
const btnSort = document.querySelector("#btnSort");
const btnClear = document.querySelector("#btnClear");
const container = document.querySelector(".arrayContainer");
const select = document.querySelector("#select");
const speed = document.querySelector("#speed");
const menu = document.querySelector(".menu");
const arrsize = document.querySelector("#arraysize");
let timer;
let arr = [];
let animating = false;
let sorted = false;
btnFill.addEventListener("click", function () {
arr = fillArray();
sorted = false;
draw(arr);
manageButtons();
});
btnClear.addEventListener("click", function () {
animating = false;
sorted = false;
clearInterval(timer);
container.innerHTML = "";
arr = [];
manageButtons();
});
btnSort.addEventListener("click", function () {
animating = true;
manageButtons();
let arrSorted;
const algo = select.value;
console.log(select.value);
if (algo == "bubble") {
arrSorted = bubbleSort();
} else if (algo == "insertion") {
arrSorted = insertionSort();
} else if (algo == "selection") {
arrSorted = selectionSort();
}
console.log(
`Total iterations: ${arrSorted.length}\nTotal snapshopts: ${
arrSorted.length * arrsize.value
}`
);
let index = 0;
timer = setInterval(function () {
draw(arrSorted[index]);
index++;
if (index == arrSorted.length) {
animating = false;
sorted = true;
manageButtons();
clearInterval(timer);
}
}, speed.value);
});
function selectionSort() {
let snapShots = [];
for (let i = 0; i < arr.length - 1; i++) {
paintInsertion(i);
let minimum = i;
for (let j = i + 1; j < arr.length; j++) {
paintInsertion(i);
arr[i].col = "select";
arr[j].col = "select";
snap(snapShots);
if (arr[j].val < arr[minimum].val) {
arr[j].col = "different";
snap(snapShots);
minimum = j;
}
}
paintInsertion(i);
if (minimum != i) {
arr[minimum].col = "different";
arr[i].col = "different";
snap(snapShots);
let tmp = arr[i].val;
arr[i].val = arr[minimum].val;
arr[minimum].val = tmp;
snap(snapShots);
arr[i].col = "select";
arr[minimum].col = "select";
snap(snapShots);
}
}
for (let i = 0; i < arr.length; i++) {
arr[i].col = "sorted";
}
snap(snapShots);
return snapShots;
}
function insertionSort() {
let snapShots = [];
for (let i = 1; i < arr.length; i++) {
let j = i;
while (j > 0 && arr[j - 1].val > arr[j].val) {
paintInsertion(i);
arr[j].col = "select";
arr[j - 1].col = "select";
snap(snapShots);
arr[j].col = "different";
arr[j - 1].col = "different";
snap(snapShots);
let tmp = arr[j - 1].val;
arr[j - 1].val = arr[j].val;
arr[j].val = tmp;
snap(snapShots);
arr[j].col = "select";
arr[j - 1].col = "select";
snap(snapShots);
j--;
}
}
for (let i = 0; i < arr.length; i++) {
arr[i].col = "sorted";
}
snap(snapShots);
return snapShots;
}
function paintInsertion(s) {
for (let i = 0; i < arr.length; i++) {
arr[i].col = "normal";
}
for (let i = 0; i < s; i++) {
arr[i].col = "sorted";
}
}
function bubbleSort() {
let snapShots = [];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
paintBubble(arr.length - i);
arr[j + 1].col = "select";
arr[j].col = "select";
snap(snapShots);
if (arr[j].val > arr[j + 1].val) {
arr[j + 1].col = "different";
arr[j].col = "different";
snap(snapShots);
let tmp = arr[j].val;
arr[j].val = arr[j + 1].val;
arr[j + 1].val = tmp;
snap(snapShots);
arr[j + 1].col = "select";
arr[j].col = "select";
snap(snapShots);
}
}
}
for (let i = 0; i < arr.length; i++) {
arr[i].col = "sorted";
}
snap(snapShots);
return snapShots;
}
function paintBubble(s) {
for (let i = 0; i < arr.length; i++) {
arr[i].col = "sorted";
}
for (let i = 0; i < s; i++) {
arr[i].col = "normal";
}
}
/*
When writing an algorithm function, keep in mind:
-snap(param) will write a snapshot of arr to param.
use it to add a frame to the "film" (snapShots)
-use snap() every time there is a color change!
*/
/*
Snapshot for comparison
Switch color for test
Snapshop for difference
SWAP
Snapshot for replace
*/
function snap(target) {
target.push(JSON.parse(JSON.stringify([...arr])));
}
function fillArray() {
const tmp = [];
const len = arrsize.value;
for (let i = 0; i < len; i++) {
tmp[i] = { val: Math.floor(Math.random() * 100 + 1), col: "normal" };
}
return tmp;
}
function draw(array) {
container.innerHTML = "";
for (let i = 0; i < array.length; i++) {
const tmp = document.createElement("div");
const grd = document.createElement("div");
const vgrd = document.createElement("div");
const num = document.createElement("span");
tmp.style.height = array[i].val + "%";
tmp.style.width = 100 / arr.length + "%";
tmp.classList.add("bar", array[i].col);
grd.classList.add("grd");
vgrd.classList.add("vgrd");
num.classList.add("num");
num.textContent = array[i].val;
tmp.appendChild(grd);
grd.appendChild(vgrd);
vgrd.appendChild(num);
container.appendChild(tmp);
}
}
function manageButtons() {
if (animating) {
btnFill.disabled = true;
btnSort.disabled = true;
btnClear.disabled = false;
} else {
if (arr.length > 0) {
if (sorted) {
btnClear.disabled = false;
btnFill.disabled = false;
btnSort.disabled = true;
} else {
btnFill.disabled = false;
btnSort.disabled = false;
btnClear.disabled = false;
}
} else {
btnFill.disabled = false;
btnSort.disabled = true;
btnClear.disabled = true;
}
}
}