Perform sorting in array in java

Write a java program to perform sorting in array.

package array;

import java.util.*;
public class ascdecarray 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size,i,j,t;
        System.out.println("enter size");
        size=s.nextInt();
        int a[]=new int[size];
        for(i=0;i<size;i++)
        {
            a[i]=s.nextInt();
        }
        for(i=0;i<size;i++)
        {
            for(j=0;j<size;j++)
            {
                if(a[i]<a[j])
                {
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
            }
        }
        for(i=0;i<size;i++)
        {
            System.out.println("Ascending order elements are::"+a[i]);
        }
        for(i=0;i<size;i++)
        {
            for(j=0;j<size;j++)
            {
                if(a[i]>a[j])
                {
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
            }
        }
        for(i=0;i<size;i++)
        {
            System.out.println("Descending order elements are::"+a[i]);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *