Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.williamfiset.algorithms.parallel.algorithm;

import java.util.concurrent.ForkJoinPool;

public class Main{

public static void main(String[] args) {
int[] arr = {7, 6, 5, 4, 3, 2, 1};

System.out.println("available processors = " + Runtime.getRuntime().availableProcessors());

ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new ParallelSorting(arr, 0, arr.length-1));

for (int num : arr){
System.out.println(num);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.williamfiset.algorithms.parallel.algorithm;

import java.util.concurrent.RecursiveAction;

public class ParallelSorting extends RecursiveAction{

private final int[] arr;
private final int low;
private final int high;

public ParallelSorting(int[] arr, int low, int high) {
this.arr = arr;
this.low = low;
this.high = high;
}

@Override
protected void compute() {
if (low < high) {
int pivotIndex = partition(arr, low, high);
invokeAll(new ParallelSorting(arr, low, pivotIndex - 1), new ParallelSorting(arr, pivotIndex + 1, high));
}
}

private int partition(int[] arr, int low, int high) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lumoto's partition

int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}