Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Nested loops: on each pass compare adjacent pairs and swap if out of order. An early-exit flag stops once a pass makes no swaps. O(n²) worst case.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know bubble sort's mechanics, the shrinking bound, and the early-exit optimisation.
Where candidates slip
No swap flag, or a non-shrinking inner bound.
How to stand out
Note it's stable and in place but O(n²); name a real-world sort for production.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Adjacent-swap passes
1Hint 1Explore hintHide hint
Each pass moves the largest unsorted element where?
2Hint 2Explore hintHide hint
Can you shrink the inner loop as elements settle?
3Hint 3Explore hintHide hint
Add a flag to stop early when a pass makes no swaps.
A · Core idea
Bubble sort makes repeated passes, swapping adjacent out-of-order pairs. Each pass 'bubbles' the largest remaining element to its final position at the end.
Why the inner bound shrinks (`n-1-i`). After pass i, the last i elements are already the i largest, sorted and settled — so the next pass needn't touch them. Shrinking the inner bound avoids redundant comparisons.
Why the early-exit flag matters. If a full pass makes zero swaps, the array is already sorted and you can stop — turning the best case (already-sorted input) into O(n). Without the flag it's always O(n²).
Stability and use. Bubble sort is stable (equal elements keep order) and in place (O(1) space), but its O(n²) average makes it a teaching algorithm, not a production one. Know it to discuss sorting fundamentals.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [3, 1, 2]pass1: 3,1
3>1 swap
[1,3,2][1, 2, 3]C · Code with teaching comments
D · Why it works and complexityO(n²) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n²) time, O(1) space — up to n passes of up to n comparisons → O(n²) worst/average; the swap flag makes a sorted input O(n).
E · Edge cases and pitfalls5nums = [1, 2, 3] — Already sorted → one pass, no swaps, O(n).Explore edge casesHide edge cases
Edge cases handled
nums = [1, 2, 3]Already sorted → one pass, no swaps, O(n).nums = [1]Trivially sorted.nums = [3, 2, 1]Worst case, full O(n²).
Pitfalls
- Skipping the early-exit and always running O(n²).
- Inner bound not shrinking (n-1-i), redoing settled elements.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void bubbleSort(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { boolean swapped = false; for (int j = 0; j < nums.length - 1 - i; j++) { if (nums[j] > nums[j + 1]) {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Skipping the early-exit and always running O(n²).
- Inner bound not shrinking (n-1-i), redoing settled elements.
Revise
Carry the pattern into the interview
Pattern to remember
Adjacent-swap passes
Code pattern to memorize
for i: for j<n-1-i: if a[j]>a[j+1] swap; stop if no swapsRules to remember
- 1Swap out-of-order neighbours.
- 2Shrink inner bound by i.
- 3Early exit on a clean pass.
Use this pattern when
- Teaching/illustrating adjacent-swap sorting
Choose another pattern when
- Production sorting — use the library O(n log n)
Interview takeaway
Bubble sort: the largest bubbles to the end each pass.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Adjacent-swap passes. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups