Home  >  Article  >  Java  >  Java program to sort array elements in descending order

Java program to sort array elements in descending order

WBOY
WBOYforward
2023-08-27 18:41:051171browse

Java program to sort array elements in descending order

An array is a collection of the same data type stored in some contiguous memory location. Array is a class in the java.until package that provides predefined sorting in a static manner and has no return value. This is the syntax of Arrays.sort() method mentioned below -

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

In the above syntax, we have

  • ar - Abbreviation of array name

  • from_index - We can use this as an optional parameter where sorting needs to be run.

  • to_index - Optional parameter providing the index of the element.

This 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 array elements present in a list and rearrange them in descending order using Java environment.

Algorithm for sorting array elements in descending order:-

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

  • Step 1 - Get Started

  • Step 2 - Set temperature =0.

  • Step 3 - Declare an array to hold the data.

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

  • Step 5 - Print "Elements of Original Array"

  • Step 6 - Declare a temporary variable to store the element while swapping.

  • Step 7 - Use two for loops to achieve the same purpose.

  • Step 8 - Repeat i

  • Step 9 - Use the first for loop to save the element and iterate through all elements.

  • Step 10 - if(arr[i]

    temporary= arr[i]

    arr[i]=arr[j]

    arr[j]=temp

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

  • Step 12 - Print new line.

  • Step 13 - Sort elements by comparison and exchange.

  • Step 14 - Iterate using for(i=0;i

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

  • Step 16 - Stop

Syntax for sorting 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));
   }
}

Method to follow

  • Method 1 - Java program to sort elements in descending order

  • Method 2 - Java program to sort elements in descending order using temp function

  • Method 3 - Java program to sort elements in descending order using common logic

Java program to sort elements in descending order

In this Java code, we try to build a logic for the process of sorting array elements in descending order.

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 elements in descending order using temp function

In this Java code, we can use the temp function to build a logic to sort the elements in the array in descending order.

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

Use common logic to sort elements in descending order

In this Java code, we have written a logic to sort the elements in an array in descending order by using some common 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

in conclusion

We have learned about the sorting of array elements in detail. Today we use various sorting methods to solve this problem through the grammar and algorithm mentioned above. Hopefully, through this article you have gained a broad understanding on how to sort array elements in descending order using Java environment.

The above is the detailed content of Java program to sort array elements in descending order. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete