Bubble Sort using Command Line Arguments in JAVA
class BubblePrg
{
public static void main(String []a)
{
int ar[],m,i,j,temp,k;
m=10;
ar=new int[m];
i=0;
for(String x:a)
{
ar[i++]=Integer.parseInt(x);
}
System.out.println("Before Sorting Array is : ");
for(i=0;i<m;i++)
System.out.print(ar[i]+",");
for(i=0;i<m;i++)
{
for(j=0;j<m-i-1;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
System.out.println("\n \nAfter Pass : "+i);
for(k=0;k<m;k++)
System.out.print(ar[k]+",");
}
System.out.println("\n \nAfter Sorting Array is : ");
for(i=0;i<m;i++)
System.out.print(ar[i]+",");
}
}
I/O : after compiling the java source code run it as follows :
$> java BubblePrg 12 21 23 34 4 22 65 56 76
Comments
Post a Comment