template<class Object>
void mergeSort(vector<Object> &a, vector<Object> &tmp; int left; int right){
if(left<right){
int center=(left+right)/2;
mergeSort(a, tmp, left, center);
mergeSort(a, tmp, center+1, right);
merge(a, tmp, left, center+1, right);
}
}
template<class Object>
void merge(vector<Object> &a; vector<Object> &tmp; int leftPos; int rightPos; int rightEnd){
int leftEnd=rightPos-1;
int tmpPos=leftPos;
int num=rightEnd-leftPos+1;
while(leftPos<=leftEnd && rightPos<=rightEnd){
if(a[leftPos]<=a[rightPos]){
tmp[tmpPos++]=std::move(a[leftPos++]);
}
else{
tmp[tmpPos++]=std::move(a[rightPos++]);
}
}
while(leftPos<=leftEnd){
tmp[tmpPos++]=std::move(a[leftPos++]);
}
while(rightPos<=rightEnd){
tmp[tmpPos++]=std::move(a[rightPos++]);
}
for(int i=0; i<num; ++i,--rightEnd){
a[rightEnd]=std::move(tmp[rightEnd]);
}
}
归并排序
最新推荐文章于 2023-09-03 20:36:33 发布