-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
239 lines (208 loc) · 6.88 KB
/
script.js
File metadata and controls
239 lines (208 loc) · 6.88 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
const AudioContext = window.AudioContext || window.webkitAudioContext
const ARRAY = [];
const SORTVIEW = document.querySelector('#sortView');
function beepFallback() { }
function audioContextBeep(index) {
if (document.querySelector('#disableSound').style.display === 'none') return;
const a = new AudioContext();
const v = a.createOscillator();
const u = a.createGain();
v.connect(u);
v.frequency.value = ARRAY[index] * 10;
v.type = 'square';
u.connect(a.destination);
u.gain.value = 1;
v.start(a.currentTime);
v.stop(a.currentTime + 0.01);
}
const beep = AudioContext ? audioContextBeep : beepFallback;
function clearDisplay() {
const c = SORTVIEW.getContext('2d');
c.fillStyle = 'white';
c.fillRect(0, 0, SORTVIEW.width, SORTVIEW.height);
}
async function displayArray() {
await (timeoutDelay(2000 / ARRAY.length));
clearDisplay();
const c = SORTVIEW.getContext('2d');
c.fillStyle = 'black';
const itemWidth = SORTVIEW.width / ARRAY.length;
for (let i = 0; i < ARRAY.length; i += 1) {
const itemHeight = SORTVIEW.height / ARRAY.length * ARRAY[i];
const horizontalStartPoint = i * itemWidth;
c.fillRect(horizontalStartPoint, SORTVIEW.height - itemHeight, itemWidth, itemHeight);
}
}
function highlightItem(index, color) {
const c = SORTVIEW.getContext('2d');
const itemWidth = SORTVIEW.width / ARRAY.length; // make global??
const itemHeight = SORTVIEW.height / ARRAY.length * ARRAY[index];
const horizontalStartPoint = index * itemWidth;
c.fillStyle = color;
c.fillRect(horizontalStartPoint, SORTVIEW.height - itemHeight, itemWidth, itemHeight);
}
async function initArray(length) {
while (ARRAY.length > 0) {
ARRAY.shift();
}
for (let i = 0; i < length; i += 1) {
ARRAY.push(i);
await timeoutDelay(3000 / length);
highlightItem(i, 'blue');
beep(i);
displayArray();
}
}
async function shuffle() {
// Fisher Yates' algorithm https://medium.com/@nitinpatel_20236/how-to-shuffle-correctly-shuffle-an-array-in-javascript-15ea3f84bfb
for (let i = ARRAY.length - 1; i > 0; i -= 1) {
const randomIndex = Math.floor(Math.random() * i);
const buffer = ARRAY[i];
highlightItem(randomIndex, 'red');
highlightItem(i, 'blue');
beep(i);
ARRAY[i] = ARRAY[randomIndex];
ARRAY[randomIndex] = buffer;
await displayArray();
}
}
async function insertionSort() {
let sorted;
while (!sorted) {
sorted = true;
for (let i = 0; i < ARRAY.length; i++) {
beep(i);
const el = ARRAY[i];
let j;
for (j = i - 1; j >= 0 && ARRAY[j] > el; j--) {
ARRAY[j + 1] = ARRAY[j];
highlightItem(i, 'blue');
highlightItem(j + 1, 'red');
beep(j + 1);
await displayArray();
}
ARRAY[j + 1] = el;
}
}
await displayArray();
}
async function bubbleSort() {
let sorted;
let sortedItems = 0;
while (!sorted) {
sorted = true;
sortedItems += 1;
for (let i = 0; i < ARRAY.length - sortedItems; i += 1) {
highlightItem(i, 'blue');
beep(i);
if (ARRAY[i] > ARRAY[i + 1]) {
highlightItem(i + 1, 'red');
sorted = false;
const buffer = ARRAY[i];
ARRAY[i] = ARRAY[i + 1];
ARRAY[i + 1] = buffer;
}
await displayArray();
}
}
}
// default parameters used as merge sort is recursive and is passed nothing in the
// first instance https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters
async function mergeSort(e, minIndex = 0, maxIndex = ARRAY.length) {
if (maxIndex - minIndex <= 1) {
return;
}
const middle = Math.floor(maxIndex - ((maxIndex - minIndex) / 2));
await mergeSort(null, minIndex, middle);
await mergeSort(null, middle, maxIndex);
await merge(minIndex, middle, maxIndex);
await displayArray();
}
async function merge(minIndex, middle, maxIndex) {
const leftArr = []; // instead of using arrays just swap if minIndex + i > middle + i :o
const rightArr = [];
let itemsSorted = 0;
for (let i = minIndex; i < middle; i += 1) leftArr.push(ARRAY[i]);
for (let i = middle; i < maxIndex; i += 1) rightArr.push(ARRAY[i]);
while (leftArr.length > 0 && rightArr.length > 0) {
if (leftArr[0] > rightArr[0]) {
ARRAY[minIndex + itemsSorted] = rightArr[0];
itemsSorted += 1;
rightArr.shift();
} else {
ARRAY[minIndex + itemsSorted] = leftArr[0];
itemsSorted += 1;
leftArr.shift();
}
highlightItem(minIndex, 'blue');
highlightItem(middle, 'blue');
highlightItem(maxIndex - 1, 'blue');
highlightItem(minIndex + itemsSorted, 'red');
beep(minIndex + itemsSorted);
await displayArray();
}
while (leftArr.length > 0) {
ARRAY[minIndex + itemsSorted] = leftArr[0];
beep(minIndex + itemsSorted);
itemsSorted += 1;
highlightItem(minIndex, 'blue');
highlightItem(middle, 'blue');
highlightItem(maxIndex - 1, 'blue');
highlightItem(minIndex + itemsSorted, 'red'); // TODO put items to highlight in array
await displayArray();
leftArr.shift();
}
while (rightArr.length > 0) {
ARRAY[minIndex + itemsSorted] = rightArr[0];
beep(minIndex + itemsSorted);
itemsSorted += 1;
highlightItem(minIndex, 'blue');
highlightItem(middle, 'blue');
highlightItem(maxIndex - 1, 'blue');
highlightItem(minIndex + itemsSorted, 'red');
await displayArray();
rightArr.shift();
}
await displayArray();
}
function timeoutDelay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function addEventListeners() {
const clickEvents = ['click', 'touch'];
const controller = document.querySelector('#controller');
const shuffleBtn = document.querySelector('#shuffle');
const bubbleBtn = document.querySelector('#bubble');
const mergeBtn = document.querySelector('#merge');
const insertionBtn = document.querySelector('#insertion');
const enableSoundBtn = document.querySelector('#enableSound');
const disableSoundBtn = document.querySelector('#disableSound');
const initBtn = document.querySelector('#init');
const body = document.querySelector('body');
for (const e of clickEvents) {
shuffleBtn.addEventListener(e, shuffle);
bubbleBtn.addEventListener(e, bubbleSort);
mergeBtn.addEventListener(e, mergeSort);
insertionBtn.addEventListener(e, insertionSort);
enableSoundBtn.addEventListener(e, () => {
enableSoundBtn.style.display = 'none';
disableSoundBtn.style.display = '';
});
disableSoundBtn.addEventListener(e, () => {
enableSoundBtn.style.display = '';
disableSoundBtn.style.display = 'none';
});
initBtn.addEventListener(e, () => initArray(50));
body.addEventListener(e, () => {
if (controller.style.display === '') {
controller.style.display = 'none';
} else if (controller.style.display === 'none') {
controller.style.display = '';
}
});
}
}
function init() {
addEventListeners();
}
window.addEventListener('load', init);