对两个数组同时进行归并排序时出错
求助 对两个数组同时进行归并排序时出错(实际只对一个数组进行排序,另一个数组只是下标与进行排序的那个数组进行同步变化)**下面是仅对一个数组排序时的代码:*
#include <iostream>
using namespace std;
void merge(int *A,int low,int high,int mid){
int length = (high-low+1);
int i=low;
int j=mid+1;
int k1[length],k2[length];
int m = 0;
while(i<=mid&&j<=high){
if(A[i]<=A[j]){
k1[m++] = A[i++];
}else{
k1[m++] = A[j++];
}
}
while(i<=mid){
k1[m++] = A[i++];
}
while(j<=high){
k1[m++] = A[j++];
}
for(int p=0;p<m;p++){
A[low+p] = k1[p];
}
}
void mergeSort(int *A,int low,int high){
if(high>low){
int mid=(low+high)/2;
mergeSort(A,low,mid);
mergeSort(A,mid+1,high);
merge(A,low,high,mid);
}
else return;
}
int main()
{
int n;
cout<<"请输入数据:"<<endl;
cin>>n;
int A[n];
int a,b,flag=0,max,maxi;
for(int i=0;i<n;i++) cin>>A[i];
mergeSort(A,0,n-1);
cout<<"输出结果"<<endl;
for(int i=0;i<n;i++) cout<<A[i]<<" ";
}
```
运行结果:
**结果正确**
**下面是题中所说的两个数组的归并排序:**
```
#include <iostream>
using namespace std;
void merge(int *A,int *B,int low,int high,int mid){
int length = (high-low+1);
int i=low;
int j=mid+1;
int k1[length],k2[length];
int m1=0, m2=0;
while(i<=mid&&j<=high){
if(A[i]<=A[j]){
k1[m1++] = A[i++];
k2[m2++] = B[i++];
}else{
k1[m1++] = A[j++];
k2[m2++] = B[j++];
}
}
while(i<=mid){
k1[m1++] = A[i++];
k2[m2++] = B[i++];
}
while(j<=high){
k1[m1++] = A[j++];
k2[m2++] = B[j++];
}
for(int p=0;p<m1;p++){
A[low+p] = k1[p];
B[low+p] = k2[p];
}
}
void mergeSort(int *A,int *B,int low,int high){
if(high>low){
int mid=(low+high)/2;
mergeSort(A,B,low,mid);
mergeSort(A,B,mid+1,high);
merge(A,B,low,high,mid);
}
else return;
}
int main()
{
int n;
cout<<"请输入数据:"<<endl;
cin>>n;
int A[n],B[n];
int a,b,flag=0,max,maxi;
for(int i=0;i<n;i++) cin>>A[i]>>B[i];
mergeSort(A,B,0,n-1);
cout<<"输出结果"<<endl;
for(int i=0;i<n;i++) cout<<A[i]<<" "<<B[i]<<endl;
}
```
运行结果:
结果错误??????
不知道错在哪了