-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortArray_1.java
42 lines (35 loc) · 927 Bytes
/
SortArray_1.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
42
package leadrat.task1;
import java.util.Scanner;
public class SortArray_1 {
public static int[] sort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
}
return arr;
}
public static void main(String[] args) {
// int[] arr = { 4, 2, 9, 4, 1, 8, 0, 3 };
// for (int ele : arr) {
// System.out.print(ele + " ");
// }
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an Array");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter the elements of an Array");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
sc.close();
int[] res = sort(arr);
for (int ele : res) {
System.out.print(ele + " ");
}
}
}