@@ -36,28 +36,28 @@ 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 DEFAULT_MERGESORT_THRESHOLD = 4096 ;
39+ static final int DEFAULT_MERGESORT_THRESHOLD = 4001 ;
4040
4141 /**
4242 * The array slices smaller than this number of elements will be sorted with
4343 * insertion sort.
4444 */
45- static final int DEFAULT_INSERTION_SORT_THRESHOLD = 16 ;
45+ static final int DEFAULT_INSERTION_SORT_THRESHOLD = 13 ;
4646
4747 /**
4848 * The minimum workload for a thread.
4949 */
5050 private static final int DEFAULT_THREAD_THRESHOLD = 65536 ;
5151
5252 /**
53- * Minimum mergesort threshold.
53+ * Minimum merge sort threshold.
5454 */
55- private static final int MINIMUM_MERGESORT_THRESHOLD = 7 ;
55+ private static final int MINIMUM_MERGESORT_THRESHOLD = 1 ;
5656
5757 /**
5858 * Minimum insertion sort threshold.
5959 */
60- private static final int MINIMUM_INSERTION_SORT_THRESHOLD = 7 ;
60+ private static final int MINIMUM_INSERTION_SORT_THRESHOLD = 1 ;
6161
6262 /**
6363 * Minimum thread workload.
@@ -147,17 +147,6 @@ private static void parallelRadixSortImpl(
147147 int rangeLength ,
148148 int recursionDepth ,
149149 int threads ) {
150- // TODO: Remove?
151- // if (rangeLength <= MERGESORT_THRESHOLD) {
152- // mergesort(source,
153- // target,
154- // sourceFromIndex,
155- // targetFromIndex,
156- // rangeLength,
157- // recursionDepth);
158- //
159- // return;
160- // }
161150
162151 if (threads == 1 ) {
163152 radixSortImpl (
@@ -212,18 +201,18 @@ private static void radixSortImpl(int[] source,
212201 int targetFromIndex ,
213202 int rangeLength ,
214203 int recursionDepth ) {
215-
216- if (rangeLength <= mergesortThreshold ) {
217- mergesort (
218- source ,
219- target ,
220- sourceFromIndex ,
221- targetFromIndex ,
222- rangeLength ,
223- recursionDepth );
224-
225- return ;
226- }
204+ //
205+ // if (rangeLength <= mergesortThreshold) {
206+ // mergesort(
207+ // source,
208+ // target,
209+ // sourceFromIndex,
210+ // targetFromIndex,
211+ // rangeLength,
212+ // recursionDepth);
213+ //
214+ // return;
215+ // }
227216
228217 int [] bucketSizeMap = new int [BUCKETS ];
229218 int [] startIndexMap = new int [BUCKETS ];
@@ -240,8 +229,7 @@ private static void radixSortImpl(int[] source,
240229 bucketSizeMap [bucketIndex ]++;
241230 }
242231
243- // Start computin the map mapping each bucket key to the index in the
244- // source array at which the key appears:
232+ // Compute starting indices for buckets in the target array.
245233 startIndexMap [0 ] = targetFromIndex ;
246234
247235 for (int i = 1 ; i != BUCKETS ; i ++) {
@@ -272,12 +260,33 @@ private static void radixSortImpl(int[] source,
272260
273261 for (int i = 0 ; i != BUCKETS ; i ++) {
274262 if (bucketSizeMap [i ] != 0 ) {
263+ int targetBucketStartIndex ;
264+ int sourceBucketStartIndex ;
265+
266+ if (recursionDepth % 2 == 0 ) {
267+ // Once here, we are sorting from original array to buffer
268+ // array:
269+ sourceBucketStartIndex = startIndexMap [i ] // Original.
270+ + sourceFromIndex ;
271+
272+ targetBucketStartIndex = startIndexMap [i ]; // Buffer.
273+ } else {
274+ // Once here, we are sorting from buffer to original array:
275+ sourceBucketStartIndex = startIndexMap [i ]
276+ - targetFromIndex ; // Buffer.
277+
278+ targetBucketStartIndex = startIndexMap [i ]; // Original.
279+ }
280+
281+ int bucketSize = bucketSizeMap [i ];
282+
283+ // Sort from 'target' to 'source':
275284 radixSortImpl (
276285 target ,
277286 source ,
278- startIndexMap [ i ] ,
279- startIndexMap [ i ] ,
280- bucketSizeMap [ i ] ,
287+ targetBucketStartIndex ,
288+ sourceBucketStartIndex ,
289+ bucketSize ,
281290 recursionDepth + 1 );
282291 }
283292 }
0 commit comments