medium

Bubble Sort Pass Count

Return how many passes an early-exit bubble sort takes to finish sorting.

1. Define the problem

Bubble Sort Pass Count

Given an integer array nums, return the number of passes an early-exit bubble sort would perform before the array is sorted. A pass is one full walk through the currently-unsorted region, comparing and swapping adjacent pairs. Track whether any swap happened during a pass — the pass that finds zero swaps is the last one performed, and it counts toward the total.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -104 ≤ numsi ≤ 104

Example

Inputnums = [1, 4, 2, 3, 5]
Output2

Explanation Pass 1 makes two swaps and leaves the array sorted. Pass 2 makes zero swaps, confirming it is sorted, so it stops after 2 passes total.

2. Know the words first

In plain terms

Early-exit bubble sort
A bubble sort that stops as soon as a pass completes with no swaps, instead of always running every possible pass.
3. Visualize the solution

Count passes, stopping the moment one finds no swaps

Count passes, stopping the moment one finds no swaps
Statuspass 1

Pass 1 on [1, 4, 2, 3, 5]: 4 swaps past 2 and 3, two swaps happen, pass count = 1.

What happens in this step

pass 1, before: [1, 4, 2, 3, 5]
  compare 1, 4 → no swap
  compare 4, 2 → swap → [1, 2, 4, 3, 5]
  compare 4, 3 → swap → [1, 2, 3, 4, 5]
  compare 4, 5 → no swap
after: [1, 2, 3, 4, 5], pass count = 1

Two swaps move 4 rightward past both 2 and 3, and the array happens to end up fully sorted — though the algorithm can't know that yet.
Step 1 of 3

Steps to visualize

  1. Run a pass: walk the unsorted region comparing and swapping adjacent pairs, and note whether any swap happened.
  2. Count that pass toward the total, whether or not it swapped anything.
  3. If the pass made zero swaps, stop — the array is sorted.
  4. Otherwise, shrink the unsorted region by one and run another pass.
4. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Count passes, stopping the moment one finds no swaps
Statuspass 1

Pass 1 on [1, 4, 2, 3, 5]: 4 swaps past 2 and 3, two swaps happen, pass count = 1.

What happens in this step

pass 1, before: [1, 4, 2, 3, 5]
  compare 1, 4 → no swap
  compare 4, 2 → swap → [1, 2, 4, 3, 5]
  compare 4, 3 → swap → [1, 2, 3, 4, 5]
  compare 4, 5 → no swap
after: [1, 2, 3, 4, 5], pass count = 1

Two swaps move 4 rightward past both 2 and 3, and the array happens to end up fully sorted — though the algorithm can't know that yet.
Step 1 of 3
5. Solution

Solution

solution.tsTypeScript
function bubbleSortPassCount(nums) {
  const arr = nums.slice();
  const n = arr.length;
  let passes = 0;

  for (let pass = 0; pass < n - 1; pass++) {
    let swapped = false;

    for (let i = 0; i < n - 1 - pass; i++) {
      if (arr[i] > arr[i + 1]) {
        const temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        swapped = true;
      }
    }

    passes++;

    if (!swapped) break;
  }

  return passes;
}
Time
O(n^2)
Space
O(1)
6. Test cases

Test cases

InputExpectedCovers
nums = [1, 4, 2, 3, 5]2example from the docstring
nums = [1, 2, 3]1already sorted, one pass confirms it
nums = [2, 1]1boundary case, exactly two elements
nums = [4, 3, 2, 1]3worst case never gets an explicit zero-swap pass, runs n - 1 passes
nums = [5, 4, 3, 2, 1]4worst case at a larger size
nums = [1, 2, 4, 3]2only one pair out of order, still needs a confirming pass
nums = [7]0smallest valid input, no passes are possible