Skip to content

Commit 8b473b4

Browse files
committed
out of range bug fixed
1 parent 7854279 commit 8b473b4

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/algorithms/controllers/MSDRadixSort.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export default {
176176
// first/last element and give the actual value of j/i
177177
let cur_i_too_high;
178178
let cur_j_too_low;
179+
let tmp_j = cur_j; // used to determine context later
179180
if (cur_i === A.length) {
180181
cur_i = undefined;
181182
cur_i_too_high = A.length - 1;
@@ -224,10 +225,11 @@ export default {
224225
// init, before partition
225226
highlight(vis, cur_i)
226227
highlight(vis, cur_j)
227-
} else if (cur_i !== undefined && cur_j === undefined && prev_i !== undefined) {
228+
} else if (cur_i !== undefined && tmp_j === undefined && prev_i !== undefined) {
228229
// just before first recursive call
229230
unhighlight(vis, prev_i)
230-
unhighlight(vis, prev_j)
231+
if (prev_j >= 0) // might have fallen off array
232+
unhighlight(vis, prev_j)
231233
} else if (checkingLeft) {
232234
if (prev_i !== undefined && prev_i !== cur_j)
233235
unhighlight(vis, prev_i)
@@ -236,8 +238,9 @@ export default {
236238
updateBinary(vis, arr[cur_i])
237239
} else {
238240
if (prev_j !== undefined && prev_j !== cur_i)
239-
unhighlight(vis, prev_j)
240-
highlight(vis, cur_j)
241+
unhighlight(vis, prev_j);
242+
if (cur_j !== undefined) // might have fallen off array
243+
highlight(vis, cur_j);
241244
if (arr && cur_j)
242245
updateBinary(vis, arr[cur_j])
243246
}

0 commit comments

Comments
 (0)