One of the comparison - based sorting algorithms is the Bubble Sort. Basically, in bubble sort, it compares two adjacent elements. Once compared, the bubble sort algorithm will sort them if they are in the wrong order. Smaller elements will "bubble" to the top of the list in every iteration, hence the name of the sorting algorithm, the bubble sort. Bubble sort may be simple but not efficient in handling more extensive lists and datasets.
Referring to the picture below, bubble sort is applied to the array.

The original and unordered array is composed of [7,6,4,3]. Through the bubble sort of comparing two adjacent elements, the first element, 7, is compared to the second element, 6. Since the first element, 7, is greater than the second element, 6, they will swap their places. This will result in [6,7,4,3]. Next, continue comparing the next two adjacent elements. This time, the elements will be 7 and 4. Since 7 is greater than 4, then they will swap places. This will result in [6,4,7,3]. Next would be the comparison between 7 and 3. Since 7 is greater than 3, they will swap places, resulting in [6,4,3,7]. This is the end of the first iteration.
After the first iteration, the resulting array, [6,4,3,7], still needs to be sorted. The bubble sort will continue. Repeat the steps in the first iteration. First, compare the first two adjacent elements. This time, it is 6 and 4. Since 6 is greater than 4, then they will swap places, resulting in the array [4,6,3,7]. The next adjacent elements to be compared will be 6 and 3. Since 6 is greater than 3, then they will swap places, resulting in the array [4,3,6,7]. Lastly, the last two adjacent elements will be compared. Since 6 and 7 are in the correct order, there will be no swapping of elements, and the array will be retained. The final resulting array of the second iteration is [4,3,6,7].
Even after the second iteration, the array [4,3,6,7] still needs to be sorted. Still the same with the previous iterations, compare the first two adjacent elements in the array. This time, the elements are 4 and 3. Since 4 is greater than 3, they will swap their places. This will lead to an array of [3,4,6,7]. This time, the final resulting array is sorted.
As seen in the example, bubble sort prioritizes having the largest element in its proper place. Its inefficiency can also be seen in how many iterations it took to finish sorting a four - element array since the bubble sort algorithm only focuses on two adjacent elements. The two adjacent elements will be the only ones to be compared to each other, not minding the other elements in the array. This resulted in some elements to be still out of order on the left side of the array. Moreover, the time complexity in the bubble sort algorithm is n raised to 2 or n squared, where n is the number of elements in the array or list. Therefore, bubble sort can not be recommended as a sorting algorithm for larger lists due to its slow performance.
There are common use cases and examples of bubble sort algorithms despite their performance:
It can be used for educational purposes for the students to learn basic sorting algorithms.
If code simplicity is favored over speed in sorting small arrays and lists.
The bubble sort algorithm can serve as a foundation for understanding how sorting algorithms function.
Overall, the bubble sort algorithm is a simple and basic comparison - based sorting algorithm. It is simple in the sense that it will only compare and swap two adjacent elements until the whole array or list is sorted. However, it is quite inefficient and is not recommended for larger lists.
Posted using Honouree