0% found this document useful (0 votes)
92 views1 page

Quicksort Openmp

This document contains code for implementing a parallel quicksort algorithm using OpenMP. It defines a quicksort function that recursively sorts halves of an array using pivot partitioning. The main function initializes an array with random values, prints it, calls quicksort on the array using OpenMP tasks, and prints the sorted array. It sets the number of threads to 4 and uses OpenMP directives like parallel, single, and task to parallelize quicksort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views1 page

Quicksort Openmp

This document contains code for implementing a parallel quicksort algorithm using OpenMP. It defines a quicksort function that recursively sorts halves of an array using pivot partitioning. The main function initializes an array with random values, prints it, calls quicksort on the array using OpenMP tasks, and prints the sorted array. It sets the number of threads to 4 and uses OpenMP directives like parallel, single, and task to parallelize quicksort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/* ordenamiento quickSort */

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
void quickSort(double*, int);
void quickSort(double *vector, int n) {
int i, j;
double t,p;
if (n < 2)
return;
p = vector[n / 2];
for (i=0,j=n-1;; i++, j--) {
while (vector[i] < p)
i++;
while (p < vector[j])
j--;
if (i >= j)
break;
t = vector[i];
vector[i] = vector[j];
vector[j] = t;
}
#pragma omp task
quickSort(vector, i);
#pragma omp task
quickSort(vector + i, n - i);
#pragma omp taskwait
}
int main(){
int n=10;
double vector[n];
int i;
omp_set_num_threads(4);
printf("\nInicializando el vector:\n");
#pragma omp parallel for ordered schedule(static,5)
for(i=0;i<n;i++)
vector[i]=rand()%n + (double)rand()/RAND_MAX;
printf("\n");
printf("Vector antes de quickSort:\n");
#pragma omp parallel for ordered schedule(static,5)
for(i=0;i<n;i++)
#pragma omp ordered
printf("%.4lf ",vector[i]);
printf("\n");
#pragma omp parallel
#pragma omp single
quickSort(vector,n);
printf("\n");
printf("Vector despues de quickSort:\n");
#pragma omp parallel for ordered schedule(static,5)
for(i=0;i<n;i++)
#pragma omp ordered
printf("%.4lf ",vector[i]);
printf("\n\n");
return 0;
}

You might also like