class Solution
{
public:
void Merge(vector<int>& nums,int lo,int mi,int hi)
{
int* a=&*begin(nums)+lo;
int lb=mi-lo;
int* b=new int[lb];
for(int i=0;i<lb;++i)
b[i]=a[i]; //将a的前mi-lo个元素保存为副本b
int lc=hi-mi;
int* c=&*begin(nums)+mi; //c指向a中以mi开头的元素(即a的后半部)
for(int i=0,j=0,k=0;(j<lb)||(k<lc);) //i为a的索引,j为b上索引,k为c上索引
{ //将b或c中较小元素续至a的末尾,除非:若b的长度较长(此时k已经超出其长度lc),则将其后面元素直接接至a的末尾;若c的长度较长(此时j已经超出其长度lb),则将其后面元素直接接至a的末尾
if((j<lb)&&(k>=lc)||(b[j]<=c[k]))
a[i++]=b[j++];
if((k<lc)&&(j>=lb)||(b[j]>c[k]))
a[i++]=c[k++];
}
delete[] b;
}
void MergeSort(vector<int>& nums,int lo,int hi)
{
if(hi-lo<2)
return;
int mi=(lo+hi)/2;
MergeSort(nums,lo,mi);
MergeSort(nums,mi,hi);
Merge(nums,lo,mi,hi);
}
int findKthLargest(vector<int>& nums, int k)
{
int n=nums.size();
MergeSort(nums,0,n-1);
return nums.at(nums.size()-k);
}
};