Problem 374030 · easy · Phase 03 Linear Management & Searching

Bubble Sort Pass Count

sorting · bubble sort · early exit

Bubble sort repeatedly walks the list from left to right and swaps any adjacent pair a[j] > a[j+1]. Each full left-to-right walk is a pass. With the early exit optimisation the algorithm stops right after the first pass in which no swap happened.

Return the total number of passes performed, counting that final swap-free pass. For lists with fewer than two elements return 0 (no pass is needed).

Examples

Input:  nums = [3, 1, 2]
Output: 2
Explanation: Pass 1 swaps twice and yields [1, 2, 3]; pass 2 makes no swap, so the sort stops.
Input:  nums = [1, 2, 3]
Output: 1
Explanation: The very first pass makes no swap.

Constraints

  • 0 <= len(nums) <= 600
  • Target complexity: O(n^2) in the worst case.

Goals

  • Implement bubble sort with a swapped flag
  • Stop as soon as a full pass makes no swap
  • Count passes precisely, including the final confirming pass
Starting Python…