Bubble sort algorithm JS

Sort and array with Bubble sort algorithm in JS

·

3 min read

Hi!

This time I will try to sort an array with bubble sort algorithm.

How algorithm works

As an example we have array below. We check first two elements, if first element is bigger than second we swap them places and go to next pair.

[34, 16, 879, 1, 10, 15, 105, 150]

So here we swapped 34 with 16. Next we check 34 and 879.

[16, 34, 879, 1, 10, 15, 105, 150]

Our number 34 is not bigger than 879 so we do not swap them.

[16, 34, 879, 1, 10, 15, 105, 150]

In next check we have 879 and 1, where first is bigger than second so we swap them.

[16, 34, 1 , 879, 10, 15, 105, 150]

We continue doing this until our array will be sorted.

Algorithm logic JS

In the example above, our third check showed us that iterating through all elements of the array once will not be enough. We need to have numbers of checks for full array of its length.

As we could imagine the lowest number could be placed last. So for our algorithm to put it first we need to have two loops.

Lets present this on simple example.

[3, 2, 1 ] - first pass through every element on our array will create [2, 1, 3 ]

[2, 1, 3 ] - second pass through every element on our array will create [1, 2, 3 ]

So our array will be sorted on second pass.

Important here will be our condition to check when we know if array is already sorted. When we check our array and no numbers will be swapped then we know we can finish our sorting.

We can start with looping through our array. I wanted to be sure that we sort every element so I took arr.length as our final number for our loops. This will not be an issue if we finish sooner because we will check when our array is sorted.

function bblSort(arr){

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

To do this I created boolean swaps variable with false value as default.

What we need to do is in our if statement when we swap elements we also set swap to true, so we know we need to continue sorting.

After the inner loop we check if there were any swaps in our array. If we had no swaps we can return our sorted array.

But if there were swaps we go back to our outer loop. Then again we need to change swap variable to default and start sorting again.

function bblSort(arr){
  let swaps = false

  for(let j=0; j< arr.length; j++){
    swaps = false
    for(let i=0; i< arr.length; i++){      
      if(arr[i] > arr[i+1]) {
        [arr[i], arr[i+1]] = [arr[i+1], arr[i]]
        swaps = true
      }      
    }
    if(swaps == false) return arr
  }  
}

And this should be it. Now to check our sort function we can call it like that.

const arr = [0, 2, 5, 7, 10, 34, 16, 879, 1, 10, 15, 105, 150]
console.log(bblSort(arr));

Our output should look like this: [ 0, 1, 2, 5, 7, 10, 10, 15, 16, 34, 105, 150, 879 ]

If you find any mistakes or you have ideas how to refactor my solutions to be simplier/ better please let me know :)

Did you find this article valuable?

Support Chris by becoming a sponsor. Any amount is appreciated!