Skip to content

Commit eb00071

Browse files
committed
sorting the element descending order
1 parent 10fb066 commit eb00071

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

sort-dsc.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class SortDsc {
2+
public static void main(String[] args) {
3+
//Initialize array
4+
int [] arr = new int [] {5, 2, 8, 7, 1};
5+
int temp = 0;
6+
7+
//Displaying elements of original array
8+
System.out.println("Elements of original array: ");
9+
for (int i = 0; i < arr.length; i++) {
10+
System.out.print(arr[i] + " ");
11+
}
12+
13+
//Sort the array in descending order
14+
for (int i = 0; i < arr.length; i++) {
15+
for (int j = i+1; j < arr.length; j++) {
16+
if(arr[i] < arr[j]) {
17+
temp = arr[i];
18+
arr[i] = arr[j];
19+
arr[j] = temp;
20+
}
21+
}
22+
}
23+
24+
System.out.println();
25+
26+
//Displaying elements of array after sorting
27+
System.out.println("Elements of array sorted in descending order: ");
28+
for (int i = 0; i < arr.length; i++) {
29+
System.out.print(arr[i] + " ");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)