Skip to content

Commit 365c29c

Browse files
committed
Found and fixed one bug in BucketInserterThread.
1 parent 259156f commit 365c29c

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.coderodde.util;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56
import java.util.Random;
67

@@ -286,6 +287,8 @@ private static void parallelRadixSortImpl(
286287

287288
int[][] processedMaps = new int[spawnDegree][BUCKETS];
288289

290+
// On linear data, all processedMaps[i][j] must be 0!
291+
289292
// Make the preprocessing map independent of each thread:
290293
for (int i = 1; i != spawnDegree; i++) {
291294
int[] partialBucketSizeMap =
@@ -318,7 +321,7 @@ private static void parallelRadixSortImpl(
318321
recursionDepth);
319322

320323
sourceStartIndex += subrangeLength;
321-
targetStartIndex += subrangeLength;
324+
// targetStartIndex += subrangeLength;
322325

323326
bucketInserterThread.start();
324327
bucketInserterThreads[i] = bucketInserterThread;
@@ -353,6 +356,19 @@ private static void parallelRadixSortImpl(
353356
}
354357
}
355358

359+
int[] testArray = source.clone();
360+
361+
Arrays.sort(testArray);
362+
363+
for (int i = 0; i != testArray.length; i++) {
364+
int sourceElement = target[i];
365+
int testElement = testArray[i];
366+
367+
if (sourceElement != testElement) {
368+
System.out.println("DEBUG: " + sourceElement + " vs. " + testElement + " at index " + i);
369+
}
370+
}
371+
356372
if (recursionDepth == DEEPEST_RECURSION_DEPTH) {
357373
// Nowhere to recur, all bytes are processed. Return.
358374
return;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,32 @@ public static int[] createRandomIntArray(
2323
public static int[] createRandomIntArray(int size, Random random) {
2424
return createRandomIntArray(size, MAX_VALUE, random);
2525
}
26+
27+
public static int[] createDebugIntArray(int size, Random random) {
28+
int[] array = new int[size];
29+
30+
for (int i = 0; i != size; i++) {
31+
array[i] = random.nextInt(256) << 24;
32+
}
33+
34+
return array;
35+
}
36+
37+
public static int[] createLinearDebugIntArray(int size, Random random) {
38+
int[] array = new int[size];
39+
40+
for (int i = 0; i != size; i++) {
41+
array[i] = i << 24;
42+
}
43+
44+
for (int i = 0; i != 2 * size; i++) {
45+
int index1 = random.nextInt(size);
46+
int index2 = random.nextInt(size);
47+
int a = array[index1];
48+
array[index1] = array[index2];
49+
array[index2] = a;
50+
}
51+
52+
return array;
53+
}
2654
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ public void testSerialRadixSort() {
8484
public void testParallelRadixSort() {
8585
Random random = new Random(29);
8686

87-
final int SIZE = 200;
87+
final int SIZE = 256;
8888

89-
int[] array1 = Utils.createRandomIntArray(
90-
SIZE,
91-
Integer.MAX_VALUE - 1,
92-
random);
89+
int[] array1 = Utils.createLinearDebugIntArray(SIZE, random);
90+
// int[] array1 = Utils.createRandomIntArray(
91+
// SIZE,
92+
// Integer.MAX_VALUE - 1,
93+
// random);
9394

9495
int[] array2 = array1.clone();
9596

96-
final int FROM_INDEX = 13;
97-
final int TO_INDEX = SIZE - 15;
97+
// final int FROM_INDEX = 13;
98+
// final int TO_INDEX = SIZE - 15;
99+
final int FROM_INDEX = 0;
100+
final int TO_INDEX = SIZE;
98101

99102
Arrays.sort(array1, FROM_INDEX, TO_INDEX);
100103
ParallelRadixSort.parallelSort(

0 commit comments

Comments
 (0)