Last Updated : 11 Jul, 2025
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:
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));
}
}
Sorted 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)
array
: Array to be sorted in ascending order. It may be an array of Object
or any primitive type.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
).
Arrays.parallelSort()
Working in Java
To understand this concept in more detail, let's go through the below diagram.
Arrays.parallelSort()
divides an array of {5, 4, 3, 2, 6, 1} into smaller sub-arrays. 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 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 + " "));
}
}
5 3 1 4 2 1 2 3 4 5Using 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");
}
}
}
For 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4