c++ 归并排序,下面的代码输出是错的
#include <bits/stdc++.h>
using namespace std;
int a[1005],help[1005];
void mergesort(int L,int R)
{
if(L>=R)
{
return;
}
int mid=(L+R)/2;
mergesort(L,mid);
mergesort(mid+1,R);
int l=L,r=mid+1,pos=L;
while(l<=mid||r<=R)
{
if(r>R||(l<=mid&&a[l]<a[r]))
{
help[pos++]=a[l++];
}
else
{
help[pos++]=a[r++];
}
for(int i=L;i<=R;i++)
{
a[i]=help[i];
}
}
}
int main()
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i];
}
mergesort(1,n);
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
为什么,可以帮我看一下嘛