Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java 8 Parallel Array Sorting

In Java 8, a new additional feature 'Parallel Array Sorting' is added into the Array class. Parallel sorting is an approach that uses multithreading, multiprocessing to perform the operations. There are mainly two approaches: sequential and parallel. In early Java versions, the Array class used a sequential approach to sort the array elements. But now a parallel approach is added that gives a performance boost to the Array class.

New methods have added to java.util.Arrays class that uses the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.

These methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects. It means, we can parallelly sort array of type: int, byte, short, char, double, float, etc. For example, to sort integer array, we can use parallelSort(int[] a) and for double array, parallelSort(double[] a) method.

Java Array New parallelSort() Methods

The following are the methods added to the Java Array class in Java 8 Version.

Methods

Description

public static void parallelSort(byte[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(byte[] a, int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(char[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(char[] a, int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(double[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(double[] a, int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(float[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(float[] a, int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(int[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(int[] a,int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(long[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(long[] a, int fromIndex, int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static void parallelSort(short[] a)

It sorts the specified array into ascending numerical order.

public static void parallelSort(short[] a,int fromIndex,int toIndex)

It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

public static <T extends Comparable<? super T>> void parallelSort(T[] a)

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

public static <T7gt; void parallelSort(T[] a,Comparator<? super T> cmp)

It sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

public static <T extends Comparable<? super T>> void parallelSort(T[] a,int fromIndex, int toIndex)

It sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)

It sorts the specified range of the specified array of objects according to the order induced by the specified comparator. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range).

Time for an Example:

Let's take an example to sort an array parallelly. Here, we are using parallelSort() method of Array class and it returns a sorted array. See the example below.

import java.util.Arrays;

public class STDemo {
	
	public static void main(String[] args) {
		int arr[] = {10,50,20,48,5};
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
		System.out.println("\nAfter sorting...");
		Arrays.parallelSort(arr);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
		
	}
}


10 50 20 48 5
After sorting...
5 10 20 48 50

Time for another Example:

Let's take another example to understand the parallelSort() method. Here, we are using one of its overloaded method that is used to sort a sub-array. We just need to specify the start and end index of the array and then the method will sort that accordingly. See the example below.

import java.util.Arrays;

public class STDemo {
	
	public static void main(String[] args) {
		int arr[] = {10,50,20,48,5};
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
		System.out.println("\nAfter sorting...");
		Arrays.parallelSort(arr,0,3);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+" ");
		}
		
	}
}


10 50 20 48 5
After sorting...
10 20 50 48 5