-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorted.java
41 lines (36 loc) · 1.33 KB
/
Sorted.java
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
import java.util.Scanner;
public class Sorted {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of an array:");
int n = sc.nextInt();
//Initialize array
int [] arr = new int [n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int temp = 0;
//Displaying elements of original array
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Sort the array in ascending order
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}