Merge Sort
Last updated
//this function sorts arr from index start to end
template <class TYPE>
void mergeSort(vector<TYPE>& arr, vector<TYPE>& tmp, int start, int end){
if (start<end){
int mid=(start+end)/2;
mergeSort(arr,tmp,start,mid);
mergeSort(arr,tmp,mid+1,end);
merge(arr,tmp,start,mid+1,end);
}
}template <class TYPE>
void merge(vector<TYPE>& arr, vector<TYPE>& tmp, int start,
int start2, int end){
int aptr=start;
int bptr=start2;
int i=start;
while(aptr<start2 && bptr <= end){
if(arr[aptr]<arr[bptr])
tmp[i++]=arr[aptr++];
else
tmp[i++]=arr[bptr++];
}
while(aptr<start2){
tmp[i++]=arr[aptr++];
}
while(bptr<=end){
tmp[i++]=arr[bptr++];
}
for(i=start;i<=end;i++){
arr[i]=tmp[i];
}
}