Elementary Sorts & the Play Chart
In short: three simple sorts all compare neighbours or scan for the
smallest, and all cost O(n²) in the worst case — but they differ in whether
ties keep their order (stability) and how they behave on data that's
almost sorted already. Tunebox's Play Chart ranks tracks by play count; in
production that's one call to sorted(), but which sort you'd hand-roll
decides what happens to ties and to a chart that just had one swap.

Rank the Play Chart with bubble sort
Walk the list comparing each pair of neighbours; swap any pair that's out of order. Each pass carries the largest remaining value to the end, like a bubble rising — and a pass with no swaps proves the list is already sorted, so you can stop early:
Each pass carries the largest unplaced value to the end, and a pass with no swaps proves the list is sorted.
Pass 1 starts at the front, where 5 is bigger than 1, so this pair is out of order. 5 cells. j at index 0. Comparing 5 and 1.
The mistake: thinking one pass finishes more than it does. One pass only guarantees the largest value reaches the end — everything else just shuffles one place:
nums = [5, 1, 4, 2]
for j in range(len(nums) - 1):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
print(nums)