Java Program to Sort the Array Elements in Descending Order


Array is a collection of same data types stored at some contiguous memory locations. The arrays are a class present in java.until package which provides pre-defined sorting with a static manner and no return value. Here is the syntax of the Arrays.sort() method mentioned below −

public static void sort(int[] ar, int from_index, int to_index)

Here in the above syntax we have

  • ar - short of the array name

  • from_index - a parameter we can use as optional, where the sorting takes a run.

  • to_index - an optional parameter donates the index of the element.

Here is an example

Input :Array = {2, 6, 23, 98, 24, 35, 78}
Output:[98, 78, 35, 24, 23, 6, 2]

Input :Array = {1, 2, 3, 4, 5}
Output:[5, 4, 3, 2, 1]

Today in this article we will learn, how to sort the array elements present in a list and rearrange them in a descending manner by using a Java environment.

Algorithm to sort the array elements in descending order:-

Here we have written the possible algorithm, by which we can sort the array elements in a descending order.

  • Step 1 − Start

  • Step 2 − SET temp =0.

  • Step 3 − Declare an array to put the data.

  • Step 4 − Initialize the array with arr[] ={5, 2, 8, 7, 1 }.

  • Step 5 − Print "Elements of Original Array"

  • Step 6 − Declare a temporary variable to store the elements while swapping.

  • Step 7 − Use two for loops for the same.

  • Step 8 − Repeat i<arr.length.

  • Step 9 − Use the first for loop to hold the elements and traverse through all the elements.

  • Step 10 − if(arr[i]<arr[j]) , then

    temp = arr[i]

    arr[i]=arr[j]

    arr[j]=temp

  • Step 11 − Use the second for loop to compare with the remaining elements

  • Step 12 − Print new line.

  • Step 13 − Sort the elements by comparing and swapping.

  • Step 14 − Iiteration with for(i=0;i<arr.length;i++).

  • Step 15 − Display the updated array as PRINT arr[i].

  • Step 16 − Stop

Syntax to sort the array elements in descending order

import java.util.*;
class Tutorialspoint071001 {
   public static void main(String[] args){
     // Initializing the array for the process
     Integer array[] = { 1, 2, 7, 16, 5,6 };

     // Sorting the array in a descending order
     Arrays.sort(array, Collections.reverseOrder());

     // Printing the elements after the process run
     System.out.println(Arrays.toString(array));
   }
}

Approaches to follow

  • Approach 1 − Java program to sort the elements in descending order

  • Approach 2 − Java program to sort the elements in descending order using temp function

  • Approach 3 − Java program to sort the elements in descending order using general logic

Java program to sort the elements in descending order

In this Java code, we have tried to build a logic of the sorting process of an array elements in a descending manner.

Example 1

import java.util.*;
public class tutorialspoint {
   public static void main(String[] args){

      // Initializing the array for the process run
      int array[] = { 1, 2, 3, 7, 5, 16 };

      // Sorting the array in ascending order if needed
      Arrays.sort(array);

      // Reversing the array for the main process
      reverse(array);

      // Printing the elements from the array
      System.out.println(Arrays.toString(array));
   }
   public static void reverse(int[] array){

      // Length of the array is mentioned here
      int n = array.length;

      // Run the process again. Swapping the first half elements with last half elements
      for (int i = 0; i < n / 2; i++) {

         // Storing the first half elements in a temp manner
         int temp = array[i];

         // Assigning the first half to the last half to get result
         array[i] = array[n - i - 1];

         // Assigning the last half to the first half to get the result
         array[n - i - 1] = temp;
      }
   }
}

Output

[16, 7, 5, 3, 2, 1]

Java program to sort the elements in descending order using temp function

In this Java code, we can build a logic to sort the elements present an array in a descending manner by using temp function.

Example 2

import java.util.Scanner;
public class Descendingtutorialspountrddarb{
   public static void main(String[] args) {
      int n, temp;
      Scanner s = new Scanner(System.in);
      System.out.print("Enter no. number of elements you want in the array---->:");
      n = s.nextInt();
      int a[] = new int[n];
      System.out.println("Enter all the elements here now to run the code further ----> :");
      for (int i = 0; i < n; i++) {
         a[i] = s.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 Output Is Here. Have A Look!:");
      for (int i = 0; i < n - 1; i++) {
         System.out.print(a[i] + ",");
      }
      System.out.print(a[n - 1]);
   }
}

Output

Enter no. number of elements you want in the array---->:7
Enter all the elements here now to run the code further ----> :
1
2
3
16
4
5
6
Descending Order Output Is Here. Have A Look!:16,6,5,4,3,2,1

Sorting the elements in descending order using general logic

In this Java code, we write a logic to sort the elements present an array in a descending manner by using some general functions.

Example 3

public class Tutorialspoint {
   public static void main(String[] args) {
      //Initialize array for the process 
      int [] arr = new int [] {16, 2022, 2001, 1997, 7}; 
      int temp = 0;

      //Displaying elements of an original array to go further
      System.out.println("Elements of original array are ---->: ");
      for (int i = 0; i < arr.length; i++) { 
         System.out.print(arr[i] + " ");
      }

      //Sort the array in descending order. Please go further
      for (int i = 0; i < arr.length; i++) { 
         for (int j = i+1; j < arr.length; j++) { 
            if(arr[i] < arr[j]) {
               temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
            } 
         } 
      }
      System.out.println();
      //Displaying elements of array after sorting process done.
      System.out.println("Elements of array sorted in descending order is here ---->: ");
      for (int i = 0; i < arr.length; i++) { 
         System.out.print(arr[i] + " ");
      }
   }
}

Output

Elements of original array are ---->: 
16 2022 2001 1997 7 
Elements of array sorted in descending order is here ---->: 
2022 2001 1997 16 7

Conclusion

We have learnt about the sorting of the array elements problem in details. Today we have used here various sorting methods to solve this problem by the above mentioned syntax and algorithm. Hope with this article you have gained the broad view about how to sort the array elements in a descending manner by using a Java environment.

Updated on: 12-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements