Skip to content

Commit 3a4042f

Browse files
committed
feat: highlight ap1 and ap2 during the merge when highlighting them
1 parent f2329a4 commit 3a4042f

File tree

2 files changed

+81
-34
lines changed

2 files changed

+81
-34
lines changed

src/algorithms/controllers/msort_arr_bup.js

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ function unhighlight(vis, index, color) {
158158
}
159159
}
160160

161+
function highlightAPointers(vis, ap1, max1, ap2, max2, color) {
162+
if (ap1 <= max1) {
163+
highlight(vis, ap1, color);
164+
}
165+
if (ap2 <= max2) {
166+
highlight(vis, ap2, color);
167+
}
168+
}
169+
170+
// this function sets array a, highlighting from left to mid red, and set the stack
171+
function resetArrayA(vis, A, left, mid, right, runlength) {
172+
vis.array.set(A, 'msort_arr_bup');
173+
highlight2Runlength(vis, left, mid, right, colorA, colorA);
174+
set_simple_stack(vis.array, [runlength]);
175+
}
176+
161177
// Assigns label to array A at index, checks if index is greater than size of array
162178
// if index is greater than size, assign label to last element in array
163179
function assignVarToA(vis, variable_name, index, size) {
@@ -297,14 +313,21 @@ export function run_msort() {
297313

298314
chunker.add('right', (vis, c_right) => {
299315
assignVarToA(vis, 'right', c_right, size);
316+
300317
}, [right]);
301318

302-
chunker.add('ap1', (vis, c_ap1) => {
319+
chunker.add('ap1', (vis, a, c_ap1, c_left, c_mid, c_right, c_rlength) => {
303320
if (isMergeExpanded()) {
321+
322+
// now in the nitty gritty of Merge
323+
// highlight the two parts you want to merge red
324+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
325+
304326
assignVarToA(vis, 'left', undefined, size); // ap1 replaces left
305327
assignVarToA(vis, 'ap1', c_ap1, size);
328+
306329
}
307-
}, [ap1]);
330+
}, [A, ap1, left, mid, right, runlength]);
308331

309332
chunker.add('max1', (vis, c_max1) => {
310333
if (isMergeExpanded()) {
@@ -319,10 +342,11 @@ export function run_msort() {
319342
}
320343
}, [ap2]);
321344

322-
chunker.add('max2', (vis, c_max2) => {
345+
chunker.add('max2', (vis, c_max2, c) => {
323346
if (isMergeExpanded()) {
324347
assignVarToA(vis, 'right', undefined, size); // max2 replaces right
325348
assignVarToA(vis, 'max2', c_max2, size);
349+
326350
}
327351
}, [max2]);
328352

@@ -338,16 +362,19 @@ export function run_msort() {
338362
if (!(ap1 <= max1 && ap2 <= max2)) break;
339363

340364
chunker.add('MergeWhile', (vis, a, c_left, c_right, c_mid, c_ap1, c_max1, c_ap2, c_max2, c_bp, c_rlength) => {
341-
vis.array.set(a, 'msort_arr_bup');
365+
366+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
367+
342368
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
369+
343370
// future color: should be colorA & colorB
344-
highlight2Runlength(vis, c_left, c_mid, c_right, colorA, colorA);
345-
set_simple_stack(vis.array, [c_rlength]);
371+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
372+
346373
}, [A, left, right, mid, ap1, max1, ap2, max2, bp, runlength]);
347374

348-
chunker.add('findSmaller', () => {
375+
chunker.add('findSmaller', (vis, c_ap1, c_max1, c_ap2, c_max2) => {
349376
// no animation
350-
}, []);
377+
}, [ap1, max1, ap2, max2]);
351378

352379
if (A[ap1] < A[ap2]) {
353380

@@ -357,20 +384,28 @@ export function run_msort() {
357384
chunker.add('copyap1', (vis, a, b, c_ap1, c_max1, c_ap2, c_max2, c_bp, c_left, c_right, c_mid, c_rlength) => {
358385

359386
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
360-
vis.array.set(a, 'msort_arr_bup');
387+
388+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
389+
361390

362391
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
363392
// future color: should be colorA & colorB
364-
highlight2Runlength(vis, c_left, c_mid, c_right, colorA, colorA);
393+
394+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
365395
// highlight sorted elements green (colorC)
366396
for (let i = c_left; i <= c_bp; i++) highlightB(vis, i, colorC);
367-
set_simple_stack(vis.array, [c_rlength]);
397+
368398
}, [A, B, ap1, max1, ap2, max2, bp, left, right, mid, runlength]);
369399

370400
ap1 = ap1 + 1;
371-
chunker.add('ap1++', (vis, c_ap1) => {
401+
chunker.add('ap1++', (vis, a, c_left, c_mid, c_right, c_rlength,
402+
c_ap1, c_max1, c_ap2, c_max2, c_bp) => {
403+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
404+
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
405+
372406
assignVarToA(vis, 'ap1', c_ap1, size);
373-
}, [ap1]);
407+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
408+
}, [A, left, mid, right, runlength, ap1, max1, ap2, max2, bp]);
374409

375410
bp = bp + 1;
376411
chunker.add('bp++', (vis, c_bp) => {
@@ -379,29 +414,40 @@ export function run_msort() {
379414
}
380415

381416
else {
382-
chunker.add('findSmallerB', () => {
417+
chunker.add('findSmallerB', (vis, c_ap1, c_max1, c_ap2, c_max2) => {
383418
// no animation
384-
}, []);
419+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
420+
421+
}, [ap1, max1, ap2, max2]);
385422

386423
B[bp] = A[ap2];
387424
A[ap2] = undefined;
388425

389426
chunker.add('copyap2', (vis, a, b, c_ap1, c_ap2, c_bp, c_max1, c_max2, c_left, c_right, c_mid, c_rlength) => {
390427
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
391-
vis.array.set(a, 'msort_arr_bup');
428+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
429+
392430
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
393431

394432
// future color: should be colorA & colorB
395-
highlight2Runlength(vis, c_left, c_mid, c_right, colorA, colorA);
433+
434+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
435+
396436
// highlight sorted elements green / colorB
397437
for (let i = c_left; i <= c_bp; i++) highlightB(vis, i, 'green')
398-
set_simple_stack(vis.array, [c_rlength]);
438+
399439
}, [A, B, ap1, ap2, bp, max1, max2, left, right, mid, runlength]);
400440

401441
ap2 = ap2 + 1;
402-
chunker.add('ap2++', (vis, c_ap2) => {
442+
chunker.add('ap2++', (vis, a, c_left, c_mid, c_right, c_rlength,
443+
c_ap1, c_max1, c_ap2, c_max2, c_bp) => {
444+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
445+
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
446+
403447
assignVarToA(vis, "ap2", c_ap2, size);
404-
}, [ap2]);
448+
highlightAPointers(vis, c_ap1, c_max1, c_ap2, c_max2, colorB);
449+
450+
}, [A, left, mid, right, runlength, ap1, max1, ap2, max2, bp]);
405451

406452
bp = bp + 1;
407453
chunker.add('bp++_2', (vis, c_bp) => {
@@ -418,7 +464,8 @@ export function run_msort() {
418464

419465
chunker.add('CopyRest1', (vis, a, b, c_ap1, c_max1, c_left, c_right, c_mid, c_bp, c_rlength) => {
420466

421-
vis.array.set(a, 'msort_arr_bup');
467+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
468+
422469
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
423470

424471
// copying A[ap1..max1]
@@ -429,9 +476,7 @@ export function run_msort() {
429476
// to highlight the solrted elements of B array green / colorC
430477
for (let i = c_left; i < c_bp; i++) highlightB(vis, i, colorC);
431478

432-
// future color: should be colorA & colorB
433-
highlight2Runlength(vis, c_left, c_mid, c_right, colorA, colorA);
434-
set_simple_stack(vis.array, [c_rlength]);
479+
435480
}, [A, B, ap1, max1, left, right, mid, bp, runlength]);
436481

437482
for (let i = ap2; i <= max2; i++) {
@@ -441,7 +486,8 @@ export function run_msort() {
441486
}
442487

443488
chunker.add('CopyRest2', (vis, a, b, c_ap2, c_max2, c_left, c_right, c_mid, c_rlength) => {
444-
vis.array.set(a, 'msort_arr_bup');
489+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
490+
445491
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
446492
assignVarToA(vis, 'ap2', c_ap2, size);
447493
assignVarToA(vis, 'max2', c_max2, size);
@@ -450,8 +496,7 @@ export function run_msort() {
450496
for (let i = c_left; i <= c_right; i++) highlightB(vis, i, colorC);
451497

452498
// future color: should be colorA & colorB
453-
highlight2Runlength(vis, c_left, c_mid, c_right, colorA, colorA);
454-
set_simple_stack(vis.array, [c_rlength]);
499+
455500
}, [A, B, ap2, max2, left, right, mid, runlength]);
456501

457502
// copy merged elements from B to A
@@ -461,12 +506,15 @@ export function run_msort() {
461506
}
462507

463508
chunker.add('copyBA', (vis, a, b, c_left, c_right, c_rlength) => {
509+
464510
vis.array.set(a, 'msort_arr_bup');
511+
set_simple_stack(vis.array, [c_rlength]);
512+
465513
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
466514

467515
// highlight all sorted elements green
468516
for (let i = c_left; i <= c_right; i++) highlight(vis, i, colorC);
469-
set_simple_stack(vis.array, [c_rlength]);
517+
470518
}, [A, B, left, right, runlength]);
471519

472520
let left2 = left; // this is the old left before it was updated
@@ -481,19 +529,17 @@ export function run_msort() {
481529
if (c_left < size) {
482530
assignVarToA(vis, 'left', c_left, size);
483531
}
484-
if (c_left + c_rlength >= size) {
485-
highlightAllRunlengths(vis, c_rlength * 2, colorA, colorB, size);
486-
487-
}
488532

489533
}, [left2, left, right, runlength]);
490534

491535
}
536+
runlength = 2 * runlength;
492537

538+
chunker.add('mergeDone', (vis, c_rlength) => {
539+
highlightAllRunlengths(vis, c_rlength, colorA, colorB, size);
540+
}, [runlength])
493541

494542

495-
496-
runlength = 2 * runlength;
497543
chunker.add('runlength2', (vis, c_rlength) => {
498544

499545
// highlightAllRunlengths(vis, c_rlength, colorA, colorB, size);

src/algorithms/pseudocode/msort_arr_bup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ MergeAll
3939
merge A[left..mid] and A[mid+1..right], with the result in A \\Ref MergeCopy
4040
left <- right + 1 // skip to the next pair of runs (if any) \\B left2
4141
\\In}
42+
// all consecutive pairs of runs merged \\B mergeDone
4243
\\Code}
4344
4445
\\Note{

0 commit comments

Comments
 (0)