Bubble Sort In Java: The Complete Guide With Examples - MasterInJava

Using the bubble sort algorithm in Java. It's very simple and easy but as a beginner, you may have different types of questions or doubt regarding bubble sort.

bubble sort in java

Don't worry and get started because in this article I will try to cover up all the doubts and questions regarding bubble sort in java and I will provide you a complete explanation. So, let's start with the explanation.

What is bubble sort in java?

As we all know that sorting is the process in which we arrange the given data in a particular order which may be either in ascending or descending order.

Similarly what bubble sort does is that it swaps the adjacent elements in a line to make them in a particular order.

bubble sort in java explanation

For example:  In a line, if there are five numbers 18, 5, 2, 6, and 9. Here the bubble sort program will first check the first two numbers 18 and 5.And it will compare it which number is large and which is small. In my case 18 is greater than 5 so it will swap the two number to make in a particular order(ascending order).

And it will continue till the 18 number is placed at the correct place(at the last). After placing 18 at the right place now it will go for the second round with the second number 5.

If there is no need of swapping for the number(placed at the correct place already) then it will go for the next number.

To determine in how many rounds the sorting process will complete, there is a mathematical equation. In my case, I have 5 elements so I can say that it will take 4 rounds.

Now, you will have a doubt how I came to know that it will complete in only 4 rounds, the answer is very simple what you have to do is minus"-" 1 for how many elements you have.

In my case, I have 5 elements so if I subtract it with 1 then the answer is 4. So, now I can say that it will take 4 rounds to provide me a full sorted list.

Advantages And Disadvantages

Advantages

  • Bubble sort programs are very easy to understand and they are really very easy to implement.
  • In the program, it doesn't consume any external type of memory.
  • While compiling it doesn't change the value of the elements.

Disadvantages

  • Utilizing the time of  O(n^2) it takes more time in large sets of data.
  • Apart from other sorting elements bubble sort takes more time to sort large sets of items.

Implementation

Applying bubble sort algorithm in your program is very simple and easy. All you need to know is to learn the basic java statements like for loop, if-else, etc.

After learning  this statement now you are ready to implement it in your program. Below I have divided the ascending order and the descending order programs with complete explanation and examples. So, firstly let's move to the ascending order program.

Ascending Order Bubble Sort Program

bubble sort in java using scanner

In this program, firstly you have to create an integer and give it randomly four names(I had given the names java, i, j and masterinjava) and close with an semi-colon.

After closing it, now you have to import a scanner by using the statement Scanner input = new Scanner(System.in). The reason for importing the scanner is that we don't have to do manually change the program for specific numbers to sort instead it will ask us for the numbers which we want to sort.

On the next line, you have to type the output statement System.out.println("Enter the numbers: "); . While running the program, this statement will ask how many numbers we want sort.

Then you have to input nextInt() method by using the first integer name(in my case it is "java")  java = input.nextInt().

After writing this statement, now for the nextInt() method you have to create a array list using the first integer name int array[] = new int[java].

And again you have to create an output statement System.out.println("Enter " + java + "integers"). Here this statement will ask you the integers or numbers which you want to sort.

Then on the next line you have to create a for loop statement with the second integer name( in my case it is "i")  for (i = 0;  i < java; i++).

After that, again you have use an array with the second integer name which inputs the nextInt() method array[i] = input.nextInt().

And now you have to input two more for loop statements using the first, second, and third integer name. Therefore the first statement is for(i = 0; i < (java -1 ); i++ .

Here in the first statement, we said that lets i value is equals to zero and i value is less then java value subtracted by 1 and i++.

So, the second statement is for(j = 0; j <  java - i - 1; j++. Here in this statement I have said that let j value be equal to 0 and j value is less than the value of java value substracted by i value and 1 and given j++.

Then below these two for loop statement, we have to give and if-else statement. So, the statement is if(array[j] > array[j+1]). In this if-else statement, I have said that if array[j] value is greater then the value of array[j+1] then the below three conditions will be applied.

Therefore the first condition is masterinjava = array[j]. Here I stated that if array[j] is greater than array[j+1] then the value of our fourth integer which is "masterinjava" will become equal to the value of array[j].

Next line you have to write the second condition which is array[j] = array[j+1]. Here if the if-else statement is true then the value of array[j] will be the same of the array[j+1].

Now, finally, you have to write the last condition of the if-else statement which is array[j+1] = masterinjava. In this condition, the value of array[j+1] will become equivalent to the value of masterinjava if the if-else statement is true.

After writing the above conditions now you have to write a output statement System.out.println("Sorted List : "). This statement will display you the whole sorted of the integers which you have inputted.

Then you have to create the last for loop statement for ( i = 0; i < java; i++). And finally create a output statement System.out.println(array[i]). And click on the run button.

Output

bubble sort in java


Descending Order Bubble Sort Program

The whole program for descending order is similar to the ascending order program.

bubble sort in java

But in this program, you to change a very minor thing, which is the greater than sign into less than sign as shown at the image above marked with red. After replacing the sign finally click on the run button.

Output

bubble sort in java with explanation

Conclusion

That's all for this article and now you are fully ready to write your own program using the bubble sort algorithm. I tried to cover every single doubts and questions regarding bubble sort in this article. But if you have any doubt in your mind regarding bubble sort just drop a comment below I will definitely answer your question.

And don't forget to share with your friends and family members to help them in learning Java.

Post a Comment

0 Comments