I'm hitting a wall with this. I'm trying to implement merge sort to an existing program. (one that utilizes serveral search algorithms) but I'm unable to get the program to sort correctly. The rest of the program works correctly as i've tested with other sort algorithms.
Merge sort code
Here's my current input/ output arraysCode:int mergesort (int a[], int p, int r)
{
int i;
int q;
double x=(p+r)/2;
if (p<r)
{
q= floor (x);
mergesort (a,p,q);
mergesort (a,q+1,r);
merge(a,p,q,r);
cout << "\n";
}
return 0;
}
int merge (int a[], int p, int q, int r)
{
int k=p;
int i=0;
int l=q-1;
int t[r];
while ((p<=l)&&(q<=r))
{
if (a[p]<=a[q])
{
t[i]=a[p];
i++;
p++;
}
else
{
t[i]=a[q];
i++;
q++;
}
}
while (p<=l)
{
t[i]=a[p];
i++;
p++;
}
while (q<=r)
{
t[i]=a[q];
i++;
q++;
}
for (i=k; i<=r; i++)
{
a[i]=t[i-k];
}
return 0;
}
I can see that it's trying to do some sorting, but it's not fully sorting the data.Code:Input
5 8 85 9 78 34 2 6 31 56 7 16 89 476 84 8 22 65 900 76
output
5 8 34 2 6 31 56 7 78 16 84 65 76 85 9 89 476 8 22 900
Any help would be appreciated.