fork download
  1. #include <omp.h>
  2. #include <stdio.h>
  3.  
  4. int find_min_index(int *A, int start, int n) {
  5. int min_index = start;
  6. #pragma omp parallel for shared(A, min_index)
  7. for (int i = start + 1; i < n; i++) {
  8. #pragma omp critical
  9. if (A[i] < A[min_index]) {
  10. min_index = i;
  11. }
  12. }
  13. return min_index;
  14. }
  15.  
  16. void recursive_selection_sort(int *A, int start, int n) {
  17. if (start >= n - 1) return;
  18. int min_index = find_min_index(A, start, n);
  19. if (min_index != start) {
  20. int temp = A[start];
  21. A[start] = A[min_index];
  22. A[min_index] = temp;
  23. }
  24. #pragma omp task shared(A)
  25. recursive_selection_sort(A, start + 1, n);
  26. #pragma omp taskwait
  27. }
  28.  
  29. int main() {
  30. int A[] = {64, 25, 12, 22, 11};
  31. int n = sizeof(A) / sizeof(A[0]);
  32. #pragma omp parallel
  33. {
  34. #pragma omp single
  35. recursive_selection_sort(A, 0, n);
  36. }
  37. for (int i = 0; i < n; i++) {
  38. printf("%d ", A[i]);
  39. }
  40. printf("\n");
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
11 12 22 25 64