Arrays.sort() and Arrays.parallelSort() method in Java
In this post, we will explain the difference between the Arrays.sort() and Arrays.parallelSort() methods in Java.
The Arrays.sort() and Arrays.parallelSort() methods are used to sort an array of objects or primitives in ascending order. However, they have different features, functionalities, and performance characteristics that make them suitable for different scenarios and use cases.
1. Overview of Arrays.sort() method
The Arrays.sort() method is a sequential sorting method that uses a single thread to sort an array of objects or primitives. The sorting algorithm used in this method is Dual-Pivot Quicksort, which is a custom implementation of the Quicksort algorithm to achieve better performance. This method has two variants:
- Arrays.sort(array) – sorts the full array into ascending order
- Arrays.sort(array, fromIndex, toIndex) – sorts only the elements from fromIndex to toIndex
Let’s see an example of both variants:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; class Main { public static void main(String[] args) { // Create an array of integers int[] numbers1 = {10, 4, 6, 2, 1, 9, 7, 8, 3, 5}; // Sort the full array Arrays.sort(numbers1); // Print the sorted array System.out.println(Arrays.toString(numbers1)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] int[] numbers2 = {10, 4, 6, 2, 1, 9, 7, 8, 3, 5}; // Sort only a part of the array Arrays.sort(numbers2, 2, 8); // Print the partially sorted array System.out.println(Arrays.toString(numbers2)); // [10, 4, 1, 2, 6, 7, 8, 9, 3, 5] } } |
The Arrays.sort() method works fast on smaller data sets but its performance degrades for large data sets. This is because it uses only one core of the system and does not utilize the parallelism of the machine.
2. Overview of Arrays.parallelSort() method
The Arrays.parallelSort() method is a parallel sorting method that uses multiple threads to sort an array of objects or primitives. The method uses a threshold value and any array of size lesser than the threshold value is sorted using the Arrays.sort() method (i.e sequential sorting). The threshold is calculated considering the parallelism of the machine and size of the array. This method also has two variants:
- Arrays.parallelSort(array) – sorts the full array into ascending order
- Arrays.parallelSort(array, fromIndex, toIndex) – sorts only the elements from fromIndex to toIndex
Let’s see an example of both variants:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; class Main { public static void main(String[] args) { // Create an array of integers int[] numbers = {10, 4, 6, 2, 1, 9, 7, 8, 3, 5}; // Sort the full array Arrays.parallelSort(numbers); // Print the sorted array System.out.println(Arrays.toString(numbers)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] int[] numbers2 = {10, 4, 6, 2, 1, 9, 7, 8, 3, 5}; // Sort only a part of the array Arrays.parallelSort(numbers2, 2, 8); // Print the partially sorted array System.out.println(Arrays.toString(numbers2)); // [10, 4, 1, 2, 6, 7, 8, 9, 3, 5] } } |
The Arrays.parallelSort() method works faster than Arrays.sort() method for large data sets as it uses multiple cores of the system and utilizes the parallelism of the machine. However, it also has some overhead for parallelization (splitting into chunks and merging) which may make it slower than Arrays.sort() method for smaller data sets.
3. Differences between Arrays.sort() and Arrays.parallelSort() method
Arrays.sort() and Arrays.parallelSort() are both useful methods to sort an array of objects or primitives in ascending order, but they have some differences and trade-offs that we must understand:
- The
Arrays.sort()is a sequential sorting method that uses a single thread to sort an array, whereasArrays.parallelSort()is a parallel sorting method that uses multiple threads to sort an array. - The
Arrays.sort()uses the Dual-Pivot Quicksort algorithm to sort an array, whereasArrays.parallelSort()uses a parallel sort-merge algorithm to sort an array. - The
Arrays.sort()works fast on smaller data sets but its performance degrades for large data sets, whereasArrays.parallelSort()works faster thanArrays.sort()for large data sets but it may be slower thanArrays.sort()for smaller data sets. - The
Arrays.sort()uses only one core of the system and does not utilize the parallelism of the machine, whereasArrays.parallelSort()uses multiple cores of the system and utilizes the parallelism of the machine.
4. What to use and when?
As we have seen, Arrays.sort() and Arrays.parallelSort() are both useful methods to sort an array of objects or primitives in ascending order. Here are some general recommendations on how to choose between them:
- If you need to sort a small or medium-sized array of objects or primitives, we should use
Arrays.sort(). It allows us to sort an array using a single thread and a custom implementation of the Quicksort algorithm. - If you need to sort a large-sized array of objects or primitives, we should use
Arrays.parallelSort(). It allows us to sort an array using multiple threads and a parallel sort-merge algorithm.
That’s all about the sort() and parallelSort() method of Arrays class in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)