Introduction
Bubble Sort is one of the simplest sorting algorithms in Java. It repeatedly compares two adjacent elements and swaps them if they are in the wrong order. This process continues until the array is completely sorted.
The algorithm gets its name because the largest (or smallest) element “bubbles” to its correct position during each pass through the array.
For example:
Original Array:
5 2 8 1 9
Sorted Array:
1 2 5 8 9
Bubble Sort is easy to understand and implement, making it one of the first sorting algorithms taught to beginners. Although it is not the fastest sorting algorithm for large datasets, it is excellent for learning the fundamentals of sorting.
In this tutorial, you’ll learn how Bubble Sort works, how to implement it in Java, and understand its time complexity and real-world applications.
What is Bubble Sort?
Bubble Sort is a comparison-based sorting algorithm that repeatedly compares adjacent elements and swaps them whenever they are in the wrong order.
For ascending order:
- If the left element is greater than the right element, swap them.
- Continue until no more swaps are required.
Example:
Array:
5 3 8 2
Pass 1:
3 5 2 8
Pass 2:
3 2 5 8
Pass 3:
2 3 5 8
After several passes, the array becomes sorted.
Syntax
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Java Bubble Sort Program
public class BubbleSort {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 9};
int n = numbers.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("Sorted Array:");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
Output
1 2 5 8 9
How Bubble Sort Works?
Consider the following array:
5 2 8 1 9
Pass 1
5 2 → Swap
2 5 8 1 9
8 1 → Swap
2 5 1 8 9
Largest element (9) reaches its correct position.
Pass 2
2 5
5 1 → Swap
2 1 5 8 9
Pass 3
2 1 → Swap
1 2 5 8 9
The array is now sorted.
Explanation of the Program
Step 1: Store the Array
int[] numbers = {5, 2, 8, 1, 9};
Step 2: Outer Loop
for(int i = 0; i < n - 1; i++)
Controls the number of passes.
Step 3: Inner Loop
for(int j = 0; j < n - i - 1; j++)
Compares adjacent elements.
Step 4: Swap Elements
if(numbers[j] > numbers[j + 1])
Swap the two elements if they are not in the correct order.
Java Bubble Sort Using User Input
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("Sorted Array:");
for (int num : numbers) {
System.out.print(num + " ");
}
scanner.close();
}
}
Output
Enter array elements:
8
3
6
1
5
Sorted Array:
Optimized Bubble Sort Program
The optimized version stops early if the array is already sorted.
public class BubbleSort {
public static void main(String[] args) {
int[] numbers = {2, 4, 6, 8, 10};
int n = numbers.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
swapped = true;
}
}
if (!swapped)
break;
}
for (int num : numbers)
System.out.print(num + " ");
}
}
Algorithm
- Read the array.
- Repeat the following until the array is sorted:
- Compare adjacent elements.
- Swap them if they are in the wrong order.
- Continue until no swaps occur.
- Print the sorted array.
Flowchart
Start
│
Read Array
│
Compare Adjacent Elements
│
Swap if Required
│
Repeat for All Passes
│
Display Sorted Array
│
End
Time Complexity
| Case | Time Complexity |
|---|---|
| Best Case (Optimized) | O(n) |
| Average Case | O(n²) |
| Worst Case | O(n²) |
Space Complexity
O(1)
Bubble Sort sorts the array in place, requiring only a temporary variable for swapping.
Advantages of Bubble Sort
- Easy to understand
- Simple implementation
- In-place sorting (no extra array required)
- Stable sorting algorithm
- Suitable for small datasets
Disadvantages of Bubble Sort
- Slow for large datasets
- High time complexity (O(n²))
- Performs many unnecessary comparisons
- Less efficient than Quick Sort, Merge Sort, or Heap Sort
Common Mistakes
1. Incorrect Inner Loop Condition
Incorrect:
for(int j = 0; j < n; j++)
Correct:
for(int j = 0; j < n - i - 1; j++)
2. Forgetting to Swap
Incorrect:
if(numbers[j] > numbers[j + 1]) {
}
Always perform the swap inside the condition.
3. Accessing an Invalid Index
Incorrect:
numbers[j + 1]
when j reaches the last index, causing an ArrayIndexOutOfBoundsException.
Use the correct loop boundary:
j < n - i - 1
4. Forgetting the Optimization
Without checking the swapped flag, the algorithm continues even if the array is already sorted.
5. Using Bubble Sort for Large Data
Bubble Sort is inefficient for large arrays. Prefer Quick Sort or Merge Sort for better performance.
Real-Life Applications
Bubble Sort is commonly used for:
- Learning sorting algorithms
- Educational purposes
- Small datasets
- Interview preparation
- Simple embedded systems
- Demonstrating algorithm concepts
Summary
The Java Bubble Sort program is one of the most popular sorting examples for beginners. It demonstrates fundamental concepts such as arrays, nested loops, comparisons, and swapping elements.
Although Bubble Sort is easy to learn and implement, it is not suitable for large datasets due to its O(n²) average and worst-case time complexity. The optimized version improves performance for already sorted arrays by stopping early when no swaps occur.