← back to unit
Medium
# sorting medium
One Bubble Pass
Walk the line once, left to right: whenever two neighbors are in the wrong order, swap them on the spot. 🫧
You get a list of nums. Do exactly ONE such pass and print the result, comma-separated. (Watch the biggest number bubble to the end!)
Example:
Input: nums = [5, 1, 4, 2, 8]
Output: 1,4,2,5,8
Walking left to right and swapping bad neighbors: 5,1 swaps to 1,5; then 5,4 swaps to 4,5; then 5,2 swaps to 2,5; then 5,8 is already fine. After one pass, 8 — the biggest — has bubbled all the way to the end.
💡 need a hint?
sort-one-bubble-pass.py🔒 given lines are locked — write your code in between
loading...