+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

jspspikeFinished PMerge73bc9bb

master
1.5 KiB55 linesraw
1// UT-EID=jkj858
2// UT-EID=jbr2558
3
4import java.util.*;
5import java.util.concurrent.*;
6
7public class PSort extends RecursiveAction {
8    int[] A;
9    int begin;
10    int end;
11
12    public PSort(int[] A, int begin, int end) {
13        this.A = A;
14        this.begin = begin;
15        this.end = end;
16    }
17
18    public static void parallelSort(int[] A, int begin, int end) {
19        ForkJoinPool pool = new ForkJoinPool();
20        pool.invoke(new PSort(A, begin, end));
21        // System.out.print("New A:");
22        // for (int k = begin; k < end; ++k) { System.out.print(" " + A[k]); }
23        // System.out.println();
24    }
25
26    @Override
27    protected void compute() {
28        if (end - begin <= 16) {
29            for (int i = begin + 1; i < end; ++i) {
30                int current = A[i];
31                int j = i - 1;
32                while (j >= begin && current < A[j]) {
33                    A[j + 1] = A[j];
34                    --j;
35                }
36                A[j + 1] = current;
37            }
38        } else {
39            int pivot = A[end - 1];
40            int i = begin;
41            for (int j = i; j < end; ++j) {
42                if (A[j] < pivot) {
43                    int temp = A[i];
44                    A[i] = A[j];
45                    A[j] = temp;
46                    ++i;
47                }
48            }
49            A[end - 1] = A[i];
50            A[i] = pivot;
51            ForkJoinTask.invokeAll(new PSort(A, begin, i),
52                                   new PSort(A, i, end));
53        }
54    }
55}