Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
8Activity
0 of .
Results for:
No results containing your search query
P. 1
4[1]. Quick Sort

4[1]. Quick Sort

Ratings: (0)|Views: 663|Likes:
Published by preethy27

More info:

Published by: preethy27 on Dec 14, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/06/2013

pdf

text

original

 
PARALLEL QUICK SORT
#include<stdio.h>#include<mpi.h>#include<stdlib.h>#define SIZE 5000int a[SIZE],s[SIZE],e[SIZE],ind=0;void swap(int *x,int *y){int t;t = *x;*x = *y;*y = t;}void quick(int m,int n){int i,j,key;if(m<n){while(1){i=m+1;j=n;key=a[m];while(a[i]<key)i++;while(a[j]>key)j--;if(i<j)swap(&a[i],&a[j]);elsebreak;}swap(&a[m],&a[j]);if(m!=j){s[ind]=m;e[ind]=j-1;quick(s[ind],e[ind]);ind++;} if(n!=j){s[ind]=j+1;e[ind]=n;
 
quick(s[ind],e[ind]);ind++;}}}void display(int c,int d){int g;for(g=c;g<=d;g++)printf("%12d\t",a[g]);printf("\n");}int main(int arg,char *argv[]){int rank,size;int k,si,c,d,pi;MPI_Init(&arg,&argv);MPI_Comm_size(MPI_COMM_WORLD,&size);MPI_Comm_rank(MPI_COMM_WORLD,&rank);MPI_Status status;double t1,t2,t3,t4,t5,t6,t7;int WORKERS=(size-1);int DSIZE=SIZE/WORKERS;int EXT=SIZE%WORKERS;int MASTER=(size-1);int LAST=(WORKERS-1);for(k=0;k<SIZE;k++)a[k]=rand();if(rank == MASTER){int x,y;t1 = MPI_Wtime();for(k=0;k<WORKERS;k++){if(k!=LAST) pi = DSIZE;elsepi = DSIZE+EXT;x = DSIZE*k;y = x+pi-1;MPI_Send(&x,1,MPI_INT,k,101,MPI_COMM_WORLD);MPI_Send(&y,1,MPI_INT,k,102,MPI_COMM_WORLD);} MPI_Recv(&t6,1,MPI_DOUBLE,LAST,203,MPI_COMM_WORLD,&status);t2 = MPI_Wtime();
 
t3 = t2-t1;printf("\nTotal Numbers: %d",SIZE);printf("\nNumber of Working Processors: %d\n",WORKERS);printf("\nTHE SORTED LIST:\n\n");quick(0,(SIZE-1));display(0,(SIZE-1));printf("\nThe Computation Time : %lf Seconds",t6);printf("\nThe Communication Time : %lf Seconds\n\n",t3-t6);}else{MPI_Recv(&c,1,MPI_INT,MASTER,101,MPI_COMM_WORLD,&status);MPI_Recv(&d,1,MPI_INT,MASTER,102,MPI_COMM_WORLD,&status);t4 = MPI_Wtime();quick(c,d);t5 = MPI_Wtime();t6 = t5-t4;MPI_Send(&t6,1,MPI_DOUBLE,MASTER,203,MPI_COMM_WORLD);}MPI_Finalize();exit(0);}
Analysis of Parallel Quick Sort***************************
Total Numbers:
50
 Number of Working Processors:
3
THE SORTED LIST:
35005211 149798315 233665123 278722862 294702567304089172 336465782 424238335 468703135 521595368

Activity (8)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Uma Umayanganie liked this
Sura ALfadly liked this
cezortia liked this
cezortia liked this
bauoc liked this
Shaymaa Elshahat liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->