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:

Download  Run Code

 
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:

Download  Run Code

 
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, whereas Arrays.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, whereas Arrays.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, whereas Arrays.parallelSort() works faster than Arrays.sort() for large data sets but it may be slower than Arrays.sort() for smaller data sets.
  • The Arrays.sort() uses only one core of the system and does not utilize the parallelism of the machine, whereas Arrays.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.