@@ -158,6 +158,22 @@ function unhighlight(vis, index, color) {
158
158
}
159
159
}
160
160
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
+
161
177
// Assigns label to array A at index, checks if index is greater than size of array
162
178
// if index is greater than size, assign label to last element in array
163
179
function assignVarToA ( vis , variable_name , index , size ) {
@@ -297,14 +313,21 @@ export function run_msort() {
297
313
298
314
chunker . add ( 'right' , ( vis , c_right ) => {
299
315
assignVarToA ( vis , 'right' , c_right , size ) ;
316
+
300
317
} , [ right ] ) ;
301
318
302
- chunker . add ( 'ap1' , ( vis , c_ap1 ) => {
319
+ chunker . add ( 'ap1' , ( vis , a , c_ap1 , c_left , c_mid , c_right , c_rlength ) => {
303
320
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
+
304
326
assignVarToA ( vis , 'left' , undefined , size ) ; // ap1 replaces left
305
327
assignVarToA ( vis , 'ap1' , c_ap1 , size ) ;
328
+
306
329
}
307
- } , [ ap1 ] ) ;
330
+ } , [ A , ap1 , left , mid , right , runlength ] ) ;
308
331
309
332
chunker . add ( 'max1' , ( vis , c_max1 ) => {
310
333
if ( isMergeExpanded ( ) ) {
@@ -319,10 +342,11 @@ export function run_msort() {
319
342
}
320
343
} , [ ap2 ] ) ;
321
344
322
- chunker . add ( 'max2' , ( vis , c_max2 ) => {
345
+ chunker . add ( 'max2' , ( vis , c_max2 , c ) => {
323
346
if ( isMergeExpanded ( ) ) {
324
347
assignVarToA ( vis , 'right' , undefined , size ) ; // max2 replaces right
325
348
assignVarToA ( vis , 'max2' , c_max2 , size ) ;
349
+
326
350
}
327
351
} , [ max2 ] ) ;
328
352
@@ -338,16 +362,19 @@ export function run_msort() {
338
362
if ( ! ( ap1 <= max1 && ap2 <= max2 ) ) break ;
339
363
340
364
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
+
342
368
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
369
+
343
370
// 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
+
346
373
} , [ A , left , right , mid , ap1 , max1 , ap2 , max2 , bp , runlength ] ) ;
347
374
348
- chunker . add ( 'findSmaller' , ( ) => {
375
+ chunker . add ( 'findSmaller' , ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 ) => {
349
376
// no animation
350
- } , [ ] ) ;
377
+ } , [ ap1 , max1 , ap2 , max2 ] ) ;
351
378
352
379
if ( A [ ap1 ] < A [ ap2 ] ) {
353
380
@@ -357,20 +384,28 @@ export function run_msort() {
357
384
chunker . add ( 'copyap1' , ( vis , a , b , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , c_left , c_right , c_mid , c_rlength ) => {
358
385
359
386
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
+
361
390
362
391
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
363
392
// 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 ) ;
365
395
// highlight sorted elements green (colorC)
366
396
for ( let i = c_left ; i <= c_bp ; i ++ ) highlightB ( vis , i , colorC ) ;
367
- set_simple_stack ( vis . array , [ c_rlength ] ) ;
397
+
368
398
} , [ A , B , ap1 , max1 , ap2 , max2 , bp , left , right , mid , runlength ] ) ;
369
399
370
400
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
+
372
406
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 ] ) ;
374
409
375
410
bp = bp + 1 ;
376
411
chunker . add ( 'bp++' , ( vis , c_bp ) => {
@@ -379,29 +414,40 @@ export function run_msort() {
379
414
}
380
415
381
416
else {
382
- chunker . add ( 'findSmallerB' , ( ) => {
417
+ chunker . add ( 'findSmallerB' , ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 ) => {
383
418
// no animation
384
- } , [ ] ) ;
419
+ highlightAPointers ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , colorB ) ;
420
+
421
+ } , [ ap1 , max1 , ap2 , max2 ] ) ;
385
422
386
423
B [ bp ] = A [ ap2 ] ;
387
424
A [ ap2 ] = undefined ;
388
425
389
426
chunker . add ( 'copyap2' , ( vis , a , b , c_ap1 , c_ap2 , c_bp , c_max1 , c_max2 , c_left , c_right , c_mid , c_rlength ) => {
390
427
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
+
392
430
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
393
431
394
432
// 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
+
396
436
// highlight sorted elements green / colorB
397
437
for ( let i = c_left ; i <= c_bp ; i ++ ) highlightB ( vis , i , 'green' )
398
- set_simple_stack ( vis . array , [ c_rlength ] ) ;
438
+
399
439
} , [ A , B , ap1 , ap2 , bp , max1 , max2 , left , right , mid , runlength ] ) ;
400
440
401
441
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
+
403
447
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 ] ) ;
405
451
406
452
bp = bp + 1 ;
407
453
chunker . add ( 'bp++_2' , ( vis , c_bp ) => {
@@ -418,7 +464,8 @@ export function run_msort() {
418
464
419
465
chunker . add ( 'CopyRest1' , ( vis , a , b , c_ap1 , c_max1 , c_left , c_right , c_mid , c_bp , c_rlength ) => {
420
466
421
- vis . array . set ( a , 'msort_arr_bup' ) ;
467
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
468
+
422
469
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
423
470
424
471
// copying A[ap1..max1]
@@ -429,9 +476,7 @@ export function run_msort() {
429
476
// to highlight the solrted elements of B array green / colorC
430
477
for ( let i = c_left ; i < c_bp ; i ++ ) highlightB ( vis , i , colorC ) ;
431
478
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
+
435
480
} , [ A , B , ap1 , max1 , left , right , mid , bp , runlength ] ) ;
436
481
437
482
for ( let i = ap2 ; i <= max2 ; i ++ ) {
@@ -441,7 +486,8 @@ export function run_msort() {
441
486
}
442
487
443
488
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
+
445
491
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
446
492
assignVarToA ( vis , 'ap2' , c_ap2 , size ) ;
447
493
assignVarToA ( vis , 'max2' , c_max2 , size ) ;
@@ -450,8 +496,7 @@ export function run_msort() {
450
496
for ( let i = c_left ; i <= c_right ; i ++ ) highlightB ( vis , i , colorC ) ;
451
497
452
498
// 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
+
455
500
} , [ A , B , ap2 , max2 , left , right , mid , runlength ] ) ;
456
501
457
502
// copy merged elements from B to A
@@ -461,12 +506,15 @@ export function run_msort() {
461
506
}
462
507
463
508
chunker . add ( 'copyBA' , ( vis , a , b , c_left , c_right , c_rlength ) => {
509
+
464
510
vis . array . set ( a , 'msort_arr_bup' ) ;
511
+ set_simple_stack ( vis . array , [ c_rlength ] ) ;
512
+
465
513
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
466
514
467
515
// highlight all sorted elements green
468
516
for ( let i = c_left ; i <= c_right ; i ++ ) highlight ( vis , i , colorC ) ;
469
- set_simple_stack ( vis . array , [ c_rlength ] ) ;
517
+
470
518
} , [ A , B , left , right , runlength ] ) ;
471
519
472
520
let left2 = left ; // this is the old left before it was updated
@@ -481,19 +529,17 @@ export function run_msort() {
481
529
if ( c_left < size ) {
482
530
assignVarToA ( vis , 'left' , c_left , size ) ;
483
531
}
484
- if ( c_left + c_rlength >= size ) {
485
- highlightAllRunlengths ( vis , c_rlength * 2 , colorA , colorB , size ) ;
486
-
487
- }
488
532
489
533
} , [ left2 , left , right , runlength ] ) ;
490
534
491
535
}
536
+ runlength = 2 * runlength ;
492
537
538
+ chunker . add ( 'mergeDone' , ( vis , c_rlength ) => {
539
+ highlightAllRunlengths ( vis , c_rlength , colorA , colorB , size ) ;
540
+ } , [ runlength ] )
493
541
494
542
495
-
496
- runlength = 2 * runlength ;
497
543
chunker . add ( 'runlength2' , ( vis , c_rlength ) => {
498
544
499
545
// highlightAllRunlengths(vis, c_rlength, colorA, colorB, size);
0 commit comments