Bubble sort belongs to a family of sorting algorithms. It is a sequential sort algorithm and an iterative approach to sorting the array or a list. Same is used for sorting the array or a list in an ascending or descending order as per the problem’s requirement. Here arises a question, why Bubble Sort is named as “Bubble”?
Now coming to answer, we all have seen air bubbles in water and how they lift up from bottom to air. There is science behind it. Bubbles have less density than water and that’s why they rush from the bottom surface to the top due to high pressure around it. This phenomenon is called Buoyancy.
Hence, air bubbles that are more buoyant than surrounding water rise to the top.
Similarly, bubble sort is worked upon the logic of lifting up the air bubble from the surrounding.
If we have unsorted elements in an array or in a list and we have to sort it in an ascending order, then we can use the bubble sort for sorting of elements. If an array contains a lesser value element then it gets lifted up and takes the position of greater one and greater will take the lesser one’s position. Also, we can say that the greater value will swap with the lesser value and the process is going on repeat again and again until the whole array or a list is not sorted.
We know that bubble sort is an iterative process, so it performs the iteration till (n-1) elements in the array to sort it.