# Bubble Sort Program In Java With Explanation

Bubble sort Algorithm is the one of the most easiest sorting algorithms . As i said it is most easiest algorithm , but at the same time it is the most worst algorithm when it comes to performing search operation on a large amount of data . **Bubble sort** is very good when we have a short amount of data .

In Bubble sort Algorithm each element is compared with its corresponding element , there for if we have large amount of data then it will take too much time to processing each element comparing and **swapping** with large one .

Here we are going to learn about how bubble sort algorithm works procedure and** source code** and step by step way to understand **bubble sort** technique.

## #Code for Bubble sort in Java

public class Bubblesort { int value[] = {6,4,2,8,3,9,4,8,6,4}; int n=value.length; int temp; void bubblesort(){ // method to perform comparing and swapping for(int i=0;i<n-1;i++) { for(int j=0;j<n-1;j++) { if(value[j]>value[j+1]){ temp=value[j+1]; value[j+1]=value[j]; value[j]=temp; } } } System.out.println("output for Bubble sort"); } void output() //method to show output { for(int i=0;i<value.length-1;i++){ System.out.print(value[i]+" "); } } public static void main(String[] args) { Bubblesort bbl = new Bubblesort(); bbl.bubblesort(); bbl.output(); } }

## #Working Of Bubble Sort Program

To implement Bubble sort there are lots of ways , i have created one bubble sort class then it has integer type of array . The array which i have given name ( value ) in this program is having an unsorted list of integer numbers . we have two methods in this program one is bubblesort method and other one is output method . As we see that** bubble sort** method will be performing the processing ( comparing and swapping the elements ) and other method is showing the output for sorted values .

in this program when control enters in loop it will compare first value and second value , if corresponding value is greater then it swaps the values and continue the same steps unlit it process the last element in the array .

with this hope it will help you to understand and perform bubble sort algorithm in java , and this the most common question in the technical java interview so if you are preparing for java interview then this must be on your tips and other sorting algorithms as well .