Wednesday, September 23, 2015

write a program for BubbleSort

import java.io.*;
class BubbleSort
{
public static void main(String arg[])
{
int n,i,j,temp;
int a[] = new int[10];
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the numbers of values");
n = Integer.parseInt(r.readLine());
System.out.println("Enter the unsorted list");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(r.readLine());
}
System.out.println("Sorted List");
for(i=n-2;i>=0;i--)
{
for(j=0 ;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1
]=temp;
}
}
}
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
System.out.println("Error is occur at"+e);
}
}
}
S

No comments:

Post a Comment