Home » Java 8 Parallel Array Sorting

Java 8 Parallel Array Sorting

by Online Tutorials Library

Java Parallel Array Sorting

Java provides a new additional feature in Array class which is used to sort array elements parallel.New methods has added to java.util.Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.The methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects.

The following table contains Arrays overloaded sorting methods.

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).

Java Parallel Array Sorting Example

Output:

5 8 1 0 6 9  Array elements after sorting 0 1 5 6 8 9  

Java Parallel Array Sorting Example: Passing Start and End Index

In the following example, we are passing starting and end index of the array. The first index is inclusive and end index is exclusive i.e. if we pass 0 as start index and 4 as end index, only 0 to 3 index elements will be sorted.

It throws IllegalArgumentException if start index > end index.

It throws ArrayIndexOutOfBoundsException if start index < 0 or end index > a.length.

Output:

5 8 1 0 6 9 50 -3  Array elements after sorting 0 1 5 8 6 9 50 -3  

You may also like