<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p>

<p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p>

<p>Can you solve it without sorting?</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [3,2,1,5,6,4], k = 2
<strong>Output:</strong> 5
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,2,3,1,2,4,5,5,6], k = 4
<strong>Output:</strong> 4
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int partition(vector<int>& nums, int low, int high) {
        int pivot = nums[high];
        int idx = low - 1;

        for (size_t k = low; k < high; ++k) {
            if (nums[k] > pivot) {
                ++idx;
                std::swap(nums[idx], nums[k]);
            }
        }
        std::swap(nums[idx+1],nums[high]);
        return idx + 1;
    }
    int quickSelect(vector<int>& nums, int low ,int high, int k) {
        if (low == high ) return nums[low];

        int pi = partition(nums, low, high);
        if ( pi ==k ) {
            return nums[pi];
        } else if (pi < k) {
            //right, 
            return quickSelect(nums, pi + 1, high, k);
        } else {
            // left, 
            return quickSelect(nums, low, pi - 1, k);
        }
    }
    int findKthLargest(vector<int>& nums, int k) {
        std::random_device rd;
        std::mt19937 g(rd());
        std::shuffle(nums.begin(), nums.end(), g); 
        int n = nums.size();
        return quickSelect(nums, 0, n-1, k-1);

    }


};
59800