// // Created by RedmiBook on 2022/5/11. // #include <bits/stdc++.h> using namespace std; int partition(vector<int>&arr,int l,int r){ int rd = l+rand()%(r-l); swap(arr[rd],arr[r]); int key = arr[r]; int i=l; for(int j=l;j<r;j++){ if(arr[j] < key){ swap(arr[i],arr[j]); i++; } } swap(arr[i],arr[r]); return i; } void quicksort(vector<int>& arr,int l,int r){ if(l < r){ int mid = partition(arr,l,r); quicksort(arr,l,mid); quicksort(arr,mid+1,r); } } vector<int> MySort(vector<int>& arr) { // write code here if(arr.size() == 0){ return arr; } int len = arr.size(); quicksort(arr,0,len-1); return arr; } int main(){ srand(time(0)); vector<int> src; int tmp = 0; for(int i=0;i<100;i++){ tmp = rand()%1000+1; src.push_back(tmp); } quicksort(src,0,99); for(int i=0;i<100;i++){ cout<<src[i]<<"\t"; } return 0; }
// // Created by RedmiBook on 2022/5/11. // #include <bits/stdc++.h> using namespace std; void merge(int A[], int m, int B[], int n) { int i=0,j=0; //i指向A j指向B int *tmp = new int[m+n+1]; int k=0; while(i<m || j<n){ if(j>=n || ( i<m&&A[i]<B[j])){ tmp[k++] = A[i++]; }else{ tmp[k++] = B[j++]; } } for(int i=0;i<k;i++){ A[i] = tmp[i]; } delete []tmp; } int main(){ int A[6] = {0}; int B[3] = {0}; A[0] = 4; A[1] = 5; A[2] = 6; B[0] = 1; B[1] = 2; B[2] = 3; merge(A,3,B,3); for(int i=0;i<6;i++){ cout<<A[i]<<"\t"; } return 0; }
// // Created by RedmiBook on 2022/5/11. // #include <bits/stdc++.h> using namespace std; int partition1(vector<int>&arr,int l,int r){ int rd = r; if(r > l){ rd = l+rand()%(r-l); } swap(arr[rd],arr[r]); int key = arr[r]; int i=l; for(int j=l;j<r;j++){ if(arr[j] >= key){ swap(arr[j],arr[i]); i++; } } swap(arr[i],arr[r]); return i; } int partition2(vector<int>&arr,int l,int r){ int ld = l; if(r > l){ ld = l+rand()%(r-l); } swap(arr[l],arr[ld]); int key = arr[l]; while(l < r){ while(l < r && arr[l] > key){ l++; } arr[r] = arr[l]; while(l < r && arr[r] <= key){ r--; } arr[l] = arr[r]; } arr[l] = key; return l; } void quickSort(vector<int>&arr,int l,int r,int k){ int mid = partition2(arr,l,r); while(mid != k-1){ if(mid < k-1){ l=mid+1; mid = partition2(arr,l,r); }else{ r = mid-1; mid = partition2(arr,l,r); } } } //通过堆来实现 ==>大顶堆 int heapSort(vector<int>&arr,int n,int k){ priority_queue<int,vector<int>,greater<int>> heap; for(int i=0;i<n;i++){ if(i < k){ heap.push(arr[i]); }else{ if(heap.top() < arr[i]){ heap.pop(); heap.push(arr[i]); } } } return heap.top(); } int findKth(vector<int>&a, int n, int K) { // write code here if(K <= 0 || K>n) return -1; //quickSort(a,0,n-1,K); return heapSort(a,n,K); //return a[K-1]; } int main(){ srand(time(0)); vector<int>src; src.push_back(1); src.push_back(2); src.push_back(3); src.push_back(4); src.push_back(5); //cout<<"aaa"<<endl; cout<<findKth(src,5,2)<<endl; return 0; }
// // Created by RedmiBook on 2022/5/10. // #include <bits/stdc++.h> using namespace std; //解法1 快排解法 int partition(vector<int>&arr,int l,int r){ int rd = l+rand()%(r-l); swap(arr[rd],arr[l]); int key = arr[l]; while(l < r){ while(l < r && arr[r] >= key){ r--; } arr[l] = arr[r]; while(l < r && arr[l] < key){ l++; } arr[r] = arr[l]; } arr[l] = key; return l; } //解法2 快排解法 int partition2(vector<int>&arr,int l,int r){ int rd = l+rand()%(r-l); swap(arr[rd],arr[r]); int key = arr[r]; int i=l; for(int j=l;j<r;j++){ if(arr[j] < key){ swap(arr[j],arr[i]); i++; } } swap(arr[i],arr[r]); return i; } vector<int> GetLeastNumbers_Solution(vector<int>& input,int k){ vector<int>ans; int len = input.size(); if(k==0 || k>=len) {return ans;} int l=0,r=len-1; int mid = partition2(input,l,r); while(mid != k-1){ if(mid < k-1){ l = mid+1; mid = partition2(input,l,r); }else{ r = mid-1; mid = partition2(input,l,r); } } for(int i=0;i<k;i++){ ans.push_back(input[i]); } return ans; } //堆排序解法 vector<int> GetLeastNumbers_heap(vector<int>& input,int k){ vector<int> ans; int len = input.size(); if(k<0 || k>=len){return ans;} priority_queue<int> heap; for(int i=0;i<len;i++){ if(i < k){ heap.push(input[i]); }else{ if(input[i] < heap.top()){ heap.pop(); heap.push(input[i]); } } } for(int i=0;i<k;i++){ ans.push_back(heap.top()); heap.pop(); } return ans; } int main(){ srand(time(0)); //解法1 快排解法 vector<int> src; //4,5,1,6,2,7,3,8 src.push_back(4); src.push_back(5); src.push_back(1); src.push_back(6); src.push_back(2); src.push_back(7); src.push_back(3); src.push_back(8); vector<int>ans = GetLeastNumbers_heap(src,4); for(int i=0;i<ans.size();i++){ cout<<ans[i]<<" "; } return 0; }