Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 

You may assume k is always valid, 1 ≤ k ≤ array's length.


class Solution {
public:
	int findKthLargest(vector<int>& nums, int k) {
		return helper(nums, 0, nums.size() - 1, nums.size()-k);
	}
private:
	int helper(vector<int>& nums, int left, int right,int k){
		int i = left, j = right;
		int pivot = nums[i];
		while (i < j){
			while (nums[j] >= pivot && i < j) j--;
			if (i < j)
				nums[i++] = nums[j];
			while (nums[i] < pivot && i < j) i++;
			if (i < j)
				nums[j--] = nums[i];
		}
		nums[i] = pivot;
		if (i == k){
			return pivot;
		}
		else if (i>k){
			return helper(nums, left, i - 1, k);
		}
		else{
			return helper(nums, i + 1, right, k);
		}
	}
};