// program for sorrting of array
import java.io.*;
class simple_array_sort_ass_desn
{
public static void main(String args[])
{
int a[]=new int[10];
int n,i,j,temp;
try
{
DataInputStream cin=new DataInputStream(System.in);
System.out.println("Enter the array size");
n=Integer.parseInt(cin.readLine());
System.out.println("enter the "+n+" elements for array");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(cin.readLine());
}
System.out.println("The given array is");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("elements in assending order");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("elements in desending order");
for(i=n-1;i>=0;i--)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
System.out.println("Wrong input");
}
}
}
No comments:
Post a Comment