Collections

Java Arrays parallelSort() Code With Example

Table of Contents

Program

import java.util.Arrays;
public class ParralelSort
{
	public static void main(String[] args)
	{
	   	int[] i1 = {18, 1, 14, 2, 15, 12, 5, 4};
  		System.out.println("Before Parallel Sort int :"+Arrays.toString(i1));
       	        Arrays.parallelSort(i1);
      		System.out.println("After Parallel Sort int :"+Arrays.toString(i1));
       		Arrays.parallelSort(i1,1,4);
       		System.out.println("parallel sort int with index :" +Arrays.toString(i1));
       		long [] l = new long [] {122l,320l,127l,222l};
       		System.out.println("Before Parallel Sort long :"+Arrays.toString(l));
       		Arrays.parallelSort(l);
      		System.out.println("After Parallel Sort long :"+Arrays.toString(l));
      		Arrays.parallelSort(l,1,4);
      		System.out.println("parallel sort long with index :" +Arrays.toString(l));
      		short [] s = new short [] {1220,1000,400,2000};
     	 	System.out.println("Before Parallel Sort short :"+Arrays.toString(s));
  		Arrays.parallelSort(s);
     		System.out.println("After Parallel Sort short :"+Arrays.toString(s));
     		Arrays.parallelSort(s,1,4);
     		System.out.println("parallel sort short with index :" +Arrays.toString(s));
     		int[] intArray = new int[7];
     		System.out.println("original int array : "+ Arrays.toString(intArray));
     		Arrays.setAll(intArray, i -> (i + 1) * 2);
     		System.out.println("array.setAll(int) output: "+ Arrays.toString(intArray));
     		long[] longArray = new long[7];
     		System.out.println("original long array with default values : "+ Arrays.toString(longArray));
		Arrays.setAll(longArray, i -> i + 10 * 20);
     		System.out.println("Long array.setAll(long) output: "+ Arrays.toString(longArray));
 	}
}

Output

      After Parallel Sort long :[122, 127, 222, 320]
	parallel sort long with index :[122, 127, 222, 320]
	Before Parallel Sort short :[1220, 1000, 400, 2000]
	After Parallel Sort short :[400, 1000, 1220, 2000]
	parallel sort short with index :[400, 1000, 1220, 2000]
	original int array : [0, 0, 0, 0, 0, 0, 0]
	array.setAll(int) output: [2, 4, 6, 8, 10, 12, 14]
	original long array with default values : [0, 0, 0, 0, 0, 0, 0]
	Long array.setAll(long) output: [200, 201, 202, 203, 204, 205, 206]

Description

public static void parallelSort​(int[] a)

Sorts the specified array into ascending numerical order.

Implementation Note:

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,
working space no greater than the size of the original array.
The ForkJoin common pool is used to execute any parallel tasks.

Parameters:

a – the array to be sorted

Since:

1.8

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

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

Implementation Note:

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.
The algorithm requires a working space no greater than the size of the specified range of the original array.
The ForkJoin common pool is used to execute any parallel tasks.

Parameters:

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

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException -if fromIndex < 0 or toIndex > a.length

Since:

1.8

public static void parallelSort​(long[] a)

Sorts the specified array into ascending numerical order.

Implementation Note:

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

Parameters:

a – the array to be sorted

Since:

1.8

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

Sorts 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

Implementation Note:

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.
The algorithm requires a working space no greater than the size of the specified range of the original array.
The ForkJoin common pool is used to execute any parallel tasks.

Parameters:

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

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException -if fromIndex < 0 or toIndex > a.length

Since:

1.8

public static void parallelSort​(short[] a)

Sorts the specified array into ascending numerical order.

Implementation Note:

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

Parameters:

a – the array to be sorted

Since:

1.8

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

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

Implementation Note:

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. The algorithm requires a working
space no greater than the size of the specified range of the original array.
The ForkJoin common pool is used to execute any parallel tasks.

Parameters:

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

Throws:

IllegalArgumentException – if fromIndex > toIndex
ArrayIndexOutOfBoundsException -if fromIndex < 0 or toIndex > a.length

Since:

1.8

public static void setAll​(int[] array, IntUnaryOperator generator)

Set all elements of the specified array, using the provided generator function to compute each element.
If the generator function throws an exception, it is relayed to the caller and the array is left in an
indeterminate state.

API Note:
Setting a subrange of an array, using a generator function to compute each element, 
		  can be written as follows:

 	IntStream.range(startInclusive, endExclusive)
          .forEach(i -> array[i] = generator.applyAsInt(i));

Parameters:

array – array to be initialized
generator – a function accepting an index and producing the desired value for that position

Throws:

NullPointerException – if the generator is null

Since:

1.8

public static void setAll​(long[] array, IntToLongFunction generator)

Set all elements of the specified array, using the provided generator function to compute each element.
If the generator function throws an exception, it is relayed to the caller and the array is left in an
indeterminate state.

API Note:Setting a subrange of an array, using a generator function to compute each element, 
      can be written as follows:

 IntStream.range(startInclusive, endExclusive)
      .forEach(i -> array[i] = generator.applyAsLong(i));

Parameters:

array – array to be initialized
generator – a function accepting an index and producing the desired value for that position

Throws:

NullPointerException – if the generator is null

Since:

1.8