Insertion Sort with OpenMP

The OpenMP ARB supports various international events to help educate and inform the OpenMP community. Notices about upcoming conferences and events will be posted here.
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help: https://stackoverflow.com/questions/tagged/openmp
Locked
mushthielv
Posts: 1
Joined: Mon Aug 29, 2016 8:41 am

Insertion Sort with OpenMP

Post by mushthielv »

I'm trying to parallelize an insertion sort algorithm in the following way:

Code: Select all

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void insertionsort(int vetor[], int tam)
{
    int i, j, tmp;
    omp_set_num_threads(4);

    #pragma omp parallel for shared (vetor, tam, tmp) private (i,j)


    for(i = 1; i < tam; i++)
    {
        j = i;

      
        while(j > 0 && vetor[j-1] > vetor[j])
        {

            tmp = vetor[j];
            vetor[j] = vetor[j-1];
            vetor[j-1] = tmp;
            j--;
        }
    }

}
But doesn't return the correct ordering. Can someone help me?

MarkB
Posts: 808
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: Insertion Sort with OpenMP

Post by MarkB »

You can't parallelise insertion sort like this: each iteration of the loop must be executed after the previous one.

(Please post questions like this in the Using OpenMP forum http://openmp.org/forum/viewforum.php?f=3 ).

Locked