Arrays.parallelSort() in Java with Examples
Last Updated :
25 Nov, 2024
The Arrays.parallelSort() method of Arrays
class in Java is used to sort arrays in parallel order. It divides an array into sub-arrays then sorts them using multiple threads, and merges them for a complete sorted result. This method uses the Fork/Join framework for efficient parallel processing.
Example:
Below is a simple example that uses Arrays.parallelSort()
to sort an integer array:
Java
import java.util.Arrays;
public class ArraysParallelSort {
public static void main(String[] args) {
int[] n = {5, 3, 1, 4, 2};
// use Arrays.parallelSort()
// to sort the array
Arrays.parallelSort(n);
System.out.println("Sorted Array: " + Arrays.toString(n));
}
}
OutputSorted Array: [1, 2, 3, 4, 5]
Syntax of Arrays.parallelSort() method
1. To Sort Entire Array in Ascending Order
public static void parallelSort(Object[] array)
- Parameter:
array
: Array to be sorted in ascending order. It may be an array of Object
or any primitive type. - Return Type: This method has no return value (
void
).
2. To Sort a Specified Range in Ascending Order
public static void parallelSort(Object[] array, int fromIndex, int toIndex)
Parameters:
fromIndex
: Starting index of the range to be sorted (inclusive).toIndex
: End index of the range to be sorted (exclusive).
Return Type: This method also returns no value (void
).
Diagrammatic Representation of Arrays.parallelSort()
Working in Java
To understand this concept in more detail, let's go through the below diagram.

- This diagram illustrates how
Arrays.parallelSort()
divides an array of {5, 4, 3, 2, 6, 1} into smaller sub-arrays. - Then sorts them in parallel using multiple threads.
- And lastly merges them to produce a sorted array.
Examples of Arrays.parallelSort() in Java
Using Arrays.parallelSort()
to Sort an Array in Ascending Order
In this example, we use Arrays.parallelSort()
to sort an integer array in ascending order by using parallel processing to speed up the element sorting process.
Java
// Java program to demonstrate
// Arrays.parallelSort() method
import java.util.Arrays;
public class ArraysParallelSort {
public static void main(String[] args) {
// create an array
int n[] = { 5, 3, 1, 4, 2, };
// print unsorted Array
System.out.print("");
// Iterating the Elements using stream
Arrays.stream(n)
.forEach(n1 -> System.out.print(n1 + " "));
System.out.println();
// using Arrays.parallelSort()
Arrays.parallelSort(n);
// Printing sorted Array
System.out.print("");
// Iterating the Elements using stream
Arrays.stream(n)
.forEach(n1 -> System.out.print(n1 + " "));
}
}
Output5 3 1 4 2
1 2 3 4 5
Using Arrays.parallelSort() and Arrays.sort() to Sort an Array
In this example, we use Arrays.parallelSort() and Arrays.sort() to compare the performance of parallel sorting and serial sorting on large integer array using multiple threads.
Java
// Java program to compare the performance of
// parallel sorting and normal sorting
// on an integer array
import java.util.Arrays;
import java.util.Random;
public class ArraysParallelSort {
public static void main(String[] args) {
int[] n = new int[100];
// Perform tests for multiple iterations
for (int i = 0; i < 1000; i += 10) {
System.out.println("\nFor iteration number: " + (i / 10 + 1));
// Generate random integers for the array
Random r = new Random();
for (int j = 0; j < 100; j++) {
n[j] = r.nextInt();
}
// Serial sort using Arrays.sort()
long s = System.nanoTime();
Arrays.sort(n);
long e = System.nanoTime();
System.out.println("Serial Sort Time: " + (e - s) + " ns");
// Parallel sort using Arrays.parallelSort()
s = System.nanoTime();
Arrays.parallelSort(n);
e = System.nanoTime();
System.out.println("Parallel Sort Time: " + (e - s) + " ns");
}
}
}
OutputFor iteration number: 1
Serial Sort Time: 311945 ns
Parallel Sort Time: 45702 ns
For iteration number: 2
Serial Sort Time: 58249 ns
Parallel Sort Time: 11921 ns
For iteration number: 3
Serial Sort ...
Note: Different time intervals will be printed, but parallel sort will be done before normal sort.
Similar Reads
Collections.sort() in Java with Examples java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better than as it can sort the elements of Array as well as link
5 min read
Arrays.binarySearch() in Java with Examples | Set 1 In Java, the Arrays.binarySearch() method searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted by the Arrays.sort() method before making this call. If it is not sorted, the results are undefined. Example:Below is a si
3 min read
SortedMap putAll() method in Java with Examples The putAll() method of SortedMap interface in Java is used to copy all of the mappings from the specified SortedMap to this SortedMap. Syntax: void putAll(Map m) Parameters: This method has the only argument map m which contains key-value mappings to be copied to given SortedMap. Returns: This metho
2 min read
Java Arrays compare() Method with Examples The Arrays compare() method in Java is a part of the java.util package to compare arrays lexicographically (dictionary order). This method is useful for ordering arrays and different overloads for different types including boolean, byte, char, double, float, int, long, short, and Object arrays. Exam
3 min read
SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th
2 min read
Comparator naturalOrder() method in Java with examples The naturalOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T>> Com
1 min read