Signup/Sign In

Java Arrays parallelSort() Method

In this tutorial, we will learn about parallelSort() method of the Arrays class in Java. This sorting technique is very fast because it works parallelly to sort elements of the given array. This sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. This algorithm offers O(n log(n)) performance on all data sets and is typically faster than traditional (one-pivot) Quicksort implementations.

Syntax

parallelSort(datatype[] array)

This method accepts an array as an argument and returns it after sorting.

List of Overloading Methods of parallelSort() Method

This table contains all the overloaded variants of parallelSort() method.

Method Description

static void parallelSort(byte[] a)

This method sorts the specified array into ascending numerical order.

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

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(char[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(char[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(double[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(double[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(float[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(float[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(int[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(int[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(long[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(long[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

static void parallelSort(short[] a)

This method sorts the specified array into ascending numerical order.

static void parallelSort(short[] a,

int fromIndex, int toIndex)

This method sorts the specified range of the array into ascending numerical order.

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

This method sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

static <T extends Comparable<? super T>> void

parallelSort?(T[] a, int fromIndex, int toIndex)

This method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.

static <T> void parallelSort(T[] a, int fromIndex,

int toIndex, Comparator<? super T> cmp)

This method sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

static <T> void parallelSort(T[] a,

Comparator<? super T> cmp)

This method sorts the specified array of objects according to the order induced by the specified comparator.

enlightenedHow parallelSort() works ?

This method works in the following steps

  1. Divide array recursively until reaches to single element.
  2. All the individual elements are sorted on multiple threads.
  3. All the subarray are merged to get a sorted array.

Example: Sort Array using parallelSort() method

In the following example, firstly we have an unsorted array of integers. After applying parallelSort() method all the elements are sorted and that can be verified after printing each element from an array.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {1, 46, 165, 6, 78, 6, 65, 955, 4, 5, 323, 256, 5, 99, 22, 33};
		//sorting an array
		Arrays.parallelSort(arr);        
		for(int num:arr)
		{
			System.out.print(num+" ");
		}
	} 
}


1 4 5 5 6 6 22 33 46 65 78 99 165 256 323 955

Example: Sort Array using parallelSort() method with a Specific range

In the following program, we can see that given unsorted array arr is sorted till the first eight elements only this is because we passed a startIndex and endIndex in the method, and this an overloading method of parallelSort().

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = {5, 7, 1, 9, 8, 11, 2, 36, 1, 22, 34, 18, 17, 22, 13 };
		//sorting first eight array elements only
		Arrays.parallelSort(arr, 0, 7);        
		for(int num:arr)
		{
			System.out.print(num+" ");
		}
	} 
}


1 2 5 7 8 9 11 36 1 22 34 18 17 22 13

Example: Sort Array using parallelSort() with variasion

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		float arr[] = {5, 7, 1, 9, 8, 11, 2, 36, 1, 22, 34, 18, 17, 22, 13 };
		System.out.println("Array Before Sorting:");
		for(float num:arr)
		{
			System.out.print(num+" ");
		}
		System.out.println("\nArray Sorting from 0 to 6:");
		Arrays.parallelSort(arr, 0, 7);        
		for(float num:arr)
		{
			System.out.print(num+" ");
		}
		System.out.println("\nArray After Sorting as a Whole:");
		Arrays.parallelSort(arr);        
		for(float num:arr)
		{
			System.out.print(num+" ");
		} 
	} 
}


Array Before Sorting:
5.0 7.0 1.0 9.0 8.0 11.0 2.0 36.0 1.0 22.0 34.0 18.0 17.0 22.0 13.0
Array Sorting from 0 to 6:
1.0 2.0 5.0 7.0 8.0 9.0 11.0 36.0 1.0 22.0 34.0 18.0 17.0 22.0 13.0
Array After Sorting as a Whole:
1.0 1.0 2.0 5.0 7.0 8.0 9.0 11.0 13.0 17.0 18.0 22.0 22.0 34.0 36.0

Example of all overloading methods of parallelSort()

In the following program, we implemented all the overloading methods of parallelSort() together. We implemented this method on various data types and on various ranges. From the output of this program, we can easily correlate how sorting is performed on different arrays.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		byte byteArray[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; 
		Arrays.parallelSort(byteArray,0,5);
		System.out.println("Arrays.parallelSort(byteArray,0,5): "+Arrays.toString(byteArray));
		Arrays.parallelSort(byteArray);
		System.out.println("Arrays.parallelSort(byteArray): "+Arrays.toString(byteArray));		
		
		char charArray[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}; 
		Arrays.parallelSort(charArray,0,5);
		System.out.println("Arrays.parallelSort(charArray,0,5): "+Arrays.toString(charArray));
		Arrays.parallelSort(charArray);
		System.out.println("Arrays.parallelSort(charArray): "+Arrays.toString(charArray));
		
		int intArray[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
		Arrays.parallelSort(intArray,0,5);
		System.out.println("Arrays.parallelSort(intArray,0,5): "+Arrays.toString(intArray));
		Arrays.parallelSort(intArray);
		System.out.println("Arrays.parallelSort(intArray): "+Arrays.toString(intArray));
		
		double doubleArray[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2}; 
		Arrays.parallelSort(doubleArray,0,5);
		System.out.println("Arrays.parallelSort(doubleArray,0,5): "+Arrays.toString(doubleArray));
		Arrays.parallelSort(intArray);
		System.out.println("Arrays.parallelSort(doubleArray): "+Arrays.toString(doubleArray));
		
		float floatArray[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f}; 
		Arrays.parallelSort(floatArray,0,5);
		System.out.println("Arrays.parallelSort(floatArray,0,5): "+Arrays.toString(floatArray));
		Arrays.parallelSort(floatArray);
		System.out.println("Arrays.parallelSort(floatArray): "+Arrays.toString(floatArray));
		
		short shortArray[] = {2, 4, 6, 8, 10 ,12, 14, 16, 18, 20}; 
		Arrays.parallelSort(shortArray,0,5);
		System.out.println("Arrays.parallelSort(shortArray,0,5): "+Arrays.toString(shortArray));
		Arrays.parallelSort(shortArray);
		System.out.println("Arrays.parallelSort(shortArray): "+Arrays.toString(shortArray));
	}
}


Arrays.parallelSort(byteArray,0,5): [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
Arrays.parallelSort(byteArray): [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
Arrays.parallelSort(charArray,0,5): [a, b, c, d, e, f, g, h, i, j, k]
Arrays.parallelSort(charArray): [a, b, c, d, e, f, g, h, i, j, k]
Arrays.parallelSort(intArray,0,5): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
Arrays.parallelSort(intArray): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
Arrays.parallelSort(doubleArray,0,5): [5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2]
Arrays.parallelSort(doubleArray): [5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2]
Arrays.parallelSort(floatArray,0,5): [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Arrays.parallelSort(floatArray): [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Arrays.parallelSort(shortArray,0,5): [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Arrays.parallelSort(shortArray): [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Conclusion

In this tutorial, we learned how parallelSort() can be used to sort given unsorted array efficiently. This method uses Fork/Join concept to sort the array. This sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. This algorithm offers O(n log(n)) performance on all data sets and is typically faster than traditional (one-pivot) Quicksort implementations.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.