Sorting is a process of arranging items in a sequential order, based on some criterion. There are several algorithms that are used for sorting and one amongst them is Bubble sort. Bubble sort algorithm is known as the simplest sorting algorithm. So this article on Bubble Sort in Java will help you understand this concept in detail.
I’ll be discussing the below topics:
What is bubble Sort?
In Bubble sort algorithm, an array is traversed. It starts from the first element and traverses until the last element. The current element is compared with the next element and if the current element is greater than the next element, it is swapped. This process goes on until the whole array is sorted. Let me explain the algorithm to you with an example.
First Pass:
( 2 5 1 7 6 ) –> ( 2 5 1 7 6)
Here, the algorithm compares the first two elements. Since 5 > 2, it does not swap but it continues further.
( 2 5 1 7 6 ) –> ( 2 1 5 7 6 )
In this case, swapping will take place, since 5 > 1.
(2 1 5 7 6) –> (2 1 5 7 6)
It won’t swap, since 5 < 7.
( 2 1 5 7 6 ) –> (2 1 5 6 7)
As 7 > 6, it swaps.
Second Pass:
( 2 1 5 6 7) –> ( 1 2 5 6 7 )
Since 1 < 2, hence it swaps.
( 1 2 5 6 7 ) –> ( 1 2 5 6 7 )
Already sorted
( 1 2 5 6 7 ) –> ( 1 2 5 6 7 )
Already sorted
( 1 2 5 6 7) –> ( 1 2 5 6 7 )
Now, we know that the array is already sorted, but our algorithm does not know if the process is completed. The algorithm will traverse the full array again and check.
Third Pass:
(1 2 5 6 7) -> (1 2 5 6 7)
(1 2 5 6 7) -> (1 2 5 6 7)
(1 2 5 6 7) -> (1 2 5 6 7)
(1 2 5 6 7) -> (1 2 5 6 7)
After this pass, the algorithm understands that the goal is accomplished.
Now that you are well acquainted with the working of the bubble sort algorithm, let’s hop onto our next segment. Here, I will show you the implementation of the algorithm through a simple Java program. Here it goes!
Bubble Sort in Java: Implementation through Java program
public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } } public static void main(String[] args) { int arr[] ={5,76,65,23,42,15}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } }
Output:
Array Before Bubble Sort
5,76,65,23,42,15
Array After Bubble Sort
5,15,23,42,65,76
Well, I hope the ambiguities about bubble sort are clear by now.
With this, we have reached the end of the blog titled “bubble sort in Java”. I hope the content explained added value to your Java knowledge.
If you found this article on “Bubble sort in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey, besides this, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of “Bubble sort in Java” and our team will be glad to answer.