In this program, we will create an array of integers then we will sort array elements in descending order using selection sort.
In the Selection Sort technique, we find the largest element and swap the largest elements with the corresponding element to sort into descending order.
Program/Source Code:
The source code to sort an array in descending order using selection sort is given below. The given program is compiled and executed successfully.
// Java program to sort an array in descending order
// using selection sort
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
int i = 0;
int j = 0;
int t = 0;
int max = 0;
int arr[] = {14, 49, 79, 87, 78};
//Sort created array.
while (i < 5) {
max = i;
j = i + 1;
while (j < 5) {
if (arr[j] > arr[max])
max = j;
j = j + 1;
}
t = arr[i];
arr[i] = arr[max];
arr[max] = t;
i = i + 1;
}
System.out.println("Sorted Array in descending order: ");
i = 0;
while (i < 5) {
System.out.print(arr[i] + " ");
i = i + 1;
}
}
}
In this program, we will create an array of integers then we will sort array elements in descending order using selection sort.
In the Selection Sort technique, we find the largest element and swap the largest elements with the corresponding element to sort into descending order.
Program/Source Code:
The source code to sort an array in descending order using selection sort is given below. The given program is compiled and executed successfully.
Output: