MPI并行冒泡二路归并排序问题

我思路是想吧随机产生的数组a 按照总进程数分s份 然后分到别的进程冒泡排序,然后在聚集回来 二路归并下边是代码 求大神帮忙改改~~~
// mpi.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "mpi.h"
#include
#include
#include
#include
#include
using namespace std;

void merge(int x[],int z[],int s,int u,int v) //2-路·归并排序
{

int i,j,q;

i=s;

j=u+1;

q=s;

while(i<=u&&j<=v)

{

if(x[i]<=x[j])

z[q++]=x[i++];

else

z[q++]=x[j++];

}

while(i<=u) //将X中剩余元素X[i..u]复制到Z

z[q++]=x[i++];

while(j<=v) //将X中剩余元素X[j..v]复制到Z

z[q++]=x[j++];

}
void bubble(int a[],int n)
{
int i,j,t;
for(i=0;i for(j=0;j if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
void main(int argc, char* argv[])
{
MPI_Comm comm=MPI_COMM_WORLD;
int n,m,rank,size,i,r,s,step;
int *a,*b,*c,*d,*e;
a=(int *)malloc(n*sizeof(int));
e=(int *)malloc(n*sizeof(int));
double startwtime,endwtime;

MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

if(rank==0){
    cout<<"输入数列上限:";
    cin>>n;
    cout<<"输入整数范围:";
    cin>>m;
    srand(unsigned(time(0)));
    cout<<"产生的随机数列:";
    s=n/size;r=n%size;
    b=(int *)malloc(n+rank-r*sizeof(int));
    for(i=0;i<n;i++)
    {
        a[i]=rand()%m;
        b[i]=a[i];
        cout<<a[i]<<" ";
    }
    cout<<endl;
    if(r!=0)
    {   
        for(i=n;i<n+rank-r;i++)
        {
            b[i]=0;
        }
        s=s+1;
    }
startwtime=MPI_Wtime();
MPI_Scatter(a,s,MPI_INT,b,s,MPI_INT,0,comm);
bubble(b,s);
}
else 
{
    c=(int *)malloc(s*sizeof(int));
    MPI_Scatter(a,s,MPI_INT,c,s,MPI_INT,0,comm);
    bubble(c,s);
}
if(rank!=0)
{
    d=(int *)malloc(n*sizeof(int));
    MPI_Gather(d,s,MPI_INT,b,s,MPI_INT,0,comm);
    MPI_Gather(d,s,MPI_INT,c,s,MPI_INT,0,comm);
    merge(d,e,0,n/2-1,n-1);
}
if(rank==0){
    cout<<"排序结果:";
    for(i=0;i<n;i++)
    {
        cout<<e[i]<<" ";
    }
    cout<<endl;
    endwtime=MPI_Wtime();
    cout<<"wall clock time="<<endwtime-startwtime<<endl;
}
MPI_Finalize();

}

我思路是想吧随机产生的数组a 按照总进程数分s份 然后分到别的进程冒泡排序,然后在聚集回来 二路归并下边是代码 求大神帮忙改改~~~

ytrytfyg我思路是想吧随机产生的数组a 按照总进程数分s份 然后分到别的进程冒泡排序,然后在聚集回来 二路归并下边是代码 求大神帮忙改改~~~