使用openmp的并行冒泡排序 我写了一个C++代码用于冒泡排序算法,不知道如何使用OpenMP来实现它,请帮助我。 代码如下: #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); } #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; INTA[40001]; void sortArray(int[],int); int q=0; int _tmain(int argc,_TCHAR*argv[] { int x=40000; int值[40000]; 对于(int i=0;i

使用openmp的并行冒泡排序 我写了一个C++代码用于冒泡排序算法,不知道如何使用OpenMP来实现它,请帮助我。 代码如下: #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); } #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; INTA[40001]; void sortArray(int[],int); int q=0; int _tmain(int argc,_TCHAR*argv[] { int x=40000; int值[40000]; 对于(int i=0;i,c++,parallel-processing,bubble-sort,C++,Parallel Processing,Bubble Sort,请尝试以下算法: #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[400

请尝试以下算法:

#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);
}
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
平行分析

#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);
}
步骤1-10是一个大循环,表示n-1次。因此, 并行时间复杂度为O(n),如果算法需要奇数步 (n/2)-2个处理器和偶数步骤需要
(n/2)-1个处理器。因此,这需要O(n)个处理器

#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);
}
您仍然可以使用
swap
标志检查在
Next k
之前停止例程

当然,如果没有数百个物理处理器,不要期望速度有很大的提高:)

你将无法并行循环,因为每次迭代都取决于上一次迭代的结果。如果你想加快它,那么就使用除Bubblesort之外的任何算法。或者,更好的是,使用
std::sort
。我想你错过了一件事:你不能要求多个处理器在各自的迭代中运行,但是你可以并行化一个迭代。基本上,如果你有6个元素[0,1,2,3,4,5],那么你可以让一个处理器在[0,1],一个在[2,3]上,一个在[4,5]上。在下一个迭代中,你在[2,3]上有一个在[4,5]上有一个,然后你回到平方一。
#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);
}