Java Program to sort the elements of an array in descending order

In this tutorial you will learn about the Java Program to sort the elements of an array in descending order and its application with practical example.

In this tutorial, we will learn to create a Java Program to sort the elements of an array in descending order using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Sorting in a an array[]

Sorting elements means to arrange elements within the array in a certain specific order. either Ascending(increasing) or descending(decreasing) order.

Java Program to sort the elements of an array in ascending order

In this program we will take size of an array  and sort the elements of an array using nested for loop. We would first declared and initialized the required variables. Next, we will sort the elements in an array. Lets have a look at the code.

Output

Sorted elements in an array.

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • number =  it will hold length of an array.
  • i =  for iteration.
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

Elements will be sorted in Descending order .As shown in the image below.

Algorithm to sort array in descending order.

  1. Start the algo
  2. Declare an array[].
  3. Taking size and element of an array from user.
  4. Declared a temp variable to store the elements for swapping.

  1. Here first for loop will hold the elements and traverse through all the elements.

  1. Second for loop to compare with the remaining elements.
  2. Sort the elements by comparing.
  3. Display the updated array.
  4. Stop

The above program shows  sort an array in descending order using for loops .

In this tutorial we have learn about the Java Program to sort the elements of an array in descending order and its application with practical example. I hope you will like this tutorial.