-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.java
35 lines (31 loc) · 974 Bytes
/
Sorting.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
public class Solution {
static int count;
public static int[] sorting(int[] a) {
count = 0;
for (int i = 0; i < a.length; i++) {
int temp = 0;
for (int l = 0; l < a.length-1; l++) {
if (a[l] > a[l+1]) {
count++;
temp = a[l+1];
a[l+1] = a[l];
a[l] = temp;
}
}
if (count == 0) break;
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
a = sorting(a);
System.out.println("Array is sorted in " + count + " swaps.");
System.out.println("First Element: " + a[0]);
System.out.println("Last Element: " + a[a.length-1]);
}
}