class Solution {
public int findKthLargest(int[] nums, int k) {
int n = nums.length;
return quickSort(nums,0,n-1,n-k);
}
public int quickSort(int[] arr, int L, int R, int k){
if(L==R) return arr[L];
swap(arr,R,L+(int) (Math.random()*(R-L+1)));
int[] scale = partition(arr,L,R);//返回等于arr[R]区域的左边界和右边界
if(k<scale[0]) return quickSort(arr,L,scale[0]-1,k);
else if(k>scale[1]) return quickSort(arr,scale[1]+1,R,k);
else return arr[k];
}
public int[] partition(int[] arr, int L, int R){
int index=L+1,right=R,left=L+1;//left指向即将要判断的下标
while(index<=right){
if(arr[index]<arr[L]){
swap(arr,index++,left++);
}else if(arr[index]>arr[L]){
swap(arr,index,right--);
}else{
index++;
}
}
swap(arr,L,left-1);
return new int[] {left,right};
}
public void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for(int x:nums){
if (q.size()<=k){
q.add(x);
}
if(q.size()>k){
q.poll();
}
}
return q.peek();
}
}