Bubble Sort
Adjacent swaps that grow a sorted suffix one pass at a time
Move the largest remaining value right
Bubble Sort scans adjacent pairs from left to right. Whenever the left value is larger, the pair swaps. After one complete pass, the largest value in the unsorted prefix has moved to the end, where it will never move again.
Read the two pointers
The arrows mark the current adjacent pair. A comparison highlights both bars, a swap changes their stable positions, and the green suffix records values already fixed. Watch how one large value can move several places during a single pass.
Start pass 1; the largest remaining value will reach the unsorted suffix.
Cost and limits
This direct version performs O(n^2) comparisons and uses O(1) auxiliary space. It is stable because equal values are never swapped. An early-exit flag can make an optimized version linear on already sorted input, but it remains a poor choice for large arrays.
p, the last p positions contain the correct largest values in sorted order. Continue with Merge Sort to replace repeated adjacent work with structured merging, or compare all sorting costs in the complexity reference.