Skip to content

Commit 7a67e56

Browse files
authored
Merge pull request #898 from AlgorithmWithGod/zinnnn37
[20250915] BOJ / G4 / 소트 / 김민진
2 parents db1ddae + e78aca1 commit 7a67e56

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_1083_소트 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static StringTokenizer st;
10+
11+
private static int N, S;
12+
private static int[] nums;
13+
14+
public static void main(String[] args) throws IOException {
15+
init();
16+
sol();
17+
}
18+
19+
private static void init() throws IOException {
20+
N = Integer.parseInt(br.readLine());
21+
22+
nums = new int[N];
23+
st = new StringTokenizer(br.readLine());
24+
for (int i = 0; i < N; i++) {
25+
nums[i] = Integer.parseInt(st.nextToken());
26+
}
27+
28+
S = Integer.parseInt(br.readLine());
29+
}
30+
31+
private static void sol() throws IOException {
32+
if (N == 1) {
33+
print();
34+
return;
35+
}
36+
37+
int idx = 0;
38+
while (S > 0 && idx < N) {
39+
int maxIdx = idx;
40+
41+
// find max
42+
for (int i = idx; i < N; i++) {
43+
if (i > idx + S) break;
44+
45+
if (nums[i] > nums[maxIdx]) {
46+
maxIdx = i;
47+
}
48+
}
49+
for (int j = maxIdx; j > idx; j--) {
50+
swap(j, j - 1);
51+
S--;
52+
}
53+
idx++;
54+
}
55+
print();
56+
}
57+
58+
private static void swap(int a, int b) {
59+
int tmp = nums[a];
60+
nums[a] = nums[b];
61+
nums[b] = tmp;
62+
}
63+
64+
private static void print() throws IOException {
65+
for (int i = 0; i < N; i++) {
66+
bw.write(nums[i] + " ");
67+
}
68+
bw.flush();
69+
bw.close();
70+
br.close();
71+
}
72+
73+
}
74+
```

0 commit comments

Comments
 (0)