-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11004.java
More file actions
68 lines (58 loc) · 1.68 KB
/
Copy pathJ11004.java
File metadata and controls
68 lines (58 loc) · 1.68 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
package TIL;
import java.io.*;
import java.util.*;
public class J11004 {
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
int num = Integer.parseInt(input[0]);
int key = Integer.parseInt(input[1]);
arr = new int[num];
input = buffer.readLine().split(" ");
for (int i = 0; i < num; i++) {
arr[i] = Integer.parseInt(input[i]);
}
quickSort(0, num-1);
System.out.println(arr[key]);
}
public static void quickSort(int start, int end){
if(start == end){
return;
}
end = end-1;
int pivot = arr[end];
while(start <= end) {
if (arr[start] < pivot)
start++;
if (arr[end] > pivot)
end--;
if(arr[start] >pivot && arr[end] < pivot){
swap(start, end);
}
if(start == end){
if(arr[start]< pivot){
Shift(start-1);
arr[start-1] = pivot;
}else{
Shift(start+1);
arr[start+1] = pivot;
}
}
}
if (start == end) {
quickSort(0,start);
quickSort(start+1,end);
}
}
public static void Shift(int index){
for(int i = arr.length; i> index; i++){
arr[i] = arr[i-1];
}
}
public static void swap(int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}