-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthElement.java
More file actions
53 lines (49 loc) · 1.17 KB
/
KthElement.java
File metadata and controls
53 lines (49 loc) · 1.17 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
public class KthElement {
public static void main(String[] args) {
KthElement ke = new KthElement();
// int[] arr = { 2, 3, 4, 5, 6, 10, 10, 33, 48, 53 };
int[] arr = {10, 5, 4, 3, 48, 6, 2, 33, 53, 10};
int a = ke.kthSmallest(arr, 4);
System.out.println(a);
}
public int kthSmallest(int[] arr, int k) {
// Code here
return findKthElement(arr, k - 1, 0, arr.length - 1);
}
private int findKthElement(int[] arr, int k, int low, int high) {
int p = partition(arr, low, high);
if (p == k)
return arr[p];
if (p > k)
return findKthElement(arr, k, low, p - 1);
else
return findKthElement(arr, k, p + 1, high);
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int j = low;
while (true) {
if (j == high)
return high;
if (arr[j] > pivot)
break;
j++;
}
int i = j;
j++;
while (j < high) {
if (arr[j] < pivot) {
swap(arr, i, j);
i++;
}
j++;
}
swap(arr, i, high);
return i;
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}