Implement Bubble Sort

Implement Bubble Sort
0

#1

Tell us what’s happening:

what is wrong?

Your code so far


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

  // change code above this line
  return array;
}

// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/implement-bubble-sort


#2

increment should be j++


#3

figured out…, I don’t know in what hurry I posted this,

the right solution is

 for(let i=0;i<array.length; i++){
   for(let j=0;j<(array.length-i-1); j++){
     if(array[j]>array[j+1]){
         let temp = array[j+1];
         array[j+1]=array[j];
         array[j]= temp;
     }
 }

}


#4

Not needed. Just j++ in inner loop is enough. Here’s a pen to prove it.


#5

true. Noted later on. Thank you :slight_smile: