/* Bubble Sort program in java */
import java.lang.*;
import java.io.*;
class BubbleSort
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i,j,temp;
System.out.println("----Welcome to the Bubble Sort Prog----");
System.out.println("Hi mani how many elements you have");
n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("Enter " +n+ " Elements");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("Before Sorting");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
System.out.println("\nAfter Sorting");
for(i=0;i<n;i++)
System.out.print("\t" +a[i]);
}
}
Output:
----Welcome to the Bubble Sort Prog----
Hi mani how many elements you have
5
Enter 5 Elements
20
30
20
15
20
Before Sorting
20 30 20 15 20
After Sorting
15 20 20 20 30
0 comments:
Post a Comment