Bubble Sort Java Examples: Master Sorting Algorithms Today

Robert Jul 13, 2026

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Java, being one of the most popular programming languages, is frequently used to implement this algorithm. Here, we'll delve into bubble sort examples in Java, their applications, and the step-by-step process of implementing them.

bubble sort doodle
bubble sort doodle

The choice to use Bubble Sort often depends on the specific requirements of the program. It has a readability advantage due to its simple implementation, making it a popular option for educational purposes. However, it's important to note that Bubble Sort has a worst-case and average time complexity of O(n^2), making it inefficient for large data sets.

Bubble Sort in Java with Example Code
Bubble Sort in Java with Example Code

Bubble Sort Algorithm in Java

The basic idea behind the Bubble Sort algorithm is to repeatedly swap the adjacent elements if they are in wrong order. This process is repeated until the list is sorted.

Java Bubble Sort Working and Example - TechVidvan
Java Bubble Sort Working and Example - TechVidvan

Here's a simple Java implementation of Bubble Sort:

```java public class BubbleSortExample { public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } public static void main(String args[]) { int arr[] = {64, 34, 25, 12, 22, 11, 90}; bubbleSort(arr); System.out.println("Sorted array:"); for(int i=0; i < arr.length; i++) System.out.print(arr[i] + " "); } } ```

Step-by-Step Explanation

BUBBLE SORT (Java, C++) |  Algorithms and Data Structures
BUBBLE SORT (Java, C++) | Algorithms and Data Structures

The outer loop iterates 'n-1' times, where 'n' is the number of elements in the array. The inner loop compares each element with its adjacent element. If the current element is greater than the next one, it swaps their positions. This continues until the array is sorted.

To better understand the sorting process, let's explore the array's state after each pass. Initially, the array is [64, 34, 25, 12, 22, 11, 90]. After the first pass, it becomes [34, 25, 12, 22, 11, 64, 90], then [25, 12, 22, 11, 34, 64, 90] after the second pass, and so on until it's sorted to [11, 12, 22, 25, 34, 64, 90].

Optimizing Bubble Sort

Bubble Sort Level 1
Bubble Sort Level 1

One way to optimize the Bubble Sort algorithm is by adding a flag to check if any swap occurs in the inner loop. If no swap occurs, it means the array is already sorted, and we can exit the loop. Here's the optimized Java code:

```java public class OptimizedBubbleSortExample { public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n-1; i++) { boolean swapped = false; for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; swapped = true; } } if (!swapped) break; } } public static void main(String args[]) { // Same as the previous example } } ```

This optimization significantly improves performance for almost-sorted or sorted arrays, reducing the time complexity to O(n) in the best case.

Applications of Bubble Sort

30 Days of Java – Day 26 | Sorting Algorithms Explained for Beginners
30 Days of Java – Day 26 | Sorting Algorithms Explained for Beginners

While Bubble Sort has a high time complexity, making it inefficient for large data sets, it still has applications in certain scenarios:

  • Small Data Sets: Bubble Sort is efficient for small data sets due to its simple implementation and linear time complexity in the best case.
  • Almost Sorted Data Sets: Due to the optimization of checking for swaps, Bubble Sort can be efficient for data sets that are nearly sorted.
  • Educational Purposes: Bubble Sort is often used to teach sorting algorithms due to its simplicity and readability.
  • Online Sorting: Bubble Sort can be used for online sorting, where the data is sorted while being streamed in real time.
Bubble Sort in Java
Bubble Sort in Java
Bubble sort
Bubble sort
Bubble Sort in Java – Learn How to Implement with Example!
Bubble Sort in Java – Learn How to Implement with Example!
Sorting an Array: Bubble Sort Algorithm Explained with Code
Sorting an Array: Bubble Sort Algorithm Explained with Code
Bubble Sort Algorithm Explained By Picking Teams At Recess
Bubble Sort Algorithm Explained By Picking Teams At Recess
How to implement Bucket Sort in Java? [Solved] - Example Tutorial
How to implement Bucket Sort in Java? [Solved] - Example Tutorial
Bubble Sort in Data Structure
Bubble Sort in Data Structure
Bubble Sort With JavaScript
Bubble Sort With JavaScript
Bubble Sort
Bubble Sort
Bubble sort in Java - Program to sort an Integer Array [Example]
Bubble sort in Java - Program to sort an Integer Array [Example]
Bubble Sort Java With Example
Bubble Sort Java With Example
Array of Strings in C++ with Examples - Hellgeeks
Array of Strings in C++ with Examples - Hellgeeks
Data structures and Java collections
Data structures and Java collections
Bubble Sort – Algorithm in Java, C++, Python with Example Code
Bubble Sort – Algorithm in Java, C++, Python with Example Code
Insertion Sort Algorithm in Java with Example
Insertion Sort Algorithm in Java with Example
Bubble Sort in C: Algorithm and C Code for Bubble Sort
Bubble Sort in C: Algorithm and C Code for Bubble Sort
Heap Sort - Java | Geekboots
Heap Sort - Java | Geekboots
Bubble Sorting algorithm with example program in C/C++/Java languages
Bubble Sorting algorithm with example program in C/C++/Java languages
Bubble Day STEM Stations for Science and End of Year for PreK-2
Bubble Day STEM Stations for Science and End of Year for PreK-2
a diagram showing the structure of an object in which it appears to be connected with other objects
a diagram showing the structure of an object in which it appears to be connected with other objects

Bubble Sort is not typically used in production environments for sorting large data sets due to its poor performance. Advanced sorting algorithms, such as Quick Sort, Merge Sort, or Heap Sort, are often preferred for such cases.

In the realm of algorithm design, understanding Bubble Sort is foundational, offering insight into how simple comparisons can lead to sorting. Its application in Java and other programming languages serves as a starting point for learning more complex sorting algorithms.