Bubble Sort In Java(Program)

import java.io.*;
class BubbleSort
{
public static void main(String args[])
{
try
{
int a[]=new int[100];
int i,n,p,j,temp;
DataInputStream dis=new DataInputStream(System.in);
System.out.println(“——-Bubble Sort——–“);
System.out.println(“Enter The Number Of Element:”);
n=Integer.parseInt(dis.readLine());
System.out.println(“Enter The Element:”);
for(i=1;i<=n;i++)
{
a[i]=Integer.parseInt(dis.readLine());
}
System.out.println(“Entered Elements Are:”);
for(i=1;i<=n;i++)
{
System.out.println(a[i]);
}

for(p=1;p<=n-1;p++) // Loop for Pass
{

for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j]; // Interchange Values
a[j]=a[j+1];
a[j+1]=temp;
}
}

}

System.out.println(“Elements After Sorting Are :”);
for(i=1;i<=n;i++)
{
System.out.println(a[i]);
}
}
catch(Exception e) {}
}
}

Leave a comment