How To Visualize Sorting Algorithm In Javascript | Bubble Sort - letsbug

    One of the most  important thing that we have learnt over the years while learning in data structures and algorithms is that as important is to understand it and to use it. One should also know how to visualize these data structures and algorithms. 

    And we've got you covered in this article we are visualizing the bubble sort algorithm in javascript. And for this we are taking a help of a Javascript library called P5.js you can learn more about it by clicking here. 

    Let me give you a brief description about P5.js to you. Its is a library that helps you in your canvas, animation and simulation projects. In this project we have used downloaded the library but there is a cdn link that you can use in your project. And if you want to learn about the bubble sort algorithm click here. 

    So let's start out project 

Bubble Sort Algorithm Visualizer

  1. Create a project folder.
  2. Create index.js, index.html files in it.
  3. Copy the html code in index.html and javascript code in index.js


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
    content="width=device-width, initial-scale=1.0">
    <title>Visualizer</title>
    <script src="./p5/p5.js"></script>
    <script src="./index.js"></script>
</head>
<body>
</body>
</html>

Above Code is HTML code

let arr = []
let states = []
let i = 0;
let j = 0;

const width = 500, height = 400
let lineWidth, noOfElements = 30
function setup(){
    createCanvas(width, height)
    lineWidth = width / noOfElements
    for(let i = 0; i < noOfElements; i++){
        arr[i] = random(noOfElements)
        states[i] = -1
    }
}              

function draw(){
    background(25)

    if(arr[j] > arr[j + 1]){
        let temp = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
        states[j] = 0
    }
    j = j + 1
    states[j]= -1
    if(i < arr.length){
        if(j >= arr.length - i - 1){
            j = 0
            i = i + 1
        }
    }else{
        noLoop()
    }

    for(let i = 0; i < noOfElements; i++){
        switch (states[i]) {
            case 0:
                fill(200,100,50)
                break;
            default: fill(100,200,50)
                break;
        }
        rect(i*lineWidth,height, lineWidth, -arr[i]*10)        
    }
}

Above is Javascript Code


Below is the output

bubble sort algorithm visualizer in javascript
Output When Sorting Completed


Bubble sort Algorithm Visualizer In Javascript
Output While sorting


Comments

Categories

Big Data Analytics Binary Search Binary Search Tree Binary To Decimal binary tree Breadth First Search Bubble sort C Programming c++ Chemical Reaction and equation class 10 class 10th Class 9 Climate Complex Numbers computer network counting sort CSS Cyber Offenses Cyber Security Cyberstalking Data Science Data Structures Decimal To Binary Development diamond pattern Digital Marketing dust of snow Economics Economics Lesson 4 Email Validation English fire and ice Food Security in India Footprints Without feet Forest And Wildlife Resources game Geography Geography lesson 6 glassmorphism Glossary Graph HackerRank Solution hindi HTML image previewer India-Size And Location Insertion Sort Internet Network Status Interview Questions Introduction to cyber crime and cyber security IT javascript tricks json to CSV converter lesson 2 lesson 1 lesson 2 Lesson 3 Lesson 6 lesson 7 Life lines of National Economy life processes Linear Search Linked List lowest common ancestor Machine Learning MCQs median in array Merge sort min and max of two numbers Moment Money and Credit My Childhood Natural Vegetation and Wildlife NCERT Network connectivity devices Network Models Network Security No Men Are foreign Node.js operator overloading P5.js PHP Physical features of India Population Prime Numbers python Quick sort R language Rain on the roof Regular Expression Resources and development reversing array saakhi science Searching Algorithm Selection sort Social Media Marketing social science Software Engineering Software Testing Sorting Algorithm Stacks staircase pattern System Concepts Text Recognition The last Leaf time converter Time Passed From A Date Todo List App Tree Trending Technologies Understanding Economic Development username and password video player Visualization water resources Wired And Wireless LAN साखी
Show more

Popular Posts

Big Data MCQs(multiple choice questions) with answers - letsbug

Digital Marketing MCQ(Multiple Choice Questions) with Answers | part 1 | letsbug

Software Engineering MCQs questions with answers - letsbug