@@ -122,6 +122,32 @@ function highlightAllRunlengths(vis, runlength, colorA, colorB, size) {
122
122
}
123
123
}
124
124
125
+ // Unhighlight entire array alternating colors for runlength
126
+ function unhighlightAllRunlengths ( vis , runlength , colorA , colorB , size ) {
127
+ let toggle = 0 ; // 0 = colorA, 1 = colorB
128
+
129
+ for ( let i = 0 ; i < size ; i ++ ) {
130
+ if ( toggle == 0 ) {
131
+ unhighlight ( vis , i , colorA ) ;
132
+ console . log ( "toggle == 0" ) ;
133
+ }
134
+ if ( toggle == 1 ) {
135
+ unhighlight ( vis , i , colorB ) ;
136
+ console . log ( "toggle == 1" ) ;
137
+ }
138
+ console . log ( "(i + 1) % runlength = " + ( runlength % ( i + 1 ) ) ) ;
139
+ console . log ( "(runlength = " + ( runlength ) ) ;
140
+ // Switch color after completing a run of length 'runlength'
141
+ if ( ( i + 1 ) % runlength == 0 ) {
142
+
143
+ console . log ( "(i + 1) % runlength == 0" ) ;
144
+
145
+ toggle = 1 - toggle ; // Flip toggle between 0 and 1
146
+
147
+ } console . log ( "toggle = " + toggle ) ;
148
+ }
149
+ }
150
+
125
151
// unhighlights arrayA
126
152
function unhighlight ( vis , index , color ) {
127
153
if ( color == 'red' ) {
@@ -227,20 +253,25 @@ export function run_msort() {
227
253
let left = 0 ;
228
254
229
255
chunker . add ( 'MainWhile' , ( vis , c_rlength , c_left ) => {
256
+
230
257
// display size label
231
258
assignVarToA ( vis , ( "size = " + size ) , size , size ) ;
232
259
233
- let left_2 = c_left ;
234
- let mid_2 = ( c_rlength + c_left - 1 ) ;
235
- let right_2 = ( Math . min ( c_rlength * 2 , size ) - 1 ) ;
236
260
237
- highlight2Runlength ( vis , left_2 , mid_2 , right_2 , colorA , colorB ) ;
238
261
239
262
} , [ runlength , left ] ) ;
240
263
241
- chunker . add ( 'left' , ( vis , c_left ) => {
264
+ chunker . add ( 'left' , ( vis , c_left , c_rlength ) => {
242
265
assignVarToA ( vis , 'left' , c_left , size ) ;
243
- } , [ left ] ) ;
266
+
267
+ unhighlightAllRunlengths ( vis , c_rlength , colorA , colorB , size ) ;
268
+
269
+ let left_2 = c_left ;
270
+ let mid_2 = ( c_rlength + c_left - 1 ) ;
271
+ let right_2 = ( Math . min ( c_rlength * 2 , size ) - 1 ) ;
272
+
273
+ highlight2Runlength ( vis , left_2 , mid_2 , right_2 , colorA , colorB ) ;
274
+ } , [ left , runlength ] ) ;
244
275
245
276
while ( ( left + runlength ) <= size ) {
246
277
@@ -442,22 +473,30 @@ export function run_msort() {
442
473
443
474
left = right + 1 ;
444
475
445
- chunker . add ( 'left2' , ( vis , old_left , c_left , c_right ) => {
476
+ chunker . add ( 'left2' , ( vis , old_left , c_left , c_right , c_rlength ) => {
446
477
// unhighlight all elements in A
447
478
for ( let i = old_left ; i <= c_right ; i ++ ) {
448
479
unhighlight ( vis , i , colorC ) ;
449
480
}
450
481
if ( c_left < size ) {
451
482
assignVarToA ( vis , 'left' , c_left , size ) ;
452
483
}
484
+ if ( c_left + c_rlength >= size ) {
485
+ highlightAllRunlengths ( vis , c_rlength * 2 , colorA , colorB , size ) ;
453
486
454
- } , [ left2 , left , right ] ) ;
487
+ }
488
+
489
+ } , [ left2 , left , right , runlength ] ) ;
455
490
456
491
}
457
492
458
493
494
+
495
+
459
496
runlength = 2 * runlength ;
460
497
chunker . add ( 'runlength2' , ( vis , c_rlength ) => {
498
+
499
+ // highlightAllRunlengths(vis, c_rlength, colorA, colorB, size);
461
500
assignVarToA ( vis , 'left' , undefined , size ) ;
462
501
set_simple_stack ( vis . array , [ c_rlength ] ) ;
463
502
0 commit comments