Open In App

Arrays.parallelSort() in Java with Examples

Last Updated : 25 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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));
    }
}

Output
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)

  • 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.

Working of Arrays.parallelSort()  in Java
  • 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 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 + " "));
    }
}

Output
5 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 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");
        }
    }
} 

Output
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.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg