Java Utility Library

Java Arrays - parallelSort() Method



The java.util.Arrays.parallelSort() method is used to sort the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

The sorting algorithm is a parallel sort-merge 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.

Syntax

public static void parallelSort(float[] a, int fromIndex, int toIndex)

Parameters

a Specify the array to be sorted.
fromIndex Specify the index of the first element, inclusive, to be sorted.
toIndex Specify the index of the last element, exclusive, to be sorted.

Return Value

void type.

Exception

  • Throws IllegalArgumentException, if fromIndex > toIndex.
  • Throws ArrayIndexOutOfBoundsException, if fromIndex < 0 or toIndex > a.length

Example:

In the example below, the java.util.Arrays.parallelSort() method is used to sort a specified range of given array of floats.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an unsorted float array
    float Arr[] = {10f, 2f, -3f, 35f, 56f, 100f};

    //printing unsorted array
    System.out.print("Before sorting\nArr contains:"); 
    for(float i: Arr)
      System.out.print(" " + i);

    //sort the specified range of the array
    Arrays.parallelSort(Arr, 2, 6);

    //printing array after sorting
    System.out.print("\n\nAfter sorting (specified range)\nArr contains:"); 
    for(float i: Arr)
      System.out.print(" " + i);   
  }
}

The output of the above code will be:

Before sorting
Arr contains: 10.0 2.0 -3.0 35.0 56.0 100.0

After sorting (specified range)
Arr contains: 10.0 2.0 -3.0 35.0 56.0 100.0

❮ Java.util - Arrays