Brick Sort Visualization using JavaScript Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd-indexed elements, and in the even phase, we perform a bubble sort on even-indexed elements.To know more about it. Please refer to Brick Sort.GUI(Graphical User Interface) helps in better understanding than programs. In this article, we will visualize Brick Sort using JavaScript.Pre-requisites:Brick Sort.JavaScript promises.Basic HTML, CSS & JavaScript.JavaScript async/await function.Approach:First, we will generate a random array using Math.random() function.Different colors are used to indicate which elements are unsorted(sky-blue), compared(red) & sorted(lightgreen).The new array can be generated by pressing the “Ctrl+R” key.Button Brick Sort the elements using the selection sort algorithm.Finally, elements are sorted.Below is the implementation of the above approach. HTML //filename:index.html <!DOCTYPE html> <html lang="en"> <!-- Head --> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <!-- Title --> <title>Brick Sort Visualizer</title> <!-- Linking style.css --> <link href="style.css" rel="stylesheet" /> </head> <!-- Body --> <body> <section class="head"> Brick Sort Visualization </section> <section class="data-container"></section> <section id="ele"></section> <div style="margin: auto; width: fit-content"> <!-- "Brick Sort" button --> <button class="btn2" onclick="BrickSort(),disable()" id="Button2"> Brick Sort </button> </div> <!-- Linking script.js --> <script src="script.js"></script> </body> </html> CSS /*filename: style.css*/ .mySlides { display: none; } body { background-color: rgb(0, 0, 0); font-family: Verdana, sans-serif; } .head { margin-top: 20px; margin-right: 20vw; margin-left: 20vw; text-align: center; font-size: 40px; background-color: #000000; color: white; font-weight: bolder; } .data-container { width: 600px; color: aliceblue; height: 364px; position: relative; margin: 0 auto; } .bar { width: 28px; position: absolute; left: 0; bottom: 0; background-color: rgb(0, 183, 255); transition: 0.2s all ease; } .bar__id { position: absolute; top: -24px; width: 100%; text-align: center; } .btn2 { padding: 12px; font-weight: bolder; background-color: white; border-radius: 10px; color: rgb(0, 0, 0); font-size: 16px; border: white; } #ele { text-align: center; height: 35px; } JavaScript //filename:script.js const container = document.querySelector(".data-container"); // Function to generate bars function generatebars(num = 20) { // For loop to generate 20 bars for (let i = 0; i < num; i += 1) { // To generate random values from 1 to 100 const value = Math.floor(Math.random() * 100) + 1; // To create element "div" const bar = document.createElement("div"); // To add class "bar" to "div" bar.classList.add("bar"); // Provide height to the bar bar.style.height = `${value * 3}px`; // Translate the bar towards positive X axis bar.style.transform = `translateX(${i * 30}px)`; // To create element "label" const barLabel = document.createElement("label"); // To add class "bar_id" to "label" barLabel.classList.add("bar__id"); // Assign value to "label" barLabel.innerHTML = value; // Append "Label" to "div" bar.appendChild(barLabel); // Append "div" to "data-container div" container.appendChild(bar); } } // Asynchronous function to perform "Brick Sort" async function BrickSort(delay = 600) { let bars = document.querySelectorAll(".bar"); // Initialize isSorted with false let isSorted = false; while (!isSorted) { // set isSorted true isSorted = true; for (let i = 1; i <= 18; i = i + 2) { // Assigning value of ith bar into value1 let value1 = parseInt(bars[i].childNodes[0].innerHTML); // Assigning value of i+1th bar into value2 let value2 = parseInt(bars[i + 1].childNodes[0].innerHTML); if (value1 > value2) { // Provide red color to the ith bar bars[i].style.backgroundColor = "red"; // Provide red color to the i+1th bar bars[i + 1].style.backgroundColor = "red"; // Swap ith bar with (i+1)th bar let temp1 = (bars[i].style.height); let temp2 = (bars[i].childNodes[0].innerText); // To pause the execution of code for 300 milliseconds await new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); // Swap pith bar with (i+1)th bar bars[i].style.height = (bars[i + 1].style.height); bars[i].childNodes[0].innerText = (bars[i + 1].childNodes[0].innerText); bars[i + 1].style.height = temp1; bars[i + 1].childNodes[0].innerText = temp2; isSorted = false; // To pause the execution of code for 300 milliseconds await new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); // Provide skyblue color to the ith bar bars[i].style.backgroundColor = "rgb(0, 183, 255)"; // Provide skyblue color to the i+1th bar bars[i + 1].style.backgroundColor = "rgb(0, 183, 255)"; } } for (let i = 0; i <= 18; i = i + 2) { // Assigning value of ith bar into value3 let value3 = parseInt(bars[i].childNodes[0].innerHTML); // Assigning value of i+1th bar into value4 let value4 = parseInt(bars[i + 1].childNodes[0].innerHTML); if (value3 > value4) { // Provide red color to the ith bar bars[i].style.backgroundColor = "red"; // Provide red color to the i+1th bar bars[i + 1].style.backgroundColor = "red"; // Swap ith bar with (i+1)th bar let temp3 = (bars[i].style.height); let temp4 = (bars[i].childNodes[0].innerText); // To pause the execution of code for 300 milliseconds await new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); // Swap ith bar with (i+1)th bar bars[i].style.height = (bars[i + 1].style.height); bars[i].childNodes[0].innerText = (bars[i + 1].childNodes[0].innerText); bars[i + 1].style.height = temp3; bars[i + 1].childNodes[0].innerText = temp4; isSorted = false; // To pause the execution of code for 300 milliseconds await new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); // Provide skyblue color to the ith bar bars[i].style.backgroundColor = "rgb(0, 183, 255)"; // Provide skyblue color to the i+1th bar bars[i + 1].style.backgroundColor = "rgb(0, 183, 255)"; } // To pause the execution of code for 300 milliseconds new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); } // To pause the execution of code for 300 milliseconds await new Promise((resolve) => setTimeout(() => { resolve(); }, 300) ); // Providing skyblue color for (var x = 0; x < 20; x++) { bars[x].style.backgroundColor = "rgb(0, 183, 255)"; } } // Providing lightgreen color for (var x = 0; x < 20; x++) { bars[x].style.backgroundColor = "rgb(49, 226, 13)"; } // To enable the button "Brick Sort" after final(sorted) document.getElementById("Button2").disabled = false; document.getElementById("Button2").style.backgroundColor = "#6f459e"; } // Call "generatebars()" function generatebars(); // Function to generate new random array function generate() { window.location.reload(); } // Function to disable the button function disable() { // To disable the button "Brick Sort" document.getElementById("Button2").disabled = true; document.getElementById("Button2").style.backgroundColor = "white"; } Output: Comment More infoAdvertise with us Next Article Brick Sort Visualization using JavaScript pankaj2407 Follow Improve Article Tags : HTML TrueGeek-2021 CSS-Properties JavaScript-Methods HTML-Questions CSS-Questions JavaScript-Questions +3 More Similar Reads Bucket Sort Visualization Using Javascript GUI(Graphical User Interface) helps in better understanding than programs. In this article, we will visualize Bucket Sort using JavaScript. We will see how the elements are stored in Buckets and then how Buckets get traversed to get the final sorted array. We will also visualize the time complexity 7 min read Heap Sort Visualization using JavaScript GUI(Graphical User Interface) helps in better understanding than programs. In this article, we will visualize Heap Sort using JavaScript. We will see how the array is first converted into Maxheap and then how we get the final sorted array. We will also visualize the time complexity of Heap Sort. Ref 4 min read Bubble Sort Visualization using JavaScript GUI(Graphical User Interface) helps in better understanding than programs. In this article, we will visualize Bubble Sort using JavaScript. We will see how the elements are swapped in Bubble Sort and how we get the final sorted array. We will also visualize the time complexity of Bubble Sort. Refer 4 min read Ternary Search Visualization using JavaScript GUI(Graphical User Interface) helps better in understanding than programs. In this article, we will visualize Ternary Search using JavaScript. We will see how the elements are being traversed in Ternary Search until the given element is found. We will also visualize the time complexity of Ternary Se 4 min read Merge Sort Visualization in JavaScript GUI(Graphical User Interface) helps users with better understanding programs. In this article, we will visualize Merge Sort using JavaScript. We will see how the arrays are divided and merged after sorting to get the final sorted array. Refer: Merge SortCanvas in HTMLAsynchronous Function in JavaSc 4 min read Gnome Sort Visualizer using JavaScript In this article, we are going to learn Gnome sort Visualizer using javascript, Gnome Sort also called Stupid sort, is based on the concept of a Garden Gnome sorting his flower pots. In order to know more about it. Please refer to Gnome Sort.An algorithm like Gnome Sort can be easily understood by vi 4 min read Pigeonhole Sort Visualizer using JavaScript Pigeonhole Sort is a simple sorting algorithm that is used to sort elements with a limited range. It is an interesting algorithm that can be easily understood and implemented. In this article, we will explore what is Pigeonhole sort, how it works, and when it can be used effectively. We will also lo 8 min read How to use HTML for Data Visualization ? In this article, we are going to discuss Data Visualization techniques that can be employed to make the communication of data easier and more effective. Data Visualization in HTML involves presenting data in graphical or visual formats using HTML, CSS, and SVG. It enhances data comprehension and ins 2 min read LeafletJS - Interacting with Maps using JavaScript Maps have become an integral part of our everyday lives. From driving to a location to finding some restaurants or stores nearby or while planning travel, almost every type of app uses maps. Using maps helps us to add location-based services in our application.One way to add maps in a web applicatio 9 min read JavaScript Quiz These JavaScript quiz questions are designed to help you test and enhance your knowledge of JavaScript, ranging from basic concepts to advanced features. These topic-specific quizzes provide a comprehensive way to practice and assess your understanding of JavaScript concepts.The JavaScript quizzes a 2 min read Like