Bubble Sort Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you can implement it, add the early-exit optimization, and admit it's impractical at scale
Forgetting the early-termination flag, or claiming bubble sort is useful for large data
Add the swapped-flag optimization for O(n) best case and explain it's stable but rarely used
Interview-ready guide
Interview answer
Bubble sort works by making repeated passes over the array, and on each pass it compares adjacent pairs of elements and swaps them if they're in the wrong order.
The effect is that the largest unsorted element bubbles up to the end of the array in each pass, like a bubble rising to the surface. After the first pass the largest element is in its final spot, after the second pass the two largest are, and so on, which means each subsequent pass can stop a little earlier because the tail is already sorted.
The time complexity is O of n squared, because I do roughly n passes and each pass does up to n comparisons. It uses O of one extra space since it sorts in place, and it's stable, meaning equal elements keep their relative order. The one optimization actually worth adding is a swapped flag.
At the start of each pass I set it to false, and I set it true whenever I make a swap. If I finish a full pass without any swaps, the array must already be sorted, so I break out early. That makes the best case O of n on already-sorted input.
I'd be honest in an interview that bubble sort is a teaching algorithm. It's intuitive and easy to reason about, which is why it's taught first, but because it's quadratic you'd never use it for real data; you'd reach for Arrays dot sort or merge or quicksort. So I can implement it cleanly with the optimization, but I also know when not to use it.
Bubble Sort Java | Fresher Interview
Direct Answer
Bubble sort repeatedly walks the array swapping adjacent out-of-order pairs, so the largest unsorted element 'bubbles' to the end each pass. It's O(n^2) time, O(1) space, and stable. With an early-exit flag (stop when a pass makes no swaps), it becomes O(n) on already-sorted data. In practice you'd never use it for real sorting - it's a teaching algorithm.
Deep Dive & Architecture
Bubble sort repeatedly compares neighboring values and swaps them when they are in the wrong order. After one complete pass, the largest value in the unsorted part has moved to its final position at the right.
Worked example: [5, 1, 4, 2]
The first pass swaps 5 with 1, then 5 with 4, then 5 with 2, producing [1, 4, 2, 5]. The last position is now fixed. The next pass keeps 1, swaps 4 with 2, and produces [1, 2, 4, 5]. A further pass makes no swaps, so the early-exit flag proves the array is sorted.
After pass p, the last p elements never need to be compared again. Reducing the inner loop by the pass number avoids useless work. Swapping only when left > right, not when values are equal, makes the algorithm stable.
The average and worst cases are O(n²), with O(1) extra space. With an early-exit flag, an already sorted array takes O(n). Bubble sort is useful for learning swaps and loop invariants or for tiny teaching examples, but Java library sorting or insertion sort is normally a better practical choice.
Code Example
void bubbleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
boolean swapped = false;
for (int j = 0; j < a.length - 1 - i; j++) { // last i are settled
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
swapped = true;
}
}
if (!swapped) break; // no swaps => already sorted => O(n)
}
}
// {5,1,4,2}: pass1 -> {1,4,2,5}; pass2 -> {1,2,4,5}; pass3 no swap -> stop
Common Mistakes & Pitfalls
- No early-exit flag: without it, even a sorted array costs O(n^2).
- Inner bound
j < n-1always: usen-1-iso settled elements aren't rechecked. - Calling it useful at scale: it's O(n^2) - never for large data.
- Off-by-one in swaps: compare
a[j]witha[j+1], nota[j-1].
Check your understanding
Answer these without looking back at the article.
- What are the time complexities of common sorting algorithms?
- What sorting algorithm does Java use internally in Arrays.sort()?
- How does insertion sort work and when is it better than O(n log n) sorts?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.