| 51 | |
| 52 | // Asynchronous BubbleSort function |
| 53 | async function BubbleSort(delay = 100) { |
| 54 | var blocks = document.querySelectorAll(".block"); |
| 55 | |
| 56 | // BubbleSort Algorithm |
| 57 | for (var i = 0; i < blocks.length; i += 1) { |
| 58 | for (var j = 0; j < blocks.length - i - 1; j += 1) { |
| 59 | |
| 60 | // To change background-color of the |
| 61 | // blocks to be compared |
| 62 | blocks[j].style.backgroundColor = "#FF4949"; |
| 63 | blocks[j + 1].style.backgroundColor = "#FF4949"; |
| 64 | |
| 65 | // To wait for .1 sec |
| 66 | await new Promise((resolve) => |
| 67 | setTimeout(() => { |
| 68 | resolve(); |
| 69 | }, delay) |
| 70 | ); |
| 71 | |
| 72 | console.log("run"); |
| 73 | var value1 = Number(blocks[j].childNodes[0].innerHTML); |
| 74 | var value2 = Number(blocks[j + 1] |
| 75 | .childNodes[0].innerHTML); |
| 76 | |
| 77 | // To compare value of two blocks |
| 78 | if (value1 > value2) { |
| 79 | await swap(blocks[j], blocks[j + 1]); |
| 80 | blocks = document.querySelectorAll(".block"); |
| 81 | } |
| 82 | |
| 83 | // Changing the color to the previous one |
| 84 | blocks[j].style.backgroundColor = "#00ccff"; |
| 85 | blocks[j + 1].style.backgroundColor = "#00ccff"; |
| 86 | } |
| 87 | |
| 88 | //changing the color of greatest element |
| 89 | //found in the above traversal |
| 90 | blocks[blocks.length - i - 1] |
| 91 | .style.backgroundColor = "#13CE66"; |
| 92 | } |
| 93 | // To enable the button "Generate New Array" after final(sorted) |
| 94 | document.getElementById("Button1").disabled = false; |
| 95 | document.getElementById("Button1").style.backgroundColor = "#6f459e"; |
| 96 | |
| 97 | // To enable the button "Bubble Sort" after final(sorted) |
| 98 | document.getElementById("Button2").disabled = false; |
| 99 | document.getElementById("Button2").style.backgroundColor = "#6f459e"; |
| 100 | } |
| 101 | |
| 102 | // Calling generatearray function |
| 103 | generatearray(); |