堆排序
class Solution {
public:
//堆排序实现
void maxheapify(vector<int>& array, int i, int n){
int left = i * 2 + 1, right = i * 2 + 2, largest = i;
if(left < n && array[left] > array[largest]){
largest = left;
}
if(right < n && array[right] > array[largest]){
largest = right;
}
if(largest != i){
swap(array[i], array[largest]);
maxheapify(array, largest, n);
}
}
void buildMaxHeap(vector<int>& nums, int n){
for(int i = n / 2 - 1; i >= 0; --i){
maxheapify(nums, i, n);
}
}
int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
buildMaxHeap(nums, n);
for(int i = 0; i < k - 1; ++i){
swap(nums[0], nums[n - 1]);
n--;
maxheapify(nums, 0, n);
}
return nums[0];
}
};