|
| 1 | +"use scrict"; |
| 2 | + |
| 3 | +const swapQuick = async function (i, j) { |
| 4 | + const bar1 = barsEl.children[i]; |
| 5 | + const bar2 = barsEl.children[j]; |
| 6 | + |
| 7 | + const bar1Height = Number(bar1.style.height.slice(0, -1)); |
| 8 | + const bar2Height = Number(bar2.style.height.slice(0, -1)); |
| 9 | + const temp = bar1Height; |
| 10 | + const t2 = bar1.children[0].innerText; |
| 11 | + bar1.style.height = `${bar2Height}%`; |
| 12 | + bar1.children[0].innerText = bar2.children[0].innerText; |
| 13 | + bar2.style.height = `${temp}%`; |
| 14 | + bar2.children[0].innerText = t2; |
| 15 | + |
| 16 | + const temp2 = bars[i]; |
| 17 | + bars[i] = bars[j]; |
| 18 | + bars[j] = temp2; |
| 19 | +}; |
| 20 | + |
| 21 | +const partition = async function (l, r) { |
| 22 | + const sz2 = Number(barsEl.childElementCount); |
| 23 | + for (let j = 0; j < sz2; ++j) |
| 24 | + if (j < l || j > r) barsEl.children[j].style.opacity = "0.5"; |
| 25 | + |
| 26 | + let barDefaultColor = barsEl.children[0].style.backgroundColor; |
| 27 | + let barDefaultBorderColor = barsEl.children[0].style.borderColor; |
| 28 | + |
| 29 | + const pvt = bars[r]; |
| 30 | + let i = l - 1; |
| 31 | + barsEl.children[i + 1].children[0].style.backgroundColor = "#FF2171"; |
| 32 | + barsEl.children[i + 1].style.borderColor = "blue"; |
| 33 | + |
| 34 | + for (let j = l; j < r; ++j) { |
| 35 | + if (bars[j] < pvt) { |
| 36 | + ++i; |
| 37 | + |
| 38 | + barsEl.children[i].style.backgroundColor = "red"; |
| 39 | + barsEl.children[j].style.backgroundColor = "red"; |
| 40 | + |
| 41 | + swapQuick(i, j); |
| 42 | + |
| 43 | + await waitforme(Number(speedSlider.value)); |
| 44 | + |
| 45 | + barsEl.children[i].children[0].style.backgroundColor = |
| 46 | + barDefaultColor; |
| 47 | + barsEl.children[i].style.borderColor = barDefaultBorderColor; |
| 48 | + barsEl.children[i].style.backgroundColor = barDefaultColor; |
| 49 | + barsEl.children[j].style.backgroundColor = barDefaultColor; |
| 50 | + |
| 51 | + barsEl.children[i + 1].children[0].style.backgroundColor = |
| 52 | + "#FF2171"; |
| 53 | + barsEl.children[i + 1].style.borderColor = "blue"; |
| 54 | + } |
| 55 | + } |
| 56 | + barsEl.children[i + 1].style.backgroundColor = "red"; |
| 57 | + barsEl.children[r].style.backgroundColor = "red"; |
| 58 | + |
| 59 | + swapQuick(i + 1, r); |
| 60 | + |
| 61 | + await waitforme(Number(speedSlider.value)); |
| 62 | + |
| 63 | + barsEl.children[i + 1].children[0].style.backgroundColor = barDefaultColor; |
| 64 | + barsEl.children[i + 1].style.borderColor = barDefaultBorderColor; |
| 65 | + barsEl.children[i + 1].style.backgroundColor = barDefaultColor; |
| 66 | + barsEl.children[r].style.backgroundColor = barDefaultColor; |
| 67 | + |
| 68 | + barsEl.children[i + 1].children[0].style.backgroundColor = barDefaultColor; |
| 69 | + barsEl.children[i + 1].style.borderColor = barDefaultBorderColor; |
| 70 | + |
| 71 | + for (let j = 0; j < sz2; ++j) |
| 72 | + if (j < l || j > r) barsEl.children[j].style.opacity = "1.0"; |
| 73 | + |
| 74 | + return i + 1; |
| 75 | +}; |
| 76 | + |
| 77 | +const quickS = async function (l, r) { |
| 78 | + if (l >= r) return; |
| 79 | + |
| 80 | + let pi = await partition(l, r); |
| 81 | + |
| 82 | + await quickS(l, pi - 1); |
| 83 | + await quickS(pi + 1, r); |
| 84 | +}; |
| 85 | + |
| 86 | +const quickSort = async function () { |
| 87 | + tempDisable(); |
| 88 | + const barsEl = document.getElementById("bars"); |
| 89 | + const sz = Number(barsEl.childElementCount); |
| 90 | + |
| 91 | + await quickS(0, Number(sz - 1)).then(() => doGreen(sz)); |
| 92 | + document.getElementById("new-array").classList.remove("disabled"); |
| 93 | + document.getElementById("size").classList.remove("disabled"); |
| 94 | +}; |
| 95 | + |
| 96 | +const btnQuickSort = document.getElementById("quick"); |
| 97 | +btnQuickSort.addEventListener("click", quickSort); |
0 commit comments