Colors: Yellow = Current element, Purple = Left element, Red = Upper boundary, Blue = Comparing, Green = Sorted
Bubble Sort is a sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order, "bubbling" larger elements to the end.
How it works:
Time Complexity: O(n²) - compares adjacent pairs repeatedly
Scenario: A teacher needs to sort student test scores for a class of 15 students to display grades in ascending order for a quick analysis of class performance.
Input: Test scores [78, 92, 65, 88, 95, 72, 85, 90, 68, 91, 74, 87, 81, 89, 77]
Process: Compare adjacent scores, swap if left > right, repeat passes until array is sorted
Output: Sorted scores [65, 68, 72, 74, 77, 78, 81, 85, 87, 88, 89, 90, 91, 92, 95]
Bubble sort excels in this scenario because the dataset is small enough that its O(n²) complexity is negligible, the algorithm is easy to understand and verify, and the in-place sorting doesn't require extra memory. Teachers often prefer bubble sort for small classroom datasets because it's straightforward to trace through, making it ideal for demonstrating sorting logic. While not suitable for large databases, it's perfect for small-scale applications where code simplicity is valued.
Benefits: Simple implementation, in-place sorting, good for learning, stable algorithm, works well on small datasets