-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrSort.java
More file actions
40 lines (32 loc) · 1.06 KB
/
Copy pathArrSort.java
File metadata and controls
40 lines (32 loc) · 1.06 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
//write a java program to sort array elements in ascending order
import java.util.Scanner;
class ArrSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size Of array :");
int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter array elements ");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Original Array is");
for (int j = 0; j < size; j++) {
System.out.print(arr[j] + " ");
}
// sorting logic
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("\nAscending Array Elements ");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
}