主题:合并问题求助
#include<iostream>
#include<cfloat>
using namespace std;
//将已经按从小到大排好序的子序列A[p...q]和A[q+1...r]合并排序保存到A中
void merge(int *A, int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int *L = new int[n1 + 1];
int *R = new int[n2 + 1];
int i;
int j;
for (i = 0; i < n1; ++i)
L[i] = A[p + i- 1];
for (j = 0; j < n2; ++j)
R[j] = A[q + j];
L[n1]=INT_MAX;//哨兵
R[n2]=INT_MAX;//哨兵
i = 0;
j = 0;
int k;
for (k = p; k < r +1; ++k)
{
if (L[i] <= R[j])
{
A[k] = L[i];
++i;
}
else
{
A[k] = R[j];
++j;
}
}
delete [ ] L;
delete [ ] R;
}
int main()
{
int a[8];
for (int i=0;i<8;i++)
cin>>a[i];
merge(a,0,3,7);
for (i=0;i<8;i++)
cout<<a[i]<<' ';
return 0;
}
输入 2 4 5 7 1 2 3 6
输出 7 1 2 3 8 2 4 5;
不知道程序哪里错了,路过的说说啊!
谢谢