Sort bubbles in parallel using openmp

I am writing C ++ code for the Bubble sorting algorithm, and I don't know how to make it parallel using openmp, so please help me ..... this is the code:

#include "stdafx.h"    
#include <iostream>
#include <time.h>
#include <omp.h>
using namespace std;

int a[40001];
void sortArray(int [], int);
int q=0;

int _tmain(int argc, _TCHAR* argv[])
{   
int x=40000;
int values[40000];
for (int i=0;i<x;i++)
{
    values[i]=rand();
}
cout << "Sorting Array .......\n";
clock_t start = clock();
sortArray(values, x);
 cout << "The Array Now Sorted\n";
printf("Elapsed Time : %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
cout << "\n";
}
 void sortArray(int array[], int size)  
{
  bool swap;
   int temp;
  do
  {
   swap = false;
  for (int count = 0; count < (size - 1); count++)
   {
   if (array[count] > array[count + 1])
  {
    temp = array[count];
    array[count] = array[count + 1];
    array[count + 1] = temp;
    swap = true;
  }
  }
  }while (swap);
}

it took about 13 seconds, and I tried to put ## pragma omp parallel for before "for statment" in the sortArray method, and it didn’t make any difference, it also took about 13 seconds ..... so please help me so fast as you can

+3
source share
1 answer

Try the Parallel Bubble Sort Algorithm :

1.  For k = 0 to n-2
2.  If k is even then
3.     for i = 0 to (n/2)-1 do in parallel
4.         If A[2i] > A[2i+1] then
5.             Exchange A[2i] ↔ A[2i+1]
6.  Else
7.     for i = 0 to (n/2)-2 do in parallel
8.         If A[2i+1] > A[2i+2] then
9.             Exchange A[2i+1] ↔ A[2i+2]
10. Next k

Parallel analysis

1-10 , n -1 . , O (n). , (n/2) - 2 (n/2) - 1 . O (n).

swap, Next k.
, :)

+6

Source: https://habr.com/ru/post/1727778/


All Articles