Sorting In Descending Order Using Array In Java

The following java program arranges a collection of numbers in descending order using array.

import java.io.*;
class Descending
{
int n,c;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter"+(i+1)+"element");
a[i]=Integer.parseInt(br.readLine());
}
}
void pro()throws Exception
{
int j;
for(int i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
void show()
{
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
}
}
class DescendingMain
{
public static void main(String arg[])throws Exception
{
Descending obj=new Descending();
obj.get();
obj.pro();
obj.show();
}
}

Leave a Reply

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