@@ -36,18 +36,48 @@ public final class ParallelRadixSort {
3636 * The array slices smaller than this number of elements will be sorted with
3737 * merge sort.
3838 */
39- static final int MERGESORT_THRESHOLD = 4096 ;
39+ static final int DEFAULT_MERGESORT_THRESHOLD = 4096 ;
4040
4141 /**
4242 * The array slices smaller than this number of elements will be sorted with
4343 * insertion sort.
4444 */
45- static final int INSERTION_SORT_THRESHOLD = 16 ;
45+ static final int DEFAULT_INSERTION_SORT_THRESHOLD = 16 ;
4646
4747 /**
4848 * The minimum workload for a thread.
4949 */
50- private static final int THREAD_THRESHOLD = 65536 ;
50+ private static final int DEFAULT_THREAD_THRESHOLD = 65536 ;
51+
52+ private static final int MINIMUM_MERGESORT_THRESHOLD = 61 ;
53+ private static final int MINIMUM_INSERTION_SORT_THRESHOLD = 7 ;
54+ private static final int MINIMUM_THREAD_WORKLOAD = 4001 ;
55+
56+ private static int insertionSortThreshold = DEFAULT_INSERTION_SORT_THRESHOLD ;
57+ private static int mergesortThreshold = DEFAULT_MERGESORT_THRESHOLD ;
58+ private static int threadWorkload = DEFAULT_THREAD_THRESHOLD ;
59+
60+ public static void setInsertionSortThreshold (
61+ int newInsertionSortThreshold ) {
62+ insertionSortThreshold =
63+ Math .max (
64+ newInsertionSortThreshold ,
65+ MINIMUM_INSERTION_SORT_THRESHOLD );
66+ }
67+
68+ public static void setMergesortThreshold (int newMergesortThreshold ) {
69+ mergesortThreshold =
70+ Math .max (
71+ newMergesortThreshold ,
72+ MINIMUM_MERGESORT_THRESHOLD );
73+ }
74+
75+ public static void setThreadWorkload (int newThreadWorkload ) {
76+ threadWorkload =
77+ Math .max (
78+ MINIMUM_THREAD_WORKLOAD ,
79+ newThreadWorkload );
80+ }
5181
5282 public static void parallelSort (int [] array ) {
5383 parallelSort (array , 0 , array .length );
@@ -62,14 +92,14 @@ public static void parallelSort(int[] array, int fromIndex, int toIndex) {
6292 return ;
6393 }
6494
65- if (rangeLength <= INSERTION_SORT_THRESHOLD ) {
95+ if (rangeLength <= insertionSortThreshold ) {
6696 insertionSort (array , fromIndex , rangeLength );
6797 return ;
6898 }
6999
70100 int [] buffer = new int [rangeLength ];
71101
72- if (rangeLength <= MERGESORT_THRESHOLD ) {
102+ if (rangeLength <= mergesortThreshold ) {
73103 mergesort (
74104 array ,
75105 buffer ,
@@ -84,7 +114,7 @@ public static void parallelSort(int[] array, int fromIndex, int toIndex) {
84114 int threads =
85115 Math .min (
86116 Runtime .getRuntime ().availableProcessors (),
87- rangeLength / THREAD_THRESHOLD );
117+ rangeLength / threadWorkload );
88118
89119 threads = Math .max (threads , 1 );
90120
@@ -172,7 +202,7 @@ private static void radixSortImpl(int[] source,
172202 int rangeLength ,
173203 int recursionDepth ) {
174204
175- if (rangeLength <= MERGESORT_THRESHOLD ) {
205+ if (rangeLength <= mergesortThreshold ) {
176206 mergesort (
177207 source ,
178208 target ,
@@ -252,18 +282,17 @@ private static void mergesort(int[] source,
252282 int [] t = target ;
253283 int sFromIndex = sourceFromIndex ;
254284 int tFromIndex = targetFromIndex ;
255- int runs = rangeLength / INSERTION_SORT_THRESHOLD ;
285+ int runs = rangeLength / insertionSortThreshold ;
256286
257287 for (int i = 0 ; i != runs ; ++i ) {
258- insertionSort (
259- source ,
288+ insertionSort (source ,
260289 offset ,
261- INSERTION_SORT_THRESHOLD );
290+ insertionSortThreshold );
262291
263- offset += INSERTION_SORT_THRESHOLD ;
292+ offset += insertionSortThreshold ;
264293 }
265294
266- if (rangeLength % INSERTION_SORT_THRESHOLD != 0 ) {
295+ if (rangeLength % insertionSortThreshold != 0 ) {
267296 // Sort the rightmost run that is smaller than
268297 // INSERTION_SORT_THRESHOLD elements.
269298 insertionSort (
@@ -274,7 +303,7 @@ private static void mergesort(int[] source,
274303 runs ++;
275304 }
276305
277- int runWidth = INSERTION_SORT_THRESHOLD ;
306+ int runWidth = insertionSortThreshold ;
278307 int passes = 0 ;
279308
280309 while (runs != 1 ) {
0 commit comments