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) {}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s