#include <omp.h>
#include <stdio.h>
int find_min_index(int *A, int start, int n) {
int min_index = start;
#pragma omp parallel for shared(A, min_index)
for (int i = start + 1; i < n; i++) {
#pragma omp critical
if (A[i] < A[min_index]) {
min_index = i;
}
}
return min_index;
}
void recursive_selection_sort(int *A, int start, int n) {
if (start >= n - 1) return;
int min_index = find_min_index(A, start, n);
if (min_index != start) {
int temp = A[start];
A[start] = A[min_index];
A[min_index] = temp;
}
#pragma omp task shared(A)
recursive_selection_sort(A, start + 1, n);
#pragma omp taskwait
}
int main() {
int A[] = {64, 25, 12, 22, 11};
int n = sizeof(A) / sizeof(A[0]);
#pragma omp parallel
{
#pragma omp single
recursive_selection_sort(A, 0, n);
}
for (int i = 0; i < n; i++) {
}
return 0;
}
I2luY2x1ZGUgPG9tcC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCmludCBmaW5kX21pbl9pbmRleChpbnQgKkEsIGludCBzdGFydCwgaW50IG4pIHsKICAgIGludCBtaW5faW5kZXggPSBzdGFydDsKICAgICNwcmFnbWEgb21wIHBhcmFsbGVsIGZvciBzaGFyZWQoQSwgbWluX2luZGV4KQogICAgZm9yIChpbnQgaSA9IHN0YXJ0ICsgMTsgaSA8IG47IGkrKykgewogICAgICAgICNwcmFnbWEgb21wIGNyaXRpY2FsCiAgICAgICAgaWYgKEFbaV0gPCBBW21pbl9pbmRleF0pIHsKICAgICAgICAgICAgbWluX2luZGV4ID0gaTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gbWluX2luZGV4Owp9Cgp2b2lkIHJlY3Vyc2l2ZV9zZWxlY3Rpb25fc29ydChpbnQgKkEsIGludCBzdGFydCwgaW50IG4pIHsKICAgIGlmIChzdGFydCA+PSBuIC0gMSkgcmV0dXJuOwogICAgaW50IG1pbl9pbmRleCA9IGZpbmRfbWluX2luZGV4KEEsIHN0YXJ0LCBuKTsKICAgIGlmIChtaW5faW5kZXggIT0gc3RhcnQpIHsKICAgICAgICBpbnQgdGVtcCA9IEFbc3RhcnRdOwogICAgICAgIEFbc3RhcnRdID0gQVttaW5faW5kZXhdOwogICAgICAgIEFbbWluX2luZGV4XSA9IHRlbXA7CiAgICB9CiAgICAjcHJhZ21hIG9tcCB0YXNrIHNoYXJlZChBKQogICAgcmVjdXJzaXZlX3NlbGVjdGlvbl9zb3J0KEEsIHN0YXJ0ICsgMSwgbik7CiAgICAjcHJhZ21hIG9tcCB0YXNrd2FpdAp9CgppbnQgbWFpbigpIHsKICAgIGludCBBW10gPSB7NjQsIDI1LCAxMiwgMjIsIDExfTsKICAgIGludCBuID0gc2l6ZW9mKEEpIC8gc2l6ZW9mKEFbMF0pOwogICAgI3ByYWdtYSBvbXAgcGFyYWxsZWwKICAgIHsKICAgICAgICAjcHJhZ21hIG9tcCBzaW5nbGUKICAgICAgICByZWN1cnNpdmVfc2VsZWN0aW9uX3NvcnQoQSwgMCwgbik7CiAgICB9CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByaW50ZigiJWQgIiwgQVtpXSk7CiAgICB9CiAgICBwcmludGYoIlxuIik7CiAgICByZXR1cm4gMDsKfQo=