Arrays parallelSort Big Array Sorting

Arrays.parallelSort splits a large object array into sub-arrays sorted and merged across the ForkJoin common pool; small arrays fall back to TimSort.

JavaCli Tooling

Code

snippet
import java.util.Arrays;
import java.util.Random;

public class BigSort {

    public static void main(String[] args) {
        Random random = new Random(7);
        Integer[] readings = new Integer[100_000];
        Arrays.setAll(readings, i -> random.nextInt(1_000_000));

        Integer[] expected = readings.clone();

        // 并行归并:分段排序后合并;n <= MIN_ARRAY_SORT_GRAN(8192) 或
        // 公共池并行度为 1 时直接退回 TimSort 串行排序
        Arrays.parallelSort(readings);

        // 对照:同一数据走串行 sort,结果必须一致
        Arrays.sort(expected);
        System.out.println("same result: " + Arrays.equals(readings, expected));

        // 排序是稳定的:同值元素相对次序不变(javadoc 明示 guaranteed stable)
        Integer[] ties = {3, 1, 3, 1};
        Arrays.parallelSort(ties);
        System.out.println("ties sorted: " + Arrays.toString(ties));

        System.out.println("head: " + readings[0] + " tail: " + readings[readings.length - 1]);
    }
}

How it works

The code’s whole job belongs to the class BigSort and the function main.

Watch out for

  • All of its symbols are language built-ins — nothing needs fetching.

Using it

Signature: public static void main(String[] args)

The demo output is labeled (same result:, ties sorted:, head:), so every line in the printed output names the step that produced it.

The key statements, annotated

Arrays.sort(expected);

Interactive Tools for This Topic

FAQ

What does Arrays parallelSort Big Array Sorting print when run?
The script runs standalone; its 3 labeled outputs include `same result:`, `ties sorted:`, `head:`.