aashish578

Aashish Panchal

Posted on October 16, 2020

Bubble sort

function bubbleSort(arr){

for(var i = 0; i < arr.length; i++){

    for(var j = 0; j < arr.length; j++){

        console.log(arr, "--" ,arr[j], "<<--SWAP TO-->> ", arr[j+1]);

        if(arr[j] > arr[j+1]){

            // SWAP....!!!!

          var temp = arr[j];

          arr[j] = arr[j+1];

          arr[j+1] = temp;

        }

    }

}

return arr;
Enter fullscreen mode Exit fullscreen mode

}

bubbleSort([87,65,45,1])

💖 💪 🙅 🚩
aashish578
Aashish Panchal

Posted on October 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related