Javaexercise.com

Java 8 Array Parallel Sort

Sorting is a process of arranging elements into an order either ascending (natural order) or descending. To sort an array elements, Java provides methods which are available in java.util.Arrays class.

To sort an Array elements, Java uses two approach sequential and concurrent. Java 7 and earlier versions used sequential approach to sort array elements. Sequential sorting uses single thread to sort the array and provides same performance in both single and multiprocessor systems. 

In Java 8, Parallel Array Sorting feature is added to the Arrays class which was based on concurrent approach. It uses a parallel sort-merge sorting algorithm to sort the elements. It breaks the array into sub-arrays, sort them and then merged. It provides the same set of sorting operations provided by the Arrays class, but with a parallel implementation that utilizes the Fork/Join framework.

The new added methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects.

Advantage of Java 8 Parallel Sort

  • Parallel sorting of large arrays on multiprocessor systems is faster than sequential array sorting.

  • ParallelSort() uses multiple threads to sort data.

 

Example: How to Sort an Array Parallelly

In this example, we are sorting an interger array using parallelSort() method of Arrays class. You must import the java.util.Arrays class before using its methods. 

import java.util.Arrays;

class Demo{
	public static void main(String[] args) {
		int[] arr = {25,3,6,21,4,4,7,5};
		System.out.println("Array...");
        for(int i:arr) {
        	System.out.print(i +" ");
        }
        System.out.println("\nAfter Sorting...");
        // Parallel array sorting 
        Arrays.parallelSort(arr);
        for(int i:arr) {
        	System.out.print(i +" ");
        }
	}
}

Output:

Array...
25 3 6 21 4 4 7 5 
After Sorting...
3 4 4 5 6 7 21 25 

 

Java 8 Arrays class parallelSort() Methods

This table contains methods of Arrays class. These are overloaded methods of parallerSort() method, we can use them according the type of array.

Method

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.

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.

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.

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.

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.

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.

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.

 

Example: How to Sort Sub Array or Partial Array

We can sort partial array by specifying the start and end index to the parallelSort() method. It is helpful when we want to sort some part of the array not the entire array.

import java.util.Arrays;

class Demo{
	public static void main(String[] args) {
		int[] arr = {25,3,6,21,4,4,7,5};
		System.out.println("Array...");
        for(int i:arr) {
        	System.out.print(i +" ");
        }
        System.out.println("\nAfter Sorting...");
        // Parallel sub-array sorting 
        Arrays.parallelSort(arr,0,4); // Passing start and end index
        for(int i:arr) {
        	System.out.print(i +" ");
        }
	}
}

Output:

Array...
25 3 6 21 4 4 7 5 
After Sorting...
3 6 21 25 4 4 7 5 

 

Example: IllegalArgumentException while Sorting

The parallelSort() method throws an exception if the start index is greater than the end index. See the below example.

import java.util.Arrays;

class Demo{
	public static void main(String[] args) {
		int[] arr = {25,3,6,21,4,4,7,5};
		System.out.println("Array...");
        for(int i:arr) {
        	System.out.print(i +" ");
        }
        System.out.println("\nAfter Sorting...");
        // Parallel sub-array sorting 
        Arrays.parallelSort(arr,4,0); // Error: startindex > endindex
        for(int i:arr) {
        	System.out.print(i +" ");
        }
	}
}

 

Output:

Array...
25 3 6 21 4 4 7 5 
After Sorting...
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(4) > toIndex(0)

 

Example: Char Array

Since Arrays class provides various overloade methods to support all the possible array type then we can sort an array of char elements too. See the below example.

import java.util.Arrays;

class Demo{
	public static void main(String[] args) {
		char[] arr = {'s','w','a','f','z','b'};
		System.out.println("Array...");
        for(char i:arr) {
        	System.out.print(i +" ");
        }
        System.out.println("\nAfter Sorting...");
        // Parallel array sorting 
        Arrays.parallelSort(arr);
        for(char i:arr) {
        	System.out.print(i +" ");
        }
	}
}

Output:

Array...
s w a f z b 
After Sorting...
a b f s w z 

 

Example: Sorting Non Parallel Array

If we don't want to use parallel sorting, in that case we can use sort() method of Arrays class that uses single thread to sort the array. See the below example

import java.util.Arrays;

class Demo{
	public static void main(String[] args) {
		int[] arr = {25,3,6,21,4,4,7,5};
		System.out.println("Array...");
        for(int i:arr) {
        	System.out.print(i +" ");
        }
        System.out.println("\nAfter Sorting...");
        // Non parallel array sorting 
        Arrays.sort(arr);
        for(int i:arr) {
        	System.out.print(i +" ");
        }
	}
}

Output:

Array...
25 3 6 21 4 4 7 5 
After Sorting...
3 4 4 5 6 7 21 25 


Conclusion

Well, in this topic, we learnt To sort array elements using Arrays class methods. We used parallelSort() method for parallel sorting and sort() for non-parallel sorting.

If we missed something, you can suggest us at - info.javaexercise.com