-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankPsAlgorithms0074.java
More file actions
42 lines (30 loc) · 1.16 KB
/
HackerrankPsAlgorithms0074.java
File metadata and controls
42 lines (30 loc) · 1.16 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
41
42
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
public class HackerrankPsAlgorithms0074 {
// Insertion Sort - Part 1
// https://www.hackerrank.com/challenges/insertionsort1/problem?isFullScreen=true
static class Result {
public static void insertionSort1(int n, List<Integer> arr) {
int last = arr.get(n - 1);
int i = n - 2;
while (i >= 0 && arr.get(i) > last) {
arr.set(i + 1, arr.get(i));
i--;
System.out.println(arr.stream().map(String::valueOf).collect(Collectors.joining(" ")));
}
arr.set(i + 1, last);
System.out.println(arr.stream().map(String::valueOf).collect(Collectors.joining(" ")));
}
}
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> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Result.insertionSort1(n, arr);
bufferedReader.close();
}
}