openMP实现的快排

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define N 10040000

int A[N],f[N],LC[N],RC[N],ans[N],cnt;
int n;// length of sequence
int th;// number of thread
void output(int i)
{
	if(i>=n) return;
	output(LC[i]);
	ans[cnt++]=A[i];
	output(RC[i]);
}

int main ()
{
	int i,root;
	bool flag=false;
	double f1,f2;
	int T=12;
	while(scanf("%d%d",&n,&th)!=EOF) // input the length of sequence and number of thread
	{
		cnt=0;
	//generate the original sequence
	srand(time(0));
	for(i=0;in)// if no left child
					{
						LC[f[i]]=i;
						flag=true;
					}
					else f[i]=LC[f[i]];
				}
				else //go to right
				{
					if(RC[f[i]]>n)// if no right child
					{
						RC[f[i]]=i;
						flag=true;
					}
					else f[i]=RC[f[i]];
				}
			}
		}
	}
	output(root); // recursively get the sorted sequence
	second=clock();
	printf("n is %d\n",n);
	printf("parallel time is %lf\n",f1=(second-first)*1.0/CLOCKS_PER_SEC);
	first=clock();
	sort(A,A+n);
	second=clock();
	printf("sequence time is %lf\n",f2=(second-first)*1.0/CLOCKS_PER_SEC);
	printf("rate is %lf\n",f2/f1);
	}
	return 0;
}



你可能感兴趣的:(算法)