#include <cstdio>
#include <ctime>
#include <omp.h>
#include <iostream>
#include <algorithm>
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;i<n;++i) A[i]=rand();
time_t first,second;
first=clock();
omp_set_num_threads(th);
#pragma omp parallel private(i) shared(root)
{
#pragma omp for
for(i=0;i<n;++i)
{
root=i;// compete to be the root of the balance tree
LC[i]=RC[i]=n+1;
}
#pragma omp for
for(i=
openMP实现的快排
这是一个使用OpenMP并行化的快速排序算法实现。通过设置线程数,程序读取序列长度和线程数量,生成随机序列,然后并行执行快速排序。最后,它比较了并行排序和标准库排序的时间,展示了并行化带来的效率提升。
摘要由CSDN通过智能技术生成