How to implement Bubble Sort in a Java Program?

public class BubbleSort
{
public static void main(String args[])
{
    int a[]={5,4,3,2,1,0,-1,2};
    for(int i=0;i<=a.length-2;i++)
    {
        for(int j=0;j<=a.length-2-i;j++) { if(a[j]>a[j+1])
            {
                int t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    for(int i=0;i<=a.length-1;i++)
        System.out.println(a[i]);
}
}

Output

-1
0
1
2
2
3
4
5

 

 

 

 

Leave a Reply