In the previous article, we have seen Java Program to Sort the Elements of an Array in Ascending Order
In this article we are going to see how we can sort an Array in descending order in Java.
Java Program to Sort the Elements of an Array in Descending Order
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to Sort the Elements of an Array in Descending Order.
- Sort an array in Decreasing order using an user-defined method
- Sort an Array In Decreasing Order Using Arrays.sort function
Method-1: Java Program to Sort an array in Decreasing order using an user-defined method
Approach:
- Ask the user to input the size and store it.
- Create an array of the specified size.
- Print the array elements
- Sort the elements using the user-defined function
sortArr( )
. - Print the sorted array elements
Program:
import java.util.Scanner; import java.util.Arrays; public class array { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Asking the user for array size System.out.println("Enter the array size : "); int size = scan.nextInt(); // Creating the array int arr[] = new int[size]; System.out.println("Enter the array elements : "); // Takes the elements as input from the user for(int i = 0;i<size;i++) { arr[i] = scan.nextInt(); } // Prints the array before and after sorting System.out.println("The array elements are"+Arrays.toString(arr)); sortArr(arr,size); System.out.println("The array elements after sorting in descending order are : "+Arrays.toString(arr)); } // Method to sort the array static void sortArr(int arr[],int size) { int temp; // Uses Bubble Sort to sort the array for (int i = 0; i < size; i++) { // Compares and replaces the element with all the remaining elements in the array for (int j = i+1; j < size; j++) { if(arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } }
Output: Enter the array size : 6 Enter the array elements : 6 1 5 3 4 2 The array elements are[6, 1, 5, 3, 4, 2] The array elements after sorting in descending order are : [6, 5, 4, 3, 2, 1]
Method-2: Java Program to Sort an Array In Decreasing Order Using Arrays.sort function
Approach:
- Ask the user to input the size and store it.
- Create an array of the specified size.
- Print the array elements
- Sort the elements using the
Arrays.sort
function. - Print the sorted array elements
Program:
import java.util.Scanner; import java.util.Collections; import java.util.Arrays; public class array { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Asking the user for array size System.out.println("Enter the array size : "); int size = scan.nextInt(); // Creating the array Integer arr[] = new Integer[size]; System.out.println("Enter the array elements : "); // Takes the elements as input from the user for(int i = 0;i<size;i++) { arr[i] = scan.nextInt(); } // Prints the array before and after sorting System.out.println("The array elements are : "+Arrays.toString(arr)); Arrays.sort(arr, Collections.reverseOrder()); System.out.println("The array elements after sorting in ascending order are : "+Arrays.toString(arr)); } }
Output: Enter the array size : 6 Enter the array elements : 6 1 5 3 4 2 The array elements are : [6, 1, 5, 3, 4, 2] The array elements after sorting in ascending order are : [6, 5, 4, 3, 2, 1]
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: