Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

Java Program to Sort the Elements in descending order.

Get array size n and n elements of array, then sort the elements of array in descending order.

Sample Input 1:

5 5 7 9 3 1

Sample Output 1:

9 7 5 3 1

Program or Solution

					    
							
import java.util.*;
class Descen
{
  public static void main(String args[])
  {
     int size,i,j,num,temp;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter Size Of Array:");
     size=sc.nextInt();
     int a[]=new int[100];
     System.out.println("Enter The Array Elements:\n");
     for(i=0;i<size;i++)
        {
            a[i]=sc.nextInt();

	}

    for(i=0;i<size;i++)
	{
		for(j=i+1;j<size;j++)
		{
			if(a[i]<a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}
	System.out.println("After Sorting It Is In Descending Order:");
	for(i=0;i<size;i++)
	{
	     System.out.println(""+a[i]);
	}
  }
}

					    
					

Program Explanation

With the help of the Selection sort the program is done