public class Finder {
public int findKth(int[] a, int n, int K) {
return help(0,n-1,a,K);
}
private int help(int left, int right, int[] a, int k) {
int m=partation(left,right,a);
if(m+1 > k){
return help(left,m-1,a,k);
}else if(m+1<k){
return help(m+1,right,a,k);
}else {
return a[m];
}
}
private int partation(int left, int right, int[] a) {
int pivotValue =a[left];
while (left<right){
while (left<right && a[right]<=pivotValue){
right--;
}
a[left]=a[right];
while (left<right && a[left]>=pivotValue){
left++;
}
a[right]=a[left];
}
a[left]=pivotValue;
return left;
}
}