Bubble sort is one of the fundamental forms of sorting in programming. Bubble sort algorithms move through a sequence of data (typically integers) and rearrange them into ascending or descending order one number at a time. To do this, the algorithm compares number X to the adjacent number Y. If X is higher than Y, the two are swapped and the algorithm starts over.
This process repeats until the entire range of numbers has been sorted in the desired order. For instance, if you were trying to arrange [1, 3, 2, 4] into ascending order, the bubble sort algorithm would run once, swapping the 3 with the 2.
In another matrix, however, your numbers might look like this: [3, 1, 4, 2]. In this case, the algorithm would run three times, swapping the 3 and the 1 the first time, then the 4 and the 2 the second time, and finally the 3 and the 2.
This ebook offers an in-depth look into what Agile is, how it benefits product managers specifically, some popular Agile frameworks and the best practices.
The name bubble sort comes from the fact that smaller or larger elements "bubble" to the top of a dataset. In the previous example of [3, 1, 4, 2], the 3 and 4 are bubbling up the dataset to find their proper positions.
This algorithm is alternatively called the sinking sort for the opposite reason; some of the elements are sinking to the bottom of the dataset. In our example, the 1 and the 2 are sinking elements.
Bubble sort's strong point is its simplicity. It takes just a few lines of code, is easy to read, and can be plugged in anywhere in your program. However, it's extremely inefficient for larger sets of numbers and should be used accordingly.