Java 8 introduced a new method parallelSort() in the Arrays class of java.util package. This method is introduced to support the parallel sorting of array elements.
Algorithm of parallel sorting:
1. The given array is divided into the sub arrays and the sub arrays are further divided into the their sub arrays, this happens until the sub array reaches a minimum granularity.
2. The sub arrays are sorted individually by multiple threads. The parallel sort uses Fork/Join Framework for sorting sub arrays parallelly.
3. The sorted sub arrays are merged.
Advantage of Parallel Sort Over Simple Sort:
The parallelSort() method uses the concept of multithreading which makes it much faster compared to the normal sort when there are lot of elements.
Example 1: Sorting Primitive Data types with Parallel Sort
import java.util.Arrays; public class Example { public static void main(String[] args) { int numbers[] = {22, 89, 1, 32, 19, 5}; //Parallel Sort method for sorting int array Arrays.parallelSort(numbers); //converting the array to stream and displaying using forEach Arrays.stream(numbers).forEach(n->System.out.print(n+" ")); } }
Output:
1 5 19 22 32 89
References:
Java 8 – Parallel Sort JavaDoc
Example 2: Parallel Sort by specifying the start and end index
We can also specify the start and end for the sorting, in this case the sub array starting from the start index and ending at the end index is sorted, the rest of the array is ignored and doesn’t get sorted.
import java.util.Arrays; public class Example { public static void main(String[] args) { int numbers[] = {22, 89, 1, 32, 19, 5}; /* Specifying the start and end index. The start index is * 1 here and the end index is 5. which means the the elements * starting from index 1 till index 5 would be sorted. */ Arrays.parallelSort(numbers, 1, 5); //converting the array to stream and displaying using forEach Arrays.stream(numbers).forEach(n->System.out.print(n+" ")); } }
Output:
22 1 19 32 89 5
Leave a Reply