Sunday, January 1, 2012

perform bubble sort in Java

Example



import java.io.*;
public class A15
{
public static void main(String args[])
throws IOException
{
int a[]=new int[5];
int i;
int j;
int temp;
BufferedReader br;
br=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<5;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Bubble sort is");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}

No comments:

Post a Comment