Implement Bubble Sort

Continuing the discussion from freeCodeCamp Challenge Guide: Implement Bubble Sort:

I tried applying the pairwise algorithm here and it worked perfectly

function bubbleSort(arr) {
  // Only change code below this line

  for (let i = 0; i < arr.length; i++){
    for (let j = i+1; j < arr.length; j++){
      let temp
      if(arr[i] > arr[j]){
        temp = arr[i];
        arr[i] = arr[j]
        arr[j] = temp
      }
    }
  }
  return arr
  // Only change code above this line
}
console.log(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]))

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

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