There are a couple of things that wrong here:
(1) You allocate two temporary arrays al and ar and never free them. This is a memory leak.
(2) The loop
for(int i=0;i<n1;i++)>
al[i] = a[p+i-1];
picks from the wrong indices, it should be:
for(int i=0;i<n1;i++)>
al[i] = a[p+i];
(3) Obviously q denotes the last element of the left range. That means that the left range contains q - p + 1 elements. Hence n1 should be
int n1 = q - p + 1;
int n2 = r - q;
(4) The next loop also uses the wrong index range, it should be
for(int j=0;j<n2;j++)>
ar[j]=a[q+j + 1];
And these are probably not the only things that go wrong. Why don't you simply use a debugger to step through your code and see what it does?!
And, by the way, this sorting algorithm is very inefficient. The allocation and deallocation of the auxiliary arrays takes up a lot of time. But as it probably is homework, this might be okay.