Algorithms - Implement Bubble Sort

Tell us what’s happening:

I wrote this code. I don’t what’s wrong with this log. It is returning sorted array. Still the tests are not passing.

Your code so far

function bubbleSort(array) {
  // Only change code below this line
  n = array.length
    for (let pass = n-1; pass >0; pass--) {
        
        for (let j = 0;j<pass;j++)
        {
            if(array[j]>array[j+1])
            {
                temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
      
    }
  return array;
  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Algorithms - Implement Bubble Sort

I don’t think you are seeing something that you should be. Try adding the following below the function:

console.log(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]));

And then make sure you look in the console pane.

Don’t forget to define your variable before you use them.
e.g. let count = 1

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.