Understanding the Bubble Sort Algorithm
Tags: algorithms, javascript
Published: 2021-04-27 04:14:30

My Fullstack group and I are on a journey....that journey is to become software developers and along that journey is to understand how things work. One day we started getting into algorithms and one of the first ones I had to fully understand was the bubble sort algorithm. 

What is the bubble sort algorithm and what is a practical example of it? First thing you to know that is important is what type of data is the bubble sort manipulating? The bubble sort is manipulating arrays simply no matter what type of values it contains(numbers, characters, other arrays, objects, etc.). The whole purpose of it is to swap adjacent values in ascending or descending order depending on what you want to do with the array.

What is an everyday example that a lot of people might not have noticed? One example of a bubble sort I saw somewhere online is using cars traveling on a two lane road all going one direction.
Cars will travel in one lane, and other cars will pass in the other lane. This sort is depending on who is traveling faster than the other. What will happen if I want to get in front of the car in front of me? I will pass the the car in the left hand and move back in the right hand lane traveling faster than the car I was previously behind. This is an example bubble sorting cars traveling in a lane based on who is traveling faster than the other.

So when looking at an array, you are trying to sort values in an array ascending or descending. Lets look at an example of ascending order.

Let's say I have the following array:
```
const array = [4,5,9,3,5,0,2,12]
```

How would I approach this?

Your first thought would be how do I swap the values in the array and return the array in its modified state?

I first created a shell of my function called....... *bubbleSort* that will take in an array as an argument

```
const bubbleSort = (arg) =>{

}
```

Then next within the function you will want to create a outer for loop that starts at first iteration and an inner for loop that will start at the first iteration as well.

```
const bubbleSort = (arg) =>{
  let len  = arg.length
    for (let i = 0; i < len; i++){
      for (let j = 0; j < len; j++){
        
        
       
      } 
   }
 
}
```
So the question for this part is "what is this doing?"
Trying to keep this simple.....the outer for loop is simply using the inner for loop to compare the adjacent values along the way...like in my array example, when it gets to position *array[3]* or *array[2+1]* it will check to see if its less than the value in the previous iteration. Since 3 is less than 9, they will switch places.

```
const bubbleSort = (arg) =>{
  let len  = arg.length
    for (let i = 0; i < len; i++){
      for (let j = 0; j < len; j++){
        
        
        
        if(arg[j] > arg[j + 1]){
        let temp = arg[j]
        arg[j] = arg[j + 1]
        arg[j + 1] = temp
        }
      } 
   }
  
}
```
You will notice I setup a temporary variable which this simply represents a holding place for the swapped out value until it's compared to next value in the iteration. Once all the values have been  swapped, the array will be returned in it modified form

```
const bubbleSort = (arg) =>{
  let len  = arg.length
    for (let i = 0; i < len; i++){
      for (let j = 0; j < len; j++){
        
        
        
        if(arg[j] > arg[j + 1]){
        let temp = arg[j]
        arg[j] = arg[j + 1]
        arg[j + 1] = temp
        }
      } 
   }
   return arg
}


console.log(bubbleSort(arrary))
```

Output:
```
[0,2,3,4,5,9,12]
```

I hope this was a clear enough example of a bubble sort algorithm, any feed back would be appreciated for my own fyi. Until then have a good evening everyone!