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.
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.

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.

##
__Ad____va____ntages 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__

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__

##
__Descending Order Bubble Sort Program__

The whole program for descending order is similar to the ascending order program.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__

###
__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.

## 0 Comments

masterinjavaofficial@gmail.com