Bubble Sort Algorithm: Loops, Swaps and Early Exit

Bubble Sort must use an outer loop for passes and an inner loop for adjacent comparisons.
Outer Loop
The outer loop controls the passes. After each pass, the largest remaining unsorted value moves to the end of the current unsorted section.
for (int pass = 0; pass < values.length - 1; pass++) {
    // one Bubble Sort pass
}

Inner Loop

The inner loop compares adjacent values. The comparison range becomes shorter after every pass because the values at the end are already sorted.
for (int i = 0; i &lt; values.length - 1 - pass; i++) {
    // compare values[i] and values[i + 1]
}

Part 5: Adjacent Comparison

Count one comparison every time the program evaluates:
values[i] > values[i + 1]

If the left value is larger than the right value, exchange the two values and count one swap.

Part 6: Swap Method

private static void swap(
    int[] values,
    int first,
    int second
)

The swap method must exchange the values at two indexes. Every call to this method represents one actual swap.

Part 7: Early-Exit Optimization

At the beginning of every pass, create a Boolean variable such as swapped and set it to false.
Whenever a swap occurs, set swapped to true. At the end of the pass, if swapped is still false, the array is already sorted and the outer loop must stop.

Important Formulas

For an array with \(N\) elements:

  • Maximum passes: \(N-1\)
  • Maximum comparisons: \(\frac{N(N-1)}{2}\)
  • Worst-case swaps: \(\frac{N(N-1)}{2}\)

Common MCQ Questions

QuestionCorrect Concept
What type of sorting?Comparison-based
What gets swapped?Adjacent elements
Where does the largest unsorted element go?Last position of the unsorted section
Is it in-place?Yes
Is it stable?Yes
Main disadvantage?High time complexity
Best case for optimized Bubble Sort?Already sorted in ascending order
Worst case for ascending sort?Descending order
What does a pass mean?One complete trip through the unsorted section

Type 2: Inner-Loop Range and Early-Exit Logic

Professors love testing whether you understand why the loops are written the way they are in your starter code.

Question: “Why does the inner loop condition use values.length - 1 - pass instead of just values.length - 1?”
Answer: Because after each pass, the largest elements have already “bubbled up” to the end and are already sorted. Shrinking the range saves unnecessary comparisons.

Question: “What does the swapped boolean variable do, and when is it checked?”
Answer: It is reset to false at the start of every pass. If a swap occurs, it becomes true. If a pass finishes and swapped is still false, the array is fully sorted, triggering an early exit.

Question: “Does a pass that triggers an early exit still count as a completed pass?”
Answer: Yes. As noted in the assignment notes, the pass that confirms the array is sorted still counts as a completed pass.

Type 3: Comparison and Swap Math

Expect questions about how many times the code executes behind the scenes.

Question: “For an array of \(N\) elements, how many comparisons does Pass 1 always perform?”
Answer: \(N – 1\) comparisons. For 6 elements, it performs 5 comparisons.

Question: “If an array of 6 elements takes 3 passes to sort, why did the total comparison count end up as 12 instead of 15?”
Answer: Because of the shrinking inner loop. Pass 1 performed 5 comparisons, Pass 2 performed 4, and Pass 3 performed 3 (\(5 + 4 + 3 = 12\)).

Type 4: Code Modification and “What Happens If”

These questions test whether you truly understand the Java code.

Question: “What would happen if values[i] > values[i + 1] were changed to values[i] < values[i + 1]?”
Answer: The array would be sorted in descending order (largest to smallest) instead of ascending order.

Question: “What would happen if the early-exit condition were removed?”
Answer: The program would still sort the array correctly, but it would waste time running unnecessary passes after the array is already completely sorted.