class Solution { public: /** * @param A an integer array * @return void */ int partion(vector<int>& num,int left,int right) { int poit=num[left]; while(left<right) { while(left<right&&num[right]>=poit) right--; num[left]=num[right]; while(left<right&&num[left]<poit) left++; num[right]=num[left]; } num[left]=poit; return left; } void qsort(vector<int>& num,int left,int right) { if(left<right) { int idx=partion(num,left,right); qsort(num,left,idx-1); qsort(num,idx+1,right); } } void sortIntegers2(vector<int>& A) { // Write your code here qsort(A,0,A.size()-1); } };
class Solution { public: /** * @param A an integer array * @return void */ void merge(vector<int>& num,int left,int median,int right) { int p1=left,p2=median+1; vector<int> vec; while(p1<=median&&p2<=right) { if(num[p1]<num[p2]) { vec.push_back(num[p1]); p1++; } else { vec.push_back(num[p2]); p2++; } } while(p1<=median) { vec.push_back(num[p1]); p1++; } while(p2<=right) { vec.push_back(num[p2]); p2++; } int j=0; for(int i=left;i<=right;i++) num[i]=vec[j++]; } void msort(vector<int>& num,int left,int right) { if(left<right) { int median=(left+right)/2; msort(num,left,median); msort(num,median+1,right); merge(num,left,median,right); } } void sortIntegers2(vector<int>& A) { // Write your code here msort(A,0,A.size()-1); } };