-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDescendingArray.java
46 lines (40 loc) · 1.38 KB
/
DescendingArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Sorting Array in an Descending Order
import java.util.Scanner;
import java.util.Arrays;
class DescendingArray {
int[] arr;
void inputArray(int n) {
Scanner input = new Scanner(System.in);
arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
System.out.println("Input array: " + Arrays.toString(arr));
}
void swap(int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void arrange() {
int l = arr.length;
for (int i = 0; i < l - 1; i++) {
int largest = i;
for (int j = i + 1; j < l; j++) {
if (arr[j] > arr[largest]) { // Change the comparison to >
largest = j;
}
}
swap(i, largest); // Swap with the largest element
}
System.out.println("The greatest element in the array is: " + arr[0]); // The first element is the greatest in descending order
System.out.println("The arranged array in descending order is: " + Arrays.toString(arr));
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
DescendingArray obj = new DescendingArray();
obj.inputArray(n);
obj.arrange();
}
}