Bubble Sort Visualization using JavaScript Last Updated : 10 May, 2023 Comments Improve Suggest changes Like Article Like Report 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: BubbleSortAsynchronous Function in JavaScript Approach: First, we will generate a random array using Math.random() function.Different colors are used to indicate which elements are being compared, sorted, and unsorted.Since the algorithm performs the operation very fast, the setTimeout() function has been used to slow down the process.The new array can be generated by pressing the "Ctrl+R" key.The sorting is performed using BubbleSort() function using the swap() function Example: Here is the implementation of the above-explained steps. Below is the program to visualize the Bubble Sort algorithm. index.html HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content= "width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> </head> <body> <br /> <p class="header">Bubble Sort</p> <div id="array"></div> <script src="script.js"></script> </body> </html> style.css: The following is the content for "style.css" used in the above file. CSS * { margin: 0px; padding: 0px; box-sizing: border-box; } .header { font-size: 20px; text-align: center; } #array { background-color: white; height: 413px; width: 598px; margin: auto; position: relative; margin-top: 64px; } .block { width: 28px; background-color: #6b5b95; position: absolute; bottom: 0px; transition: 0.2s all ease; } .block_id { position: absolute; color: black; margin-top: -20px; width: 100%; text-align: center; } script.js: The following is the content for "script.js" file used in the above HTML code. javascript var container = document.getElementById(& quot; array & quot;); // Function to generate the array of blocks function generatearray() { for (var i = 0; i & lt; 20; i++) { // Return a value from 1 to 100 (both inclusive) var value = Math.ceil(Math.random() * 100); // Creating element div var array_ele = document.createElement(& quot; div & quot;); // Adding class 'block' to div array_ele.classList.add(& quot; block & quot;); // Adding style to div array_ele.style.height = `${value * 3}px`; array_ele.style.transform = `translate(${i * 30}px)`; // Creating label element for displaying // size of particular block var array_ele_label = document.createElement(& quot; label & quot;); array_ele_label.classList.add(& quot; block_id & quot;); array_ele_label.innerText = value; // Appending created elements to index.html array_ele.appendChild(array_ele_label); container.appendChild(array_ele); } } // Promise to swap two blocks function swap(el1, el2) { return new Promise((resolve) =& gt; { // For exchanging styles of two blocks var temp = el1.style.transform; el1.style.transform = el2.style.transform; el2.style.transform = temp; window.requestAnimationFrame(function () { // For waiting for .25 sec setTimeout(() =& gt; { container.insertBefore(el2, el1); resolve(); }, 250); }); }); } // Asynchronous BubbleSort function async function BubbleSort(delay = 100) { var blocks = document.querySelectorAll(& quot;.block & quot;); // BubbleSort Algorithm for (var i = 0; i & lt; blocks.length; i += 1) { for (var j = 0; j & lt; blocks.length - i - 1; j += 1) { // To change background-color of the // blocks to be compared blocks[j].style.backgroundColor = & quot; #FF4949 & quot;; blocks[j + 1].style.backgroundColor = & quot; #FF4949 & quot;; // To wait for .1 sec await new Promise((resolve) =& gt; setTimeout(() =& gt; { resolve(); }, delay) ); console.log(& quot; run & quot;); var value1 = Number(blocks[j].childNodes[0].innerHTML); var value2 = Number(blocks[j + 1] .childNodes[0].innerHTML); // To compare value of two blocks if (value1 & gt; value2) { await swap(blocks[j], blocks[j + 1]); blocks = document.querySelectorAll(& quot;.block & quot;); } // Changing the color to the previous one blocks[j].style.backgroundColor = & quot;#6b5b95 & quot;; blocks[j + 1].style.backgroundColor = & quot;#6b5b95 & quot;; } //changing the color of greatest element //found in the above traversal blocks[blocks.length - i - 1] .style.backgroundColor = & quot;#13CE66 & quot;; } } // Calling generatearray function generatearray(); // Calling BubbleSort function BubbleSort(); Output: Comment More infoAdvertise with us Next Article Bubble sort visualizer using PyGame T tk315 Follow Improve Article Tags : Sorting Technical Scripter JavaScript Web Technologies DSA Technical Scripter 2020 JavaScript-Questions +3 More Practice Tags : Sorting Similar Reads Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Recursive Bubble Sort Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.Example: First Pass: ( 5 1 4 2 8 ) --> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) --> ( 1 4 10 min read Time and Space Complexity Analysis of Bubble Sort The time complexity of Bubble Sort is O(n^2) in the worst-case scenario and the space complexity of Bubble sort is O(1). Bubble Sort only needs a constant amount of additional space during the sorting process. Complexity TypeComplexityTime ComplexityBest: O(n)Average: O(n^2)Worst: O(n^2)Space Comple 3 min read Bubble Sort in different languagessin() in CThe sin() function in C is a standard library function to find the sine value of the radian angle. It is used to evaluate the trigonometric sine function of an angle. It takes the radian angle as input and returns the sin of that angle. It is defined inside <math.h> header file. Syntax of sin( 1 min read Bubble Sort in C++Bubble Sort Algorithm is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. It is often used to introduce the concept of a sorting and is particularly suitable for sorting small datasets.In this article, we will learn how to impleme 4 min read C++ Program for Recursive Bubble SortBackground: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Following is the iterative Bubble sort algorithm : // Iterative Bubble Sort bubbleSort(arr[], n) { for (i = 0; i n-1; i++) // Last i elements are already 2 min read Bubble Sort - PythonBubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Bubble Sort algorithm, sorts an array by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. The algorithm iterates through the a 2 min read Bubble Sort algorithm using JavaScriptBubble sort algorithm is an algorithm that sorts an array by comparing two adjacent elements and swapping them if they are not in the intended order. Here order can be anything like increasing or decreasing. How Bubble-sort works?We have an unsorted array arr = [ 1, 4, 2, 5, -2, 3 ], and the task is 4 min read p5.js | Bubble SortBubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. In order to know more about it. Please refer Bubble Sort Approach: Create an array of random values and render a bar corresponding to that value in terms of height. C 2 min read Sorting an array in Bash using Bubble sortPrerequisite: Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9 Approach : For sorting the array bubble sort is the simplest technique. Bubble sort works by swapping the adjacent elements if they 2 min read Visualization of Bubble SortSorting Algorithms Visualization : Bubble SortThe human brain can easily process visuals in spite of long codes to understand the algorithms. In this article, Bubble sort visualization has been implemented using graphics.h library. As we all know that bubble sort swaps the adjacent elements if they are unsorted and finally the larger one being 5 min read Visualizing Bubble sort using PythonPrerequisites: Introduction to Matplotlib, Introduction to PyQt5, Bubble Sort Learning any algorithm can be difficult, and since you are here at GeekforGeeks, you definitely love to understand and implement various algorithms. It is tough for every one of us to understand algorithms at the first go. 2 min read Bubble Sort Visualization using JavaScriptGUI(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 Bubble sort visualizer using PyGameIn this article we will see how we can visualize the bubble sort algorithm using PyGame i.e when the pygame application get started we can see the unsorted bars with different heights and when we click space bar key it started getting arranging in bubble sort manner i.e after every iteration maximum 3 min read Visualizing Bubble Sort using Tkinter in PythonIn this article, we will use the Python GUI Library Tkinter to visualize the Bubble Sort algorithm. Tkinter is a very easy to use and beginner-friendly GUI library that can be used to visualize the sorting algorithms.Here Bubble Sort Algorithm is visualized which works by repeatedly swapping the ad 5 min read Bubble Sort for Linked List by Swapping nodes Given a singly linked list, sort it using bubble sort by swapping nodes. Examples:Input: 5 -> 1 -> 32 -> 10 -> 78Output: 1 -> 5 -> 10 -> 32 -> 78 Input: 20 -> 4 -> 3Output: 3 -> 4 -> 20Approach: To apply Bubble Sort to a linked list, we need to traverse the list m 10 min read Sorting Strings using Bubble Sort Given an array of strings arr[]. Sort given strings using Bubble Sort and display the sorted array. In Bubble Sort, the two successive strings arr[i] and arr[i+1] are exchanged whenever arr[i]> arr[i+1]. The larger values sink to the bottom and are hence called sinking sort. At the end of each pa 4 min read Sort an array using Bubble Sort without using loops Given an array arr[] consisting of N integers, the task is to sort the given array by using Bubble Sort without using loops. Examples: Input: arr[] = {1, 3, 4, 2, 5}Output: 1 2 3 4 5 Input: arr[] = {1, 3, 4, 2}Output: 1 2 3 4 Approach: The idea to implement Bubble Sort without using loops is based o 9 min read Bubble Sort On Doubly Linked List Given a doubly linked list, the task is to sort the linked list in non-decreasing order by using bubble sort.Examples: Input : head: 5<->3<->4<->1<->2Output : head: 1<->2<->3<->4<->5Input : head: 5<->4<->3<->2Output : head: 2<-> 15+ min read Bubble sort using two Stacks Prerequisite : Bubble Sort Write a function that sort an array of integers using stacks and also uses bubble sort paradigm. Algorithm: 1. Push all elements of array in 1st stack 2. Run a loop for 'n' times(n is size of array) having the following : 2.a. Keep on pushing elements in the 2nd stack till 6 min read Like