fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. struct Compare { int val; int index; };
  5. #pragma omp declare reduction(maximum : struct Compare : omp_out = omp_in.val > omp_out.val ? omp_in : omp_out)
  6.  
  7. void selectionsort(int* arr, int size)
  8. {
  9. for (int i = size - 1; i > 0; --i)
  10. {
  11. struct Compare max;
  12. max.val = arr[i];
  13. max.index = i;
  14. #pragma omp parallel for reduction(maximum:max)
  15. for (int j = i - 1; j >= 0; --j)
  16. {
  17. if (arr[j] > max.val)
  18. {
  19. max.val = arr[j];
  20. max.index = j;
  21. }
  22. }
  23. int tmp = arr[i];
  24. arr[i] = max.val;
  25. arr[max.index] = tmp;
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. int x[10] = {8,7,9,1,2,5,4,3,0,6};
  32. selectionsort(x, 10);
  33.  
  34. for (int i = 0; i < 10; i++)
  35. printf("%d\n", x[i]);
  36. return 0;
  37. }
Success #stdin #stdout 0s 5368KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
7
8
9