import java.util.*;

public class Solution {
    public int findKth(int[] a, int n, int k) {
        // write code here
        return findK(a, 0, n - 1, k - 1);
    }

   public static int findK(int[] nums, int start, int end, int k){
        int left = start, right = end;
        int x = nums[left + (right - left)/2];
        while(left <= right){
            while (left <= right && nums[left] > x) {
                left++;
            }
            while (left <= right && nums[right] < x) {
                right--;
            }
            if(left <= right){
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
            }
        }
        if(k <= right) {
            return findK(nums, start, right, k);
        }else if(k >= left){
            return findK(nums, left, end, k);
        }else{
            return nums[k];
        }
    }
}