DEV Community

8bit Programmer
8bit Programmer

Posted on

Day 3 Bubble Sort in JS

Bubble sort is a sorting technique which sort by swapping the nearest values
Time Comp

const bubbleSort=(arr)=>{
  let n=arr.length
  for(let i=0;i<n;i++)
  {
    for(let j=0;j<n-i-1;j++)
    {
      if(arr[j] > arr[j+1])
      {
        [arr[j],arr[j+1]] = [arr[j+1],arr[j]]
      }
    }
  }
  return arr
}
console.log(bubbleSort([1,34,45,54,12,35,45,29,10,14,16]))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)