/* heap sort */ #include #include #include #define N 1000000 void showElapsed(int id, char *m); void showVector(int *v, int n, int id); int * merge(int *v1, int n1, int *v2, int n2); void Sort( int *T, int root, int bottom, int *operation, int *test, int *swap); void heapsort( int *T, int n); double startTime, stopTime; void showElapsed(int id, char *m) { printf("%d: %s %f secs\n",id,m,(clock()-startTime)/CLOCKS_PER_SEC); } void showVector(int *v, int n, int id) { int i; printf("%d: ",id); for(i=0;i=T[child2])?child1:child2; /* maxchild take the number of the max value between child 1 and 2 */ test++; /* T[child1]>=T[child2] */ operation++; /* affectation of maxchild */ test++; /* child1==bottom */ } if ( T[root]< T[maxchild] ){ temp=T[root]; T[root]=T[maxchild]; T[maxchild]=temp; /* if the maxvalue is bigger than the value of the root, // the 2 values change. */ swap++; operation=operation+3; Sort(T, maxchild, bottom, operation, test, swap); /* recursive calling of the function Sort */ } test++; } test++; /* child1<=bottom */ } void heapsort( int *T, int n){ /* this function build a heap and sort it*/ int i, temp; int operation=0; int test=0; int swap=0; /* // T is the array to sort, n the number of element in T, i is a // loop indice, temp is needed for exchanging the 2 elements of // the table. */ for (i=(n/2)-1; i>=0; i--){ Sort (T,i,n-1, &operation, &test, &swap); test++; /* i>=0 */ operation++; /* initialisation + incrementation of i */ } for (i=n-1; i>=1; i--){ test++; /* i>=1 */ operation++; /* affectation + incrementation of i */ temp=T[0]; T[0]=T[i]; T[i]=temp; /* puts the bigger element at the end */ swap++; operation=operation+3; Sort (T, 0,i-1, &operation, &test, &swap); /* sort the rest of the table */ operation++; /* i-1 */ } } main(int argc, char **argv) { int * data; int * chunk; int * other; int m,n=N; int id,p; int s; int i; int step; MPI_Status status; startTime = clock(); MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&id); MPI_Comm_size(MPI_COMM_WORLD,&p); showElapsed(id,"MPI setup complete"); if(id==0) { int r; srandom(clock()); s = n/p; r = n%p; data = (int *)malloc((n+s-r)*sizeof(int)); for(i=0;i