-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
257 lines (197 loc) Β· 5.24 KB
/
script.js
File metadata and controls
257 lines (197 loc) Β· 5.24 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
let array = [];
let isSorting = false;
let stopSorting = false;
/* ---------------- AUDIO ---------------- */
const completeSound = new Audio("complete.mp3");
// unlock audio on first click (fixes browser blocking)
document.addEventListener("click", () => {
completeSound.play().then(() => {
completeSound.pause();
completeSound.currentTime = 0;
}).catch(() => {});
}, { once: true });
function playSound() {
completeSound.currentTime = 0;
completeSound.play().catch(() => {});
}
/* ---------------- ARRAY ---------------- */
function generateArray() {
if (isSorting) return;
array = [];
for (let i = 0; i < 50; i++) {
array.push(Math.floor(Math.random() * 300) + 10);
}
playSound(); // sound on generate
render();
}
/* ---------------- RENDER ---------------- */
function render() {
const bars = document.getElementById("bars");
bars.innerHTML = "";
for (let i = 0; i < array.length; i++) {
const bar = document.createElement("div");
bar.classList.add("bar");
bar.style.height = array[i] + "px";
bars.appendChild(bar);
}
}
/* ---------------- DELAY ---------------- */
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* ---------------- BUBBLE SORT ---------------- */
async function bubbleSort() {
if (isSorting) return;
isSorting = true;
stopSorting = false;
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (stopSorting) return;
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
render();
playSound(); // π sound on swap
await sleep(50);
}
}
}
playSound(); // π finish sound
isSorting = false;
}
/* ---------------- SELECTION SORT ---------------- */
async function selectionSort() {
if (isSorting) return;
isSorting = true;
stopSorting = false;
for (let i = 0; i < array.length; i++) {
if (stopSorting) return;
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
[array[i], array[minIndex]] = [array[minIndex], array[i]];
render();
playSound();
await sleep(50);
}
playSound(); // π finish
isSorting = false;
}
/* ---------------- INSERTION SORT ---------------- */
async function insertionSort() {
if (isSorting) return;
isSorting = true;
stopSorting = false;
for (let i = 1; i < array.length; i++) {
if (stopSorting) return;
let key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
if (stopSorting) return;
array[j + 1] = array[j];
j--;
render();
playSound();
await sleep(50);
}
array[j + 1] = key;
render();
await sleep(50);
}
playSound(); // π finish
isSorting = false;
}
/* ---------------- MERGE SORT ---------------- */
async function mergeSortWrapper() {
if (isSorting) return;
isSorting = true;
stopSorting = false;
await mergeSort(0, array.length - 1);
playSound(); // π finish
isSorting = false;
}
async function mergeSort(left, right) {
if (left >= right || stopSorting) return;
let mid = Math.floor((left + right) / 2);
await mergeSort(left, mid);
await mergeSort(mid + 1, right);
await merge(left, mid, right);
}
async function merge(left, mid, right) {
let temp = [];
let i = left;
let j = mid + 1;
while (i <= mid && j <= right) {
if (array[i] <= array[j]) {
temp.push(array[i++]);
} else {
temp.push(array[j++]);
}
}
while (i <= mid) temp.push(array[i++]);
while (j <= right) temp.push(array[j++]);
for (let k = 0; k < temp.length; k++) {
array[left + k] = temp[k];
render();
await sleep(30);
}
}
/* ---------------- QUICK SORT ---------------- */
async function quickSortWrapper() {
if (isSorting) return;
isSorting = true;
stopSorting = false;
await quickSort(0, array.length - 1);
playSound(); // π finish
isSorting = false;
}
async function quickSort(low, high) {
if (low < high && !stopSorting) {
let pi = await partition(low, high);
await quickSort(low, pi - 1);
await quickSort(pi + 1, high);
}
}
async function partition(low, high) {
let pivot = array[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
[array[i], array[j]] = [array[j], array[i]];
render();
playSound();
await sleep(30);
}
}
[array[i + 1], array[high]] = [array[high], array[i + 1]];
render();
return i + 1;
}
/* ---------------- STALIN SORT ---------------- */
function stalinSort() {
if (isSorting) return;
let newArray = [array[0]];
for (let i = 1; i < array.length; i++) {
if (array[i] >= newArray[newArray.length - 1]) {
newArray.push(array[i]);
}
}
array = newArray;
render();
playSound(); // π finish
}
function testSound() {
completeSound.currentTime = 0;
completeSound.play();
}
/* ---------------- EXPORT ---------------- */
window.generateArray = generateArray;
window.bubbleSort = bubbleSort;
window.selectionSort = selectionSort;
window.insertionSort = insertionSort;
window.mergeSortWrapper = mergeSortWrapper;
window.quickSortWrapper = quickSortWrapper;
window.stalinSort = stalinSort;