Skip to content

Commit d563446

Browse files
committed
...
1 parent fb00ab2 commit d563446

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/main/java/com/github/coderodde/util/ParallelRadixSort.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/java/com/github/coderodde/util/ParallelRadixSortTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ public void testMergesort() {
5656
@Test
5757
public void testSerialRadixSort() {
5858
Random random = new Random(26);
59-
final int SIZE = 128;
59+
final int SIZE = 100;
6060
int[] array1 = Utils.createRandomIntArray(
6161
SIZE,
6262
Integer.MAX_VALUE - 1,
6363
random);
6464

6565
int[] array2 = array1.clone();
6666

67-
final int FROM_INDEX = 14;
68-
final int TO_INDEX = SIZE - 14;
67+
final int FROM_INDEX = 10;
68+
final int TO_INDEX = SIZE - 5;
6969

7070
Arrays.sort(array1, FROM_INDEX, TO_INDEX);
7171
ParallelRadixSort.parallelSort(
@@ -215,6 +215,6 @@ public void getBucketIndex() {
215215
0x8000_0503,
216216
0);
217217

218-
assertEquals(1, bucketKey);
218+
assertEquals(0, bucketKey);
219219
}
220220
}

0 commit comments

Comments
 (0)