Bubble Sort JavaScript - JavaScript bubblesort

Bubble Sort in Javascript:

Basic of the code: 
const: const is the keyword of javascript language. This keyword is used to constant a variable or variables or a function, which will not be changed in the program running process.
let: It is also the keyword of javascript. The let is used to define a variable in a block. Most of the programmer love to use the keyword.
length: This is the built-in variable class function of javascript, which is used to find the length of an array or string following a dot used before the function.
console.log: A print function used to give the output in the console of a browser.
window.alert: Basically an output function that is used to give a message in the pop-up window for a browser.

The code body: 

const DataArray = [2, 5, 1, 4, 3];

let i, j, temp;

for (i=0; i<DataArray.length; i++)

    {

        for (j=0; j<DataArray.length; j++)

        {

            if (DataArray[j+1] < DataArray[j])

            {

                temp = DataArray[j];

                DataArray[j] = DataArray[j+1];

                DataArray[j+1] = temp;

            }

        }

    }

    

//output in the console of the instpect
console.log(DataArray)

//output in the pop up window
window.alert(DataArray)
//End of the code body

Sample Input: 
2, 5, 1, 4, 3

Sample Output:
1, 2, 3, 4, 5


Related post:

Bubble Sort in C++ with algorithm

Bubble Sort Python

Post a Comment

Previous Post Next Post