Spot the bug: the last pair
Standard · Find the bug · about 6 min
This bubble sort leaves some arrays unsorted. Click the line with the bug, then fix it.
Example 1
Input:
[[2,1]]
Output:
[1,2]
The buggy code returns [2, 1].
Constraints
- 0 ≤ nums.length ≤ 1,000
Learn the idea first: Bubble sort, a visual lesson.
More Bubble sort problems
- State after one pass Warm-up
- Count swaps Warm-up
- Implement bubble sort with early exit Standard
- Array after k passes Standard
- How many swaps? Standard
- Count passes until sorted Challenge