Skip to content

Commit 10fb066

Browse files
committed
sorting the element ascending order
1 parent a824f9e commit 10fb066

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

sort-asc.java

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

0 commit comments

Comments
 (0)