82. JAVA Program to Sort the elements in Descending Order.
How does this program work?
- This program is used to sort the elements in descending oder using java.
- In this program, an array of elements can be sorted without using any sorting function in java.
- Each element is compared with the next element, If the element is less than the other in comparison then the elements are swapped.
- After doing this for each element of the given array, then we get the elements in descending order.
Here is the code
//To Arrange the elements in Descending Order
import java.util.Scanner;public class order
{
public static void main(String[] args )
{
int n, temp;
System.out.print("Enter Elements:\n");
Scanner skill = new Scanner( System.in);
n = skill.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = skill.nextInt();
}
for(int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for(int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + "," );
}
System.out.print(a[n - 1] );
}
}