-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.java
40 lines (33 loc) · 1.23 KB
/
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
36
37
38
39
40
/*
Problem Title: Sorting
Problem URL: https://www.hackerrank.com/challenges/30-sorting/problem
Max Score: 30
Score: 30
Language: Java
Category: 30 Days of Code!
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
bufferedReader.close();
int numberOfSwaps = 0;
for (int i = 0; i < n; ++i) {
boolean swaped = false;
for (int j = 0; j < n-1; ++j)
if (a.get(j) > a.get(j+1)) {
Collections.swap(a, j, j+1);
numberOfSwaps++;
swaped = true;
}
if (!swaped)
break;
}
System.out.printf("Array is sorted in %d swaps.\n", numberOfSwaps);
System.out.printf("First Element: %d\n", a.get(0));
System.out.printf("Last Element: %d\n", a.get(n-1));
}
}