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(int[] 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 ints.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating an unsorted int array int Arr[] = {10, 2, -3, 35, 56, 100}; //printing unsorted array System.out.print("Before sorting\nArr contains:"); for(int 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(int i: Arr) System.out.print(" " + i); } }
The output of the above code will be:
Before sorting Arr contains: 10 2 -3 35 56 100 After sorting (specified range) Arr contains: 10 2 -3 35 56 100
❮ Java.util - Arrays