Java Collections - Arrays.parallelSort() Examples

Code Snippets Java Collections Java 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods sort the specified array into natural order or in the order provided by the specified comparator.

public static void parallelSort(byte[] a)
public static void parallelSort(byte[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(char[] a)
public static void parallelSort(char[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(short[] a)
public static void parallelSort(short[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(int[] a)
public static void parallelSort(int[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(long[] a)
public static void parallelSort(long[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(float[] a)
public static void parallelSort(float[] a,
                                int fromIndex,
                                int toIndex)
public static void parallelSort(double[] a)
public static void parallelSort(double[] a,
                                int fromIndex,
                                int toIndex)
public static <T extends Comparable<? super T>> void parallelSort(T[] a)
public static <T extends Comparable<? super T>> void parallelSort(T[] a,
                                                                  int fromIndex,
                                                                  int toIndex)
public static <T> void parallelSort(T[] a,
                                    Comparator<? super T> cmp)
public static <T> void parallelSort(T[] a,
                                    int fromIndex,
                                    int toIndex,
                                    Comparator<? super T> cmp)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelSortExample {

public static void main(String... args) {
int[] arr = {8, 9, 4, 3, 6, 7, 0, 8, 7, 5, 1};
Arrays.parallelSort(arr);
System.out.println(Arrays.toString(arr));
}
}

Output

[0, 1, 3, 4, 5, 6, 7, 7, 8, 8, 9]




Index based:

package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelSortExample2 {

public static void main(String... args) {
int[] arr = {8, 9, 4, 3, 6, 7, 0, 8, 7, 5, 1};
System.out.println(Arrays.toString(arr));

Arrays.parallelSort(arr, 0, 5);
System.out.println(Arrays.toString(arr));
}
}

Output

[8, 9, 4, 3, 6, 7, 0, 8, 7, 5, 1]
[3, 4, 6, 8, 9, 7, 0, 8, 7, 5, 1]




parallelSort(T[] a, Comparator<? super T> cmp) example

package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class ParallelSortExample3 {

public static void main(String... args) {
Integer[] arr = {8, 9, 4, 3, 6, 7, 0, 8, 7, 5, 1};
System.out.println(Arrays.toString(arr));

Arrays.parallelSort(arr, Comparator.reverseOrder());
System.out.println(Arrays.toString(arr));
}
}

Output

[8, 9, 4, 3, 6, 7, 0, 8, 7, 5, 1]
[9, 8, 8, 7, 7, 6, 5, 4, 3, 1, 0]




A quick performance comparision between sort() and parallelSort(). Repeating it twice because of cold start.

package com.logicbig.example.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class ParallelSortExample4 {

public static void main(String... args) {
String[] arr = {"z", "y", "a", "s", "c"};
System.out.println(Arrays.toString(arr));

Arrays.parallelSort(arr, Comparator.reverseOrder());
System.out.println(Arrays.toString(arr));
}
}

Output

[z, y, a, s, c]
[z, y, s, c, a]

A quick performance comparision between sort() and parallelSort(). Repeating it twice because of cold start.

package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelSortExample5 {

public static void main(String... args) {
for (int c = 0; c < 2; c++) {
int[] arr1 = new int[10000000];
int[] arr2 = new int[10000000];
Arrays.setAll(arr1, i -> (int) (Math.random() * 1000));
Arrays.setAll(arr2, i -> (int) (Math.random() * 1000));

long start = System.nanoTime();
Arrays.parallelSort(arr1);
System.out.printf(" parallel: %,d%n", System.nanoTime() - start);

start = System.nanoTime();
Arrays.sort(arr2);
System.out.printf("sequential: %,d%n", System.nanoTime() - start);
}
}
}

Output

  parallel: 207,779,283
sequential: 360,978,823
parallel: 121,877,362
sequential: 388,106,946




See Also