I'm new to recursion and trying to write merge sort code but it is not working properly. I can't find my mistake. I have tried to dry run the code but failing in that and doesn't know the exact mistake.
#include<bits/stdc++.h>
using namespace std;
void merge(vector<int> &v,int lo,int mid,int hi){
vector<int> temp;
int i=lo;
int j=mid;
while(i<=mid-1 && j<=hi){
if(v[i]<=v[j]){
temp.push_back(v[i++]);
}
else{
temp.push_back(v[j++]);
}
}
while(i<=mid-1){
temp.push_back(v[i++]);
}
while(j<=hi){
temp.push_back(v[i++]);
}
for(int m=lo;m<=hi;i++){
v[m]=temp[m-lo];
}
}
void mergeSort(vector<int> &v,int lo,int hi){
if(lo<hi){
int mid=lo+(hi-lo)/2;
mergeSort(v,lo,mid);
mergeSort(v,mid+1,hi);
merge(v,lo,mid+1,hi);
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
mergeSort(v,0,n-1);
for(int i=0;i<n;i++){
cout<<v[i]<<" ";
}
cout<<endl;
}
return 0;
}