Bubble Sort algorithm in JavaScript | Data Structure &algorithm| Bubble Sort implementation

Deal Score0
Deal Score0
Spread the love

The bubble sort algorithm sorts an array by comparing two adjacent elements and swapping them if they are not in the correct order. The order here can be anything from rising to decreasing.

What is Bubble-sort?

var arr = [23, 423, 515, 631, 51, 16, 25, 57]

is an unsorted string. The task is to use bubble sort to sort the list.

Bubble sort compares the element from index 0 and swaps the values if the 0th index is less than the 1st index; if the 0th index is greater than the 1st index, nothing happens.

The 1st index is then compared to the 2nd index, which is then compared to the 3rd index, and so on.

// bubbleSort function
function bubbleSort(arr){
for(var i = 0; i < arr.length; i++){
// Last i elements are already in place
for(var j = 0; j < ( arr.length - i); j++){
    if(arr[j] > arr[j+1]){
    // If number is greater then condition is true
    var temp = arr[j]
    arr[j] = arr[j + 1]
    arr[j+1] = temp
//  sorted array

// This is our unsorted array
var arr = [23, 423, 515, 631, 51, 16, 25, 57];

// Now pass this array to the bblSort() function

Our Free online tools JPG to PNG – Convert PNG to jpg

Convert your images from JPG to PNG-PNG to jpg-bmp TO PNG or bmp to jpg online tools and for free to unlimited image size.


We will be happy to hear your thoughts

Leave a reply

Enable registration in settings - general