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
nums = [1, 4, 2, 3, 5]2Explanation 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.
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.
Count passes, stopping the moment one finds no swaps
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.
Steps to visualize
- Run a pass: walk the unsorted region comparing and swapping adjacent pairs, and note whether any swap happened.
- Count that pass toward the total, whether or not it swapped anything.
- If the pass made zero swaps, stop — the array is sorted.
- Otherwise, shrink the unsorted region by one and run another pass.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
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.
Solution
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)
Test cases
| Input | Expected | Covers |
|---|---|---|
nums = [1, 4, 2, 3, 5] | 2 | example from the docstring |
nums = [1, 2, 3] | 1 | already sorted, one pass confirms it |
nums = [2, 1] | 1 | boundary case, exactly two elements |
nums = [4, 3, 2, 1] | 3 | worst case never gets an explicit zero-swap pass, runs n - 1 passes |
nums = [5, 4, 3, 2, 1] | 4 | worst case at a larger size |
nums = [1, 2, 4, 3] | 2 | only one pair out of order, still needs a confirming pass |
nums = [7] | 0 | smallest valid input, no passes are possible |