The current sorting implementations provided by the Java Collection Framework, i.e. Collections.sort and Arrays.sort sequentially performs the sorting. In this tutorial, we will see how to sort an array in parallel.
How to Sort an Array in Parallel in Java 8:
Java 7 has been introduced fork/join parallelism framework for lightweight data processing. By taking advantage of this framework, parallel array sorting has been introduced by providing additional utility methods to the Arrays class. These methods can be useful to sort an array in a parallel manner.
Sort an Array in Parallel:
We can sort arrays in parallel for all primitive types expect boolean, comparable object types and arbitrary object types. This implementation has been taken from Lea’s ParallelArray framework.
import java.util.Arrays;
public class ParallelArraySorting {
public static void main(String[] args) {
int[] int_array = {10,5,15,-10,-50,20,05,01,89,100,01};
System.out.println("Before Sort : ");
printArray(int_array);
System.out.println("\nAfter Sort : ");
Arrays.parallelSort(int_array);
printArray(int_array);
}
public static void printArray(int[] int_array){
for(int val : int_array){
System.out.print(val+" ");
}
}
}
Output:
Before Sort :
10 5 15 -10 -50 20 5 1 89 100 1
After Sort :
-50 -10 1 1 5 5 10 15 20 89 100
Sort an Array in Parallel in the specific range:
We can also sort an array in parallel in a specific range. That is, in the following example, we are going to sort an array parallelly by providing the start and end indexes.
Note: The start index is inclusive, and the end index is exclusive.
import java.util.Arrays;
public class ParallelArraySorting {
public static void main(String[] args) {
int[] int_array = {10,5,15,-10,-50,20,05,01,89,100,01};
System.out.println("Before Sort : ");
printArray(int_array);
System.out.println("\nAfter Sort : ");
Arrays.parallelSort(int_array,0,5);
printArray(int_array);
}
public static void printArray(int[] int_array){
for(int val : int_array){
System.out.print(val+" ");
}
}
}
Output:
Before Sort :
10 5 15 -10 -50 20 5 1 89 100 1
After Sort :
-50 -10 5 10 15 20 5 1 89 100 1
Array Sort Parallel in Java 8 – Performace:
As per the Java Docs – A minimum speedup of at least 1.3 over sequential sort on a two-core system, for a suitably sized data set.
I am going to test the Array Sort performance on my machine with the below configuration.
- Windows 10 Pro
- RAM 8 GB
- Intel Core i3 CPU @ 1.90GHz
In the following example, I am going to generate random integers with the size of 10_00_000 and applying Arrays.sort() and Arrays.parallelSort().
import java.util.Arrays;
import java.util.Random;
public class ParallelArrayPerformance {
public static void main(String[] args) {
performance();
}
private static void performance() {
Random random = new Random();
int[] elements = randomInts(random, 10_00_000);
// Arrays.Sort()
long start = System.currentTimeMillis();
Arrays.sort(elements);
System.out.println("For "+elements.length +" Ints - Arrays.sort took "+ (System.currentTimeMillis() - start) +" mills to Sort");
// Arrays.parallelSort()
long parallel_start = System.currentTimeMillis();
Arrays.parallelSort(elements);
System.out.println("For "+elements.length +" Ints - Arrays.parallelSort took "+ (System.currentTimeMillis() - parallel_start) +" mills to Sort");
}
private static int[] randomInts(Random random, int size) {
return random.ints(size, 1, size).toArray();
}
}
Output:
For 1000000 Ints - Arrays.sort took 407 mills to Sort
For 1000000 Ints - Arrays.parallelSort took 57 mills to Sort
References:
Happy Learning 🙂