From 61764c349f4f644c18954682fad06a0a12d7b6b3 Mon Sep 17 00:00:00 2001 From: "omar.mahamid" Date: Mon, 16 Jan 2023 18:41:19 +0200 Subject: [PATCH 1/2] S-12345-add-new-parallel-module --- .../com/williamfiset/algorithms/parallel/algorithm/Main.java | 2 ++ .../algorithms/parallel/algorithm/ParallelSorting.java | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java create mode 100644 src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java diff --git a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java new file mode 100644 index 000000000..f31d67de4 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java @@ -0,0 +1,2 @@ +package com.williamfiset.algorithms.parallel.algorithm;public class Main { +} diff --git a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java new file mode 100644 index 000000000..709202213 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java @@ -0,0 +1,2 @@ +package com.williamfiset.algorithms.parallel.algorithm;public class ParallelSorting { +} From 9bcaef93ee1225d5e6916b46dc32e798cfd29d11 Mon Sep 17 00:00:00 2001 From: "omar.mahamid" Date: Mon, 16 Jan 2023 18:41:54 +0200 Subject: [PATCH 2/2] S-12345-add-new-parallel-module --- .../algorithms/parallel/algorithm/Main.java | 19 ++++++++- .../parallel/algorithm/ParallelSorting.java | 41 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java index f31d67de4..8f8f5cfc1 100644 --- a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java +++ b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/Main.java @@ -1,2 +1,19 @@ -package com.williamfiset.algorithms.parallel.algorithm;public class Main { +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); + } + } } diff --git a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java index 709202213..c0b9e1e6b 100644 --- a/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java +++ b/src/main/java/com/williamfiset/algorithms/parallel/algorithm/ParallelSorting.java @@ -1,2 +1,41 @@ -package com.williamfiset.algorithms.parallel.algorithm;public class ParallelSorting { +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) { + 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; + } }