Saturday, December 10, 2022

Parallel Array Sorting in Java - Arrays.parallelSort() Example

There are multiple ways to sort array in Java. For example, you can use Array.sort() method to sort any primitive or object array in Java but Java 8 presents a new feature that is useful to sort array’s elements. It can implemented by using the package “java.util.Arrays” which represents several methods to sort the array. The biggest benefit is that parallel array sorting is that its faster than any other sorting method due to usage of multithreading conception. So multiple threads can break the array into parts and work simultaneously to sort it. 

The method used to sort the array is called Arrays.parallelSort(array). Similar to Arrays.sort() this method is also overloaded to work with different kind of input like byte[], int[], char[], short[], long[], float[], double[], and T[]

As per Java doc, Arrays.parallelSort() method uses aa parallel sort-merge sorting algorithm that breaks the array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. 

If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort() method. The algorithm requires a working space no greater than the size of the original array. The ForkJoin common pool is used to execute any parallel tasks as shown in following diagram






Java Parallel Array Sorting Example

Let’s see an example of Java Parallel Array Sorting:
public static void main(String[] args) {
   
int[] array = { 3, 6, 11, 4, 16, 1, 13, 8, 14};

   
System.out.println("Array before sorting :");
    for
(int i = 0; i < array.length; i++) {
        System.
out.print(array[i] + "  ");
   
}
    Arrays.sort(array)
;
   
System.out.println("\n\n" + "Array after sorting :");
    for
(int i = 0; i < array.length; i++) {
        System.
out.print(array[i] + "  ");
   
}

}


The output will be:

 Array before sorting :
  
3  6  11  4  16  1  13  8  14 

 
Array after sorting :
 
1  3  4  6  8  11  13  14  16

 


Moreover, we can precise in the array from which index to which index to be sorted: Arrays.sort (array, int start, int end)

public static void main(String[] args) {

   
int[] array = {3, 6, 11, 4, 16, 1, 13, 8, 14};
   
System.out.println("Array before sorting :");
    for
(int i = 0; i < array.length; i++) {
        System.
out.print(array[i] + "  ");
   
}

    Arrays.parallelSort(array
, 0, 5);

   
System.out.println("\n\n" + "Array after sorting :");
    for
(int i = 0; i < array.length; i++) {
        System.
out.print(array[i] + "  ");
   
}

}

 

The output will be:

 Array before sorting :
 
3  6  11  4  16  1  13  8  14

 
Array after sorting :
 
3  4  6  11  16  1  13  8  14


As we see the array is been sorted until index 5. While I didn't compare the performance of Arrays.sort() and Arrays.parallelSort() as it doesn't make any difference if array is small but for large array, it will make difference and you should use paralleSort() method to sort a large array in Java. 


That's all about how to do parallel array sorting in Java. It's a nice feature which is really very useful given most of the application needs to sort the array. Now it will be faster, especially if you have large array with millions of values. 

Other Array related articles and tutorials you may like
  • How to find all pairs on integer array whose sum is equal to given number? [solution]
  • How to sort an array in place using QuickSort algorithm? [solution]
  • Write a program to find missing number in integer array of 1 to 100? [solution]
  • How do you remove duplicates from array in place? [solution]
  • How do you reverse array in place in Java? [solution]
  • 30 Array Interview Questions with answers (array problems)
  • How to check if array contains a number in Java? [solution]
  • Difference between array and list in Java? (array vs list)
  • Write a program to find top two numbers from an integer array? [solution]
  • How to merge two sorted array in Java? (merge array)
  • How to find maximum and minimum number in unsorted array? [solution]
  • How to remove duplicates from an array in Java? [solution]

Thanks for reading this article so far. If you like this Java parallel array sorting tutorial then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you, Btw what is your favorite sorting algorithm quick sort, merge sort or selection sort?

P. S. - If you are looking for free Algorithms courses to improve your understanding of Data Structure and Algorithms in Java, then can also checkout these best free Data Structures courses where I have shared free resources from sites like Udemy, Coursera, YouTube and others. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.