This project demonstrates the implementation of a parallel sorting algorithm using Java's multithreading capabilities. Specifically, it leverages the RecursiveTask class and the ForkJoinPool to efficiently sort large arrays in parallel. This implementation is particularly useful for improving performance on multi-core processors.
- Parallel Merge Sort: The algorithm uses the merge sort technique, which is well-suited for parallel execution.
- ForkJoin Framework: Utilizes Java's
ForkJoinPoolto manage and execute tasks. - Scalable Performance: Designed to take advantage of multiple CPU cores, resulting in faster sorting times for large datasets.
- Java 8 or higher
- Maven (for building the project)
-
Clone the repository:
git clone https://github.com/atakoutene/ParallelSorting cd parallel-sorting -
Build the project using Maven:
mvn clean install
Here is a quick example of how to use the parallel sorting algorithm in your code:
import java.util.concurrent.ForkJoinPool;
public class Main {
public static void main(String[] args) {
List<Integer> array = List.of(5, 3, 8, 4, 2, 7, 1, 6);
ForkJoinPool pool = new ForkJoinPool();
ParallelMergeSort task = new ParallelMergeSort(array);
pool.invoke(task);
List<Integer> sortedArray = task.join();
sortedArray.forEach(e -> {System.out::println});
}
}The ForkJoinPool is used to execute the sorting tasks in parallel. It automatically manages the thread pool and task scheduling.
ForkJoinPool pool = new ForkJoinPool();
ParallelMergeSort task = new ParallelMergeSort(array);
pool.invoke(task);
List<Integer> sortedArray = task.join();The parallel merge sort algorithm can significantly reduce the sorting time for large arrays by dividing the work across multiple CPU cores. However, the performance gain depends on the hardware and the size of the input array.
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or bug reports.
This project is licensed under the MIT License - see the LICENSE file for details.