There is a parallel solution to this problem, but it is probably not worth the effort.
First, we define the operation xchg(m, n) from the array a:
xchg(m, n) => ((a[m] > a[n] && a[n] != 0) || a[m] == 0) ? swap(a[m],a[n])
This operation sorts the two elements "m" and "n" in ascending order if they both contain nonzero values ββor change them if the value in the element "m" is zero.
Next, we perform a set of five such operations as follows:
xchg(0,2) xchg(1,3) xchg(0,1) xchg(2,3) xchg(1,2)
Paralyzed xchg operations can be performed in parallel, reducing time costs by 40% for strictly sequential execution. When we are done, any non-zero elements of the array will be sorted in ascending order. The smallest element will be at [0]. If this value is zero, there are no nonzero values ββin the array.
This solution uses the built-in parallelism provided by the sorting networks ( http://en.wikipedia.org/wiki/Sorting_network ), but sequential scanning of 4 elements also uses no more than three comparison operations, and to a decisive extent, it takes half as much storage entry :
sequential scan
int v = a[0] for (n = 1; n < 4; n++) { if ((a[n] < v && a[n] != 0 ) || v == 0) v = a[n] }
Vince source share