chevron_left
57 points
7 3 2

Here, we will write the Java programs to sort the array in descending order. Consider a list of an array element in an unsorted format, we will sort them in descending order.

Algorithm:

  1. Take the input size of an element.
  2. Use a loop to Input array elements.
  3. Now, take two loops.
  4. In an outside loop, start the iteration from the 0th position to the end of the array an element.
  5. In another loop, start the iteration from the previous loop variable.
  6. Compare both the elements and swap them.
  7. Now, at the end display elements.

Java programs to sort array in descending order:

package com.abc;

import java.util.Scanner;

/*
Java programs to sort array in descending order.
 */
public class SortArrayInDescending {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Size of array");
        int size = sc.nextInt();
        int arr[] = new int[size];
        System.out.println("Enter array");
        for(int i=0;i<arr.length;i++){
            arr[i] = sc.nextInt();
        }
        for (int i = 0;i<arr.length;i++){
            for (int j=i+1;j<arr.length;j++){
                if (arr[i]<arr[j]){
                    int temp = arr[i];
                    arr[i]= arr[j];
                    arr[j]=temp;
                }
            }
        }
        System.out.println("Descending order: ");
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]+" ");
        }

    }
}

Output

Enter Size of array
5
Enter array
0 15 42 63 98 
Descending order: 
98 
63 
42 
15 
0 

Process finished with exit code 0

Thank you for reading this article!!

2 Comments

I've been looking for something like this for a long time. Thank you for making this available.
This article is really helpful. Thanks.

More Posts

Sort array without using sort function in Java Hafsa_Rao - Oct 1
Program to find maximum and minimum number in an array in java Being human - May 30
How to sort array of objects in javascript IvanLomakin - Aug 4
Sort String array java without using sort method kesav.eee - May 15
how to sort a string in java Hasnain_khan - Oct 13, 2020
Java program to find maximum and minimum number without using array Sanjana Sagar - May 20
Sort the array according to frequency of elements kesav.eee - Aug 12
Javascript sort array of objects by number IvanLomakin - Aug 4
Javascript sort array of objects by property amna - May 20
How to sort an array in java without using sort method ? Naymul01 - 21 hours