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

In this program, the goal is to sort the given array in descending order, arranging the elements from largest to smallest. This is achieved by utilizing two loops. The outer loop selects an element, while the inner loop compares the selected element with the remaining elements in the array.

The objective is to sort the given array in descending order. The sorting process ensures that the largest element appears on the extreme left, while the smallest element appears on the extreme right.

Algorithm for a Java code to sort the elements of an array in descending order

STEP 1: START

STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }.

STEP 3: SET temp =0

STEP 4: PRINT “Elements of Original Array”

STEP 5: REPEAT STEP 6 UNTIL i<arr.length
//for(i=0; i<arr.length; i++)

STEP 6: PRINT arr[i]

STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<arr.length
//for(i=0; i<arr.length; i++ )

STEP 8: REPEAT STEP 9 UNTIL j<arr.length
//for(j=i+1;j<arr.length;j++)

STEP 9: if(arr[i]<arr[j]) then
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp

STEP 10: PRINT new line

STEP 11: PRINT “Elements of the array sorted in descending order”

STEP 12: REPEAT STEP 13 UNTIL i<arr.length
//for(i=0;i<arr.length;i++)

STEP 13: PRINT arr[i]

STEP 14: END

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

public class SortDsc {    
    public static void main(String[] args) {        
        //Initialize array     
        int [] arr = new int [] {5, 2, 8, 7, 1};     
        int temp = 0;    
            
        //Displaying elements of original array    
        System.out.println("Elements of original array: ");    
        for (int i = 0; i < arr.length; i++) {     
            System.out.print(arr[i] + " ");    
        }    
            
        //Sort the array in descending order    
        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    
        System.out.println("Elements of array sorted in descending order: ");    
        for (int i = 0; i < arr.length; i++) {     
            System.out.print(arr[i] + " ");    
        }    
    }    
}

Output:

Elements of original array:
5 2 8 7 1
Elements of array sorted in descending order:
8 7 5 2 1

In conclusion, the provided Java code effectively sorts the elements of an array in descending order. By utilizing two loops, the elements are compared and swapped iteratively, resulting in the desired arrangement from largest to smallest. Follow tutorials.freshersnow.com to learn more.