-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.js
53 lines (46 loc) · 2.04 KB
/
bubbleSort.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
49
50
51
52
53
// Bubble sort algorithm (optimized)
async function bubbleSortAlgo() {
let noSwaps // this valirable will store whether at least 1 swap has been performed in a pass
for (let i = 0; i < barArray.length - 1; i++) {
noSwaps = true // default it to true
for (let j = 0; j < barArray.length - i - 1; j++) {
barArray[j].classList.add("activeBar");
barArray[j + 1].classList.add("activeBar");
// check for reset
if (resetButton) {
if (i > 0) {
barArray[barArray.length - i].classList.remove("sortedBar");
}
barArray[j].classList.remove("activeBar");
barArray[j + 1].classList.remove("activeBar");
return;
}
// check for pause
if (pauseButton) {
await waitForUnPause();
pauseButton = false;
}
await new Promise(resolve => setTimeout(() => { resolve(); }, interval));
// the comparison and swap
if (integerArray[j] > integerArray[j + 1]) {
swapBars(j, j + 1);
swapInts(j, j + 1);
noSwaps = false // if a swap has been performed
}
await new Promise(resolve => setTimeout(() => { resolve(); }, interval));
barArray[j].classList.remove("activeBar");
barArray[j + 1].classList.remove("activeBar");
}
//await new Promise(resolve => setTimeout(() => { resolve(); }, interval));
barArray[barArray.length - i - 1].classList.add("sortedBar");
// if no swaps were performed, we know the array is sorted, so finish early
if (noSwaps) {
// mark all remaining bars as sorted
for (let k = barArray.length - i - 2; k >= 0; k--) {
barArray[k].classList.add('sortedBar')
}
break
}
}
barArray[0].classList.add("sortedBar"); // this line sets the leftmost bar to sorted at the end of the algorithm
}