DEV Community

Aashish Panchal
Aashish Panchal

Posted on

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])

Top comments (0)