-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortDescending.java
More file actions
28 lines (27 loc) · 996 Bytes
/
Copy pathBubbleSortDescending.java
File metadata and controls
28 lines (27 loc) · 996 Bytes
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
import java.io.*;
public class BubbleSortDescending {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER OF ELEMENTS=");
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
System.out.println("ENTER THE NUMBERS=");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[j] < arr[j + 1]) { // Change the comparison to sort in descending order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("SORTED ELEMENTS = ");
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
}
}
}