Repository navigation
Expand file tree
/
Copy pathMain_11004.java
More file actions
77 lines (64 loc) · 1.65 KB
/
Copy pathMain_11004.java
File metadata and controls
77 lines (64 loc) · 1.65 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_11004 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
quickSort(arr, 0, arr.length - 1, k - 1);
System.out.println(arr[k-1]);
}
public static void quickSort(int[] arr, int s, int e, int k) {
if (s < e) {
int pivot = getPivot(arr, s, e);
if (pivot == k) {
return ;
} else if (k < pivot) {
quickSort(arr, s, pivot - 1, k); // 왼쪽 정렬
} else {
quickSort(arr, pivot + 1, e, k); // 오른쪽 정렬
}
}
}
public static int getPivot(int[] arr, int s, int e) {
if (s + 1 == e) { // s와 e가 만나서 s가 e보다 컸다면 swap하고 pivot을 리턴
if (arr[s] > arr[e]) {
swap(arr, s, e);
}
return e;
}
int middle = (s+e) / 2; // 중간값 pivot
swap(arr, s, middle);
int pivot = arr[s];
int i = s + 1;
int j = e;
while (i <= j) {
while(j > 0 && pivot < arr[j]) {
j--;
}
while(i < arr.length - 1 && pivot > arr[i]) {
i++;
}
if (i <= j) {
swap(arr, i++, j--);
}
}
arr[s] = arr[j];
arr[j] = pivot;
return j;
}
public static void swap(int[] arr, int s, int e) {
int temp = arr[e];
arr[e] = arr[s];
arr[s] = temp;
}
}