Write a java Program to Sort In Ascending and Descending
import java.util.*;
class SortOrder
{
public static void main(String args[])
{
Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter the array size");
int size=sc.nextInt();
int[] arr= new int[size];
System.out.println("Enter Array Elements");
for(int i=0;i<size;i++)
{
System.out.print("Element number" +(i+1)+" :");
arr[i]=sc.nextInt();
}
System.out.println("Before Swapping");
for(int i=0;i<size;i++)
System.out.println(" "+arr[i]);
for(int i=0;i<size;i++)
{
int temp;
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println();
System.out.println("After sorting in ascending order:");
for(int i=0;i<size;i++)
{
System.out.println(""+arr[i]);
}
System.out.println();
System.out.println("After sorting in descending order:");
for(int i=size-1;i>=0;i--)
{
System.out.println(""+arr[i]);
}
}
}
loading...
Leave a Comment