@@ -28,6 +28,14 @@ import {
28
28
areExpanded ,
29
29
} from './collapseChunkPlugin' ;
30
30
31
+ import { colors } from '../../components/DataStructures/colors' ;
32
+ const partLColor = colors . peach ;
33
+ const partRColor = colors . sky ;
34
+ const pivotColor = colors . leaf ;
35
+ const sortedColor = colors . stone ;
36
+ const highlightColor = colors . apple ; // was for i,j in partition,...
37
+
38
+
31
39
export function isPartitionExpanded ( ) {
32
40
return areExpanded ( [ 'Partition' ] ) ;
33
41
}
@@ -85,6 +93,7 @@ const QS_BOOKMARKS = {
85
93
MEDIAN3_set_pivot_to_value_at_array_indx_right_minus_1 : 5 ,
86
94
SHARED_while_i_less_j : 6 ,
87
95
SHARED_incri_i_until_array_index_i_greater_eq_pivot : 7 ,
96
+ SHARED_incri_j_until : 7 , // shortened name
88
97
SHARED_decri_j_until : 8 , // shortened name
89
98
SHARED_if_j_greater_i : 9 ,
90
99
SHARED_swap_array_i_j_vals : 10 ,
@@ -354,12 +363,15 @@ export function run_QS(is_qs_median_of_3) {
354
363
[ a [ n1 ] , a [ n2 ] ] = [ a [ n2 ] , a [ n1 ] ]
355
364
356
365
chunker . add ( bookmark ,
357
- ( vis , _n1 , _n2 , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
366
+ ( vis , _n1 , _n2 , cur_real_stack , cur_finished_stack_frames ,
367
+ cur_i , cur_j , cur_pivot_index , cur_depth , bm ) => {
358
368
359
369
vis . array . swapElements ( _n1 , _n2 ) ;
360
- refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth )
370
+ refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) ;
371
+ if ( bm == QS_BOOKMARKS . SHARED_swap_pivot_into_position )
372
+ vis . array . selectColor ( _n1 , sortedColor ) ;
361
373
} ,
362
- [ n1 , n2 , real_stack , finished_stack_frames , i , j , pivot_index , depth ] ,
374
+ [ n1 , n2 , real_stack , finished_stack_frames , i , j , pivot_index , depth , bookmark ] ,
363
375
depth ) ;
364
376
}
365
377
@@ -387,15 +399,33 @@ export function run_QS(is_qs_median_of_3) {
387
399
388
400
const mid = Math . floor ( ( left + right ) / 2 ) ;
389
401
402
+ // here we color the three potential pivot elements according
403
+ // to their final positions after swapping, so the colors
404
+ // stick to the elements and will be in the right order after
405
+ // swaps. We duplicate the if-then-elses but just change
406
+ // vars, not array elements.
407
+ let finalleft = left ;
408
+ let finalmid = mid ;
409
+ let finalright = right ;
410
+ if ( a [ finalleft ] > a [ finalmid ] ) {
411
+ [ finalleft , finalmid ] = [ finalmid , finalleft ] ;
412
+ }
413
+ if ( a [ finalmid ] > a [ finalright ] ) {
414
+ [ finalmid , finalright ] = [ finalright , finalmid ] ;
415
+ if ( a [ finalleft ] > a [ finalmid ] ) {
416
+ [ finalmid , finalleft ] = [ finalleft , finalmid ] ;
417
+ }
418
+ }
419
+
420
+
390
421
// assigning the pivot as the midpoint calculated above
391
- chunker_add_if ( QS_BOOKMARKS . MEDIAN3_mid_to_middle_index , ( vis , cur_mid , cur_left , cur_right ) => {
392
- highlight ( vis , cur_mid , false ) ;
393
- // highlight seems to increment counter rather than set flag
394
- if ( cur_left !== cur_mid )
395
- highlight ( vis , cur_left , false ) ;
396
- highlight ( vis , cur_right , false ) ;
422
+ chunker_add_if ( QS_BOOKMARKS . MEDIAN3_mid_to_middle_index , ( vis , fin_mid , fin_left , fin_right ) => {
423
+ vis . array . selectColor ( fin_mid , pivotColor ) ;
424
+ vis . array . selectColor ( fin_left , partLColor ) ;
425
+ vis . array . selectColor ( fin_right , partRColor ) ;
426
+ // XXX check/fix above for small partitions
397
427
} ,
398
- [ mid , left , right ] ) ;
428
+ [ finalmid , finalleft , finalright ] ) ;
399
429
400
430
chunker_add_if ( QS_BOOKMARKS . MEDIAN3_first_if_A_idx_left_greater_A_idx_right ) ;
401
431
if ( a [ left ] > a [ mid ] ) {
@@ -421,11 +451,11 @@ export function run_QS(is_qs_median_of_3) {
421
451
pivot_index = right - 1 ;
422
452
chunker_add_if ( QS_BOOKMARKS . MEDIAN3_set_pivot_to_value_at_array_indx_right_minus_1 ,
423
453
( vis , cur_right , cur_left , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
424
- unhighlight ( vis , cur_right , false ) ;
425
- unhighlight ( vis , cur_right - 1 , false ) ;
454
+ // unhighlight(vis, cur_right, false);
455
+ // unhighlight(vis, cur_right -1, false);
426
456
// unhighlight seems to decrement counter rather than unset flag
427
- if ( cur_left !== cur_right - 1 )
428
- unhighlight ( vis , cur_left , false ) ;
457
+ // if (cur_left !== cur_right -1)
458
+ // unhighlight(vis, cur_left, false);
429
459
430
460
refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) // refresh stack to show pivot_index
431
461
} ,
@@ -439,6 +469,7 @@ export function run_QS(is_qs_median_of_3) {
439
469
QS_BOOKMARKS . RIGHT_P_set_pivot_to_value_at_array_indx_right ,
440
470
( vis , cur_right , cur_left , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
441
471
refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) // refresh stack to show pivot_index
472
+ vis . array . selectColor ( cur_pivot_index , pivotColor ) ;
442
473
} ,
443
474
[ right , left , real_stack , finished_stack_frames , i , j , pivot_index , depth ] ) ;
444
475
//refresh_stack);
@@ -478,20 +509,37 @@ export function run_QS(is_qs_median_of_3) {
478
509
do {
479
510
i += 1 ;
480
511
512
+ let iColor ;
513
+ if ( a [ i ] < pivot_value ( ) )
514
+ iColor = partLColor ;
515
+ else
516
+ iColor = partRColor ;
481
517
chunker_add_if (
482
- QS_BOOKMARKS . SHARED_incri_i_until_array_index_i_greater_eq_pivot ,
483
- refresh_stack ) ;
518
+ QS_BOOKMARKS . SHARED_incri_j_until ,
519
+ ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
520
+ refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) ;
521
+ vis . array . selectColor ( cur_i , iColor ) ;
522
+ } ) ;
484
523
485
524
} while ( a [ i ] < pivot_value ( ) ) ;
486
525
487
526
do {
488
527
j -= 1 ;
489
528
529
+ let jColor ;
530
+ if ( i <= j && pivot_value ( ) < a [ j ] )
531
+ jColor = partRColor ;
532
+ else
533
+ jColor = partLColor ;
490
534
chunker_add_if (
491
535
QS_BOOKMARKS . SHARED_decri_j_until ,
492
- refresh_stack ) ;
536
+ ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
537
+ refresh_stack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) ;
538
+ if ( cur_j >= left ) // XXX pass in left?
539
+ vis . array . selectColor ( cur_j , jColor ) ;
540
+ } ) ;
493
541
494
- } while ( i <= j && pivot_value ( ) < a [ j ] ) ;
542
+ } while ( i < j && pivot_value ( ) < a [ j ] ) ;
495
543
496
544
497
545
chunker_add_if ( QS_BOOKMARKS . SHARED_if_j_greater_i ) ;
@@ -507,12 +555,14 @@ export function run_QS(is_qs_median_of_3) {
507
555
swapAction ( QS_BOOKMARKS . SHARED_swap_pivot_into_position , i ,
508
556
is_qs_median_of_3 ? right - 1 : right
509
557
) ;
510
-
558
+ // XXX best make pivot sorted above and delete next chunk?
559
+ /*
511
560
chunker_add_if(
512
561
QS_BOOKMARKS.SHARED_swap_pivot_into_position,
513
562
(vis, cur_real_stack, cur_finished_stack_frames, cur_i, cur_j, cur_pivot_index, cur_depth) => {
514
563
vis.array.sorted(cur_pivot_index);
515
564
});
565
+ */
516
566
517
567
return [ i , a ] ; // Return [pivot location, array partition_num_array]
518
568
}
@@ -543,15 +593,19 @@ export function run_QS(is_qs_median_of_3) {
543
593
// is a chunk at this recursion level as the first chunk in the
544
594
// collapsed code for the recursive call
545
595
// We no longer want to display 'pivot' or 'j' but want 'i'
596
+ // (we use pivot_index but keep pivot for deselect)
546
597
let pivot_index = undefined ;
547
598
let j = undefined ;
548
599
let i = pivot ;
549
600
chunker . add ( QS_BOOKMARKS . SHARED_pre_left ,
550
- ( vis , cur_right , cur_left , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
601
+ ( vis , cur_right , cur_left , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth , old_pivot ) => {
551
602
refresh_stack ( vis , cur_real_stack ,
552
603
cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) // refresh shows i
604
+ for ( let i = cur_left ; i <= cur_right ; i ++ )
605
+ if ( i !== old_pivot )
606
+ vis . array . deselect ( i ) ;
553
607
} ,
554
- [ right , left , real_stack , finished_stack_frames , i , j , pivot_index , depth ] , depth ) ;
608
+ [ right , left , real_stack , finished_stack_frames , i , j , pivot_index , depth , pivot ] , depth ) ;
555
609
556
610
QuickSort ( a , left , pivot - 1 , depth + 1 ) ;
557
611
0 commit comments