Sorting is a fundamental operation in computer science
Start with an unordered array
[5, 7, 2, 3, 5, 2, 8, 1, 1, 5]
and transform it into a sorted array
[1, 1, 2, 2, 3, 5, 5, 5, 7, 8]
Same elements in increasing order.
5 7 2 3
j = 1, 2, ...
j
th element in sorted order by pairwise swaps5 7 2 3
5 7 2 3
7 5 3 2
5 4 7 9 2 8 3
Sequential:
Parallel:
Why is parallelization potentially problematic?
Creating more tasks is helpful?
Efficient, as long as no idle processes
Recall Fork-Join pools:
Creating FJ pool:
import java.util.concurrent.ForkJoinPool;
...
ForkJoinPool pool = new ForkJoinPool(POOL_SIZE);
...
pool.invoke(new SomeTask(...));
Tasks for fork-join pools (without return values)
RecursiveAction
compute()
methodclass MyTask extends RecursiveAction {
...
@Override
protected void compute () {
//... compute stuff ...//
MyTask sub1 = new MyTask(...) // create a sub-task
sub1.fork(); // start subtask
MyTask sub2 = new MyTask(...) // create another sub-task
sub2.fork(); // start other subtask
sub1.join(); // wait for sub1 to complete
sub2.join(); // wait for sub2 to complete
//... compute more stuff stuff ...//
}
}
Basic task: Sort array between index i
and j
Your task:
double
s as quickly as possible
Arrays.sort()
ForkJoinPool
Arrays.sort()
as a sub-routinefor (int i = 1; i < data.length; ++i) {
for (int j = i; j > 0; --j) {
if (data[j-1] > data[j]) {
swap(data, j-1, j)
}
}
}
if (data[i] > data[j]) {
swap(data, i, j)
}
for (int i = 1; i < data.length; ++i) {
for (int j = i; j > 0; --j) {
if (data[j-1] > data[j]) {
swap(data, j-1, j)
}
}
}
depth = max # of comparators on any path from input to output
for (int m = data.length - 1; m > 0; --m) {
for (int i = 0; i < m; ++i) {
if (data[i] > data[i+1]) {
swap(data, i, i+1)
}
}
}
When fuly parallelized, both are same sorting network
What is known:
What is not known: