-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySorting.java
More file actions
46 lines (32 loc) · 1.21 KB
/
Copy pathArraySorting.java
File metadata and controls
46 lines (32 loc) · 1.21 KB
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
import java.util.Arrays;
import java.util.Scanner;
public class ArraySorting {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] numbers = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
sortDescending(numbers);
System.out.println("Sorted in descending order: " + Arrays.toString(numbers));
}
public static void sortDescending(int[] array) {
int n = array.length;
boolean swapped;
do {
swapped = false;
for (int i = 0; i < n - 1; i++) {
if (array[i] < array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
}
}