Count passes until sorted
Challenge · Trace the algorithm · about 8 min
Bubble sort with early exit runs on [4, 5, 6, 1, 2, 3]. Predict how many passes it runs, counting the final pass that finds no swaps.
Try to answer without simulating every comparison.
Example 1
Input:
{"values":[4,5,6,1,2,3]}
Output:
4
Constraints
- Answer with a whole number.
- A pass with no swaps ends the sort.
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
- Spot the bug: the last pair Standard