Bubble Sort Program in Java – Explained with Examples

Basics of Python

5 Hrs. duration

9 Modules

1800+ Learners

Start Learning

In this article, we will learn about the bubble sort algorithm and how to implement it step by step in the Java programming language. Bubble Sort is one of the simplest algorithms to understand and implement. This article will explore the bubble sort, its mechanics, and its implementation in Java and provide some examples to solidify our understanding.

What is Bubble Sort?

The simplest sorting algorithm repeatedly steps through the list or array. This algorithm compares the adjacent elements and swaps them if they are in the wrong order. This process continues until no swaps are needed, indicating that the list has been sorted.

Bubble Sort Algorithm in Java

Let’s see how the bubble sorting algorithm works.

Run the two loops that are nested in one another.

The outer loop will run from i=0 to i< n-1, where n is the number of elements listed.

There is one more loop, which is run on the inner loop j=0 to j< n -i -1. It is because, after each iteration of the outer loop, one another element at the end (or at the start if the order is decreasing order) will be in its right place, so we can leave it as is. Then, we will check if the arr[j] > arr[j+1]. If it’s true, then we will swap places at these elements. If the condition is false, we will continue with the next iteration. This process will be repeated till the conditions of the loop are not satisfied.

This process will be repeated till the conditions of the loop are not satisfied.

For Decreasing, we have to change this thing in our bubble sort algorithm.

This inner loop will run from j = i to j< n-1.

We will compare the elements as arr[j] < arr[j+1]

The following program demonstrates the Bubble Sort in Java:

Program

class Main{
public static void bubbleSort(int []arr){
int n = arr.length ;
boolean swapped ;
for(int i =0 ;i< n-1;i++){
swapped = false ;
}
for(int j = 0;j< n-1 -i;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j] ;
arr[j] = arr[j+1] ;
arr[j+1] = temp ;
swapped = true ;
}
}
}
public static void display(int []arr){
for(int num: arr){
System.out.println(num+" ");
}
}
public static void main(String args[]){
int [] arr = {64,32,25,12,33,11,90} ;
System.out.println("Unsorted Array");
display(arr);
bubbleSort(arr);
System.out.println("Sorted Array");
display(arr);
}
}

The advantages of Bubble Sort in Java without number formatting:

Simplicity: The Bubble sort algorithm is easy to understand and implement, making it accessible for beginners.

No Extra Memory Usage: The bubble sort operates in place, requiring no additional memory beyond the original array, leading to a space complexity of O(1).

Stable Sorting: It maintains the relative order of equal elements, which can be important in applications where the order matters.

Education Value: Bubble sort is the foundational algorithm for learning basic sorting concepts and understanding algorithm design.

Disadvantages of Bubble Sort in Java

The bubble sort algorithm also has some disadvantages:

High Number of Comparisons: The algorithm performs many comparisons and swaps, which can lead to significant overhead and slower performance as the dataset grows.

Not Cache-Friendly: The bubble sort may not efficiently utilize CPU cache due to its repeated access patterns, which can further hinder performance.

Increased Development Time: Developers may need to spend additional time implementing and optimizing bubble sort for specific use cases, even though better alternatives exist.

It is a very simple sorting algorithm that introduces the concept of sorting and algorithm design. It is a comparison-based algorithm design. Being a comparison-based algorithm, comparison and swap of adjacent elements make it understandable, which is one of the many reasons it is often used for didactic purposes. It is a very valuable sorting algorithm for understanding the other algorithmic principles. It is essential to recognize its constraints and consider more efficient alternatives for practical use. Understanding bubble sort sets the groundwork for exploring more complex algorithms.

FAQs

Can bubble Sort be used to sort linked lists?

Yes, Bubble Sort can be implemented for linked lists, but it is generally inefficient. Other algorithms like Merge Sort are preferred for linked lists.

Is Bubble Sort adaptive?

An optimized version of Bubble Sort can be considered adaptive because it can finish early if the array becomes sorted before completing all passes.

Can Bubble Sort handle very large integers?

Yes, the bubble sort algorithm can handle large integers if they fit within the data type used in the implementations (eg, int, long).

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.