Have a look at your merging part of code:
for(k = lo;k <= hi;k++){
if(left[i] < right[j]){
a[k] = left[i];
i = i + 1;
}else{
a[k] = right[j];
j = j+1;
}
}
Lets assume that
left[] = {1, 2}
right[] = {3, 4, 5}
After 2 iterations of your merging loop (k) the value of "i" will be 2. From now on, you will try to compare
left[2] < right[j]
Which is invalid, as left[2] will refer to some random memory (left array size is only 2, so element of index 2 doesnt exist, only 0 and 1)
So, if you add guards for the i and j values, for example by changing first if condition to:
if(i != n1 && (j == n2 || left[i] < right[j])){
You should be fine.
Anyway, I should also tell you that you shouldnt declare array sizes with values that are not const, like:
int left[n1];
int right[n2];
Its actually forbidden by standard. You should either allocate it dynamically, use vectors(if c++) or jus declare them global with large enough size (maximum n value)