class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 将给定数组排序
* @param arr int整型vector 待排序的数组
* @return int整型vector
*/
int Partition(vector<int>& arr,int l,int r){
int x=arr[l];
while(l<r){
while(l<r&&arr[r]>=x)
r--;
arr[l]=arr[r];
while(l<r&&arr[l]<x)
l++;
arr[r]=arr[l];
}
arr[l]=x;
return l;
}
void quickSort(vector<int>& arr,int l,int r){
if(l<r){
int mid=Partition(arr,l,r);
quickSort(arr, l, mid-1);
quickSort(arr, mid+1, r);
}
}
vector<int> MySort(vector<int>& arr) {
// write code here
int cd=arr.size();
quickSort(arr,0,cd-1);
return arr;
}
};
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int a=m-1,b=n-1;
for(int i=m+n-1;i>=0;i--){
if(b<0||(a>=0&&b>=0&&A[a]>B[b])){
A[i]=A[a];
a--;
}
else{
A[i]=B[b];
b--;
}
}
}
};
class Solution {
public:
int findKth(vector<int> a, int n, int K) {
// write code here
quickSort(a, 0, n-1);
return (K>=a.size()) ? -1 : a[n-K];
}
void quickSort(vector<int>& a, int l, int r)
{
if (l >= r)
return;
int x = a[l];
int i = l;
int j =r;
while(i < j)
{
while (a[j]>=x && i<j) j--;
while (a[i]<=x && i<j) i++;
if(i<j)
{
int swp = a[i];
a[i] = a[j];
a[j] = swp;
}
}
a[l] = a[i];
a[i] = x;
quickSort(a,l, i-1);
quickSort(a,i+1, r);
}
};
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
int cd=input.size();
vector<int> jg;
priority_queue<int>heap;
if(k<=0)
return jg;
for(int i=0;i<cd;i++){
if(i<k)
heap.push(input[i]);
else{
if(input[i]<heap.top())
{
heap.pop();
heap.push(input[i]);
}
}
}
while(!heap.empty()){
jg.push_back(heap.top());
heap.pop();
}
return jg;
}
};