A teaching tool shows bubble sort one pass at a time. One pass walks from the start of the list and swaps each pair of neighbors where the left one is bigger. Pass number p (starting at 1) stops before the last p - 1 items, which are already in place.
Write afterPasses(nums, k) that returns the list after k passes of bubble sort (ascending). If the list becomes sorted earlier, extra passes change nothing. Do not change nums; return a new array.
Input: nums = [5,1,4,2,8], k = 1 Output: [1,4,2,5,8]
One pass carries 5 right until it meets the bigger 8.
Input: nums = [5,1,4,2,8], k = 2 Output: [1,2,4,5,8]
The second pass swaps 4 and 2.
0 <= nums.length <= 10000 <= k <= 1000-10^6 <= nums[i] <= 10^6Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #185 Leaderboard After K Passes. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.