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.
Code
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
Big-O Complexity VisualizerVisualize and compare algorithm time complexities like O(1), O(log n), O(n), O(n log n), and O(n²). Plot growth curves to understand performance scaling at a glance.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.JSON SorterSort JSON object keys by code point ascending or descending, recursively or top-level only, preserving array order.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.