p5.js | Bubble Sort

Bubble 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.
- Create a function for bubble sort which compares the adjacent bar in order to swap the bar.
- In order to get better visualization, set the color to the bar which is currently being processed (red in example shown below).
Example:
| <!DOCTYPE html> <html>  Â<head>     <title>Bubble Sort</title>      Â    <meta charset="UTF-8">  Â    <script src=     type="text/javascript"></script>      Â    <style>          body {             padding: 0;  Â        }         canvas {             vertical-align: top;         }      </style> </head>   Â<body>     <script type="text/javascript">      Â        // Set Global Variables          let values = [];         let w = 20;           Â        // To store the state of each bar         // in order to change the color         let states = [];           Â          Â        functionsetup() {              Â            // Create Canvas of Size Windows             // Width * Windows Height             createCanvas(800, 400);           Â            // Insert Random values in array             values = newArray(floor(width/w));           Â            for(let i = 0; i < values.length; i++) {                 values[i] = float(random(height));                 states[i] = -1;              }           Â            // Print Unsorted Array             print("Unsorted Array:"+ values);           Â            // Call to bubble sort function             bubbleSort(values, 0, values.length);           Â            // Print Sorted Array             print("Sorted Array:"+ values);           Â        }           Â        // Definition of bubble sort         async functionbubbleSort(arr, start, end) {             if(start >= end) {                 return;             }            Â            for(vari = 0; i < end-1; i++) {                 for(varj = 0; j < end-i-1; j++) {                     if(arr[j] >= arr[j+1]) {                         states[j] = 1;           Â                        // Call to swap function                         await swap(arr, j, j+1);                         states[j+1] = 0;                     }                     states[j] = 2;                 }             }             returnarr;         }           Â        // Definition of draw function         functiondraw() {             background(51);              Â            for(let i = 0; i < values.length; i++) {                 stroke(0);                 fill(255);                  Â                if(states[i] == 0) {                     fill(255, 0, 0);                 }                 elseif(states[i] == 1) {                      Â                    // Element currently sorting                     fill("#58FA82");                 }                 else{                     fill(255);                 }                 rect(i*w, height - values[i], w, values[i]);             }         }           Â        // Definition of swap function         async functionswap(arr, a, b) {             Â            await sleep(20);             let t = arr[a];             arr[a] = arr[b];             arr[b] = t;         }           Â        // Definition of sleep function         functionsleep(ms) {             returnnewPromise(resolve => setTimeout(resolve, ms));         }     </script> </body>  Â</html>              | 
Output:
 
				 
					



