-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionSort.js
48 lines (39 loc) · 1.48 KB
/
selectionSort.js
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
// Selection sort algorithm
async function selectionSortAlgo() {
for (let i = 0; i < barArray.length - 1; i++) {
let minIndex = i;
barArray[minIndex].classList.add("activeBar");
await new Promise(resolve => setTimeout(() => { resolve(); }, interval));
for (let j = i + 1; j < barArray.length; j++) {
barArray[j].classList.add("activeBar");
// check for reset
if (resetButton) {
barArray[j].classList.remove("activeBar");
barArray[minIndex].classList.remove("activeBar");
return;
}
// check for pause
if (pauseButton) {
await waitForUnPause();
pauseButton = false;
}
await new Promise(resolve => setTimeout(() => { resolve(); }, interval));
// the comparison
if (integerArray[j] < integerArray[minIndex]) {
barArray[minIndex].classList.remove("activeBar");
minIndex = j;
barArray[minIndex].classList.add("activeBar");
} else {
barArray[j].classList.remove("activeBar");
}
}
// the swap
if (minIndex !== i) {
swapBars(i, minIndex);
swapInts(i, minIndex);
}
barArray[i].classList.add("sortedBar");
barArray[minIndex].classList.remove("activeBar");
}
barArray[barArray.length - 1].classList.add("sortedBar");
}