01Bubble sort: swap neighbours
Walk through the array and compare each pair of neighbours. If they are in the wrong order, swap them.
After one full pass, the largest value has been carried all the way to the end, like a bubble rising to the surface. It is now in its final place, so the next pass can stop one step earlier.
The invariant: after pass k, the last k positions hold the k largest values in order. Repeat n − 1 passes and everything is sorted. If a pass makes no swaps, the array is already sorted and you can stop early; that makes bubble sort O(n) on sorted input.