@@ -28,6 +28,14 @@ import {
2828 areExpanded ,
2929} from './collapseChunkPlugin' ;
3030
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+
3139export function isPartitionExpanded ( ) {
3240 return areExpanded ( [ 'Partition' ] ) ;
3341}
@@ -85,6 +93,7 @@ const QS_BOOKMARKS = {
8593 MEDIAN3_set_pivot_to_value_at_array_indx_right_minus_1 : 5 ,
8694 SHARED_while_i_less_j : 6 ,
8795 SHARED_incri_i_until_array_index_i_greater_eq_pivot : 7 ,
96+ SHARED_incri_j_until : 7 , // shortened name
8897 SHARED_decri_j_until : 8 , // shortened name
8998 SHARED_if_j_greater_i : 9 ,
9099 SHARED_swap_array_i_j_vals : 10 ,
@@ -354,12 +363,15 @@ export function run_QS(is_qs_median_of_3) {
354363 [ a [ n1 ] , a [ n2 ] ] = [ a [ n2 ] , a [ n1 ] ]
355364
356365 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 ) => {
358368
359369 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 ) ;
361373 } ,
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 ] ,
363375 depth ) ;
364376 }
365377
@@ -387,15 +399,33 @@ export function run_QS(is_qs_median_of_3) {
387399
388400 const mid = Math . floor ( ( left + right ) / 2 ) ;
389401
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+
390421 // 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
397427 } ,
398- [ mid , left , right ] ) ;
428+ [ finalmid , finalleft , finalright ] ) ;
399429
400430 chunker_add_if ( QS_BOOKMARKS . MEDIAN3_first_if_A_idx_left_greater_A_idx_right ) ;
401431 if ( a [ left ] > a [ mid ] ) {
@@ -421,11 +451,11 @@ export function run_QS(is_qs_median_of_3) {
421451 pivot_index = right - 1 ;
422452 chunker_add_if ( QS_BOOKMARKS . MEDIAN3_set_pivot_to_value_at_array_indx_right_minus_1 ,
423453 ( 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);
426456 // 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);
429459
430460 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
431461 } ,
@@ -439,6 +469,7 @@ export function run_QS(is_qs_median_of_3) {
439469 QS_BOOKMARKS . RIGHT_P_set_pivot_to_value_at_array_indx_right ,
440470 ( vis , cur_right , cur_left , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth ) => {
441471 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 ) ;
442473 } ,
443474 [ right , left , real_stack , finished_stack_frames , i , j , pivot_index , depth ] ) ;
444475 //refresh_stack);
@@ -478,20 +509,37 @@ export function run_QS(is_qs_median_of_3) {
478509 do {
479510 i += 1 ;
480511
512+ let iColor ;
513+ if ( a [ i ] < pivot_value ( ) )
514+ iColor = partLColor ;
515+ else
516+ iColor = partRColor ;
481517 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+ } ) ;
484523
485524 } while ( a [ i ] < pivot_value ( ) ) ;
486525
487526 do {
488527 j -= 1 ;
489528
529+ let jColor ;
530+ if ( i <= j && pivot_value ( ) < a [ j ] )
531+ jColor = partRColor ;
532+ else
533+ jColor = partLColor ;
490534 chunker_add_if (
491535 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+ } ) ;
493541
494- } while ( i <= j && pivot_value ( ) < a [ j ] ) ;
542+ } while ( i < j && pivot_value ( ) < a [ j ] ) ;
495543
496544
497545 chunker_add_if ( QS_BOOKMARKS . SHARED_if_j_greater_i ) ;
@@ -507,12 +555,14 @@ export function run_QS(is_qs_median_of_3) {
507555 swapAction ( QS_BOOKMARKS . SHARED_swap_pivot_into_position , i ,
508556 is_qs_median_of_3 ? right - 1 : right
509557 ) ;
510-
558+ // XXX best make pivot sorted above and delete next chunk?
559+ /*
511560 chunker_add_if(
512561 QS_BOOKMARKS.SHARED_swap_pivot_into_position,
513562 (vis, cur_real_stack, cur_finished_stack_frames, cur_i, cur_j, cur_pivot_index, cur_depth) => {
514563 vis.array.sorted(cur_pivot_index);
515564 });
565+ */
516566
517567 return [ i , a ] ; // Return [pivot location, array partition_num_array]
518568 }
@@ -543,15 +593,19 @@ export function run_QS(is_qs_median_of_3) {
543593 // is a chunk at this recursion level as the first chunk in the
544594 // collapsed code for the recursive call
545595 // We no longer want to display 'pivot' or 'j' but want 'i'
596+ // (we use pivot_index but keep pivot for deselect)
546597 let pivot_index = undefined ;
547598 let j = undefined ;
548599 let i = pivot ;
549600 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 ) => {
551602 refresh_stack ( vis , cur_real_stack ,
552603cur_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 ) ;
553607 } ,
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 ) ;
555609
556610 QuickSort ( a , left , pivot - 1 , depth + 1 ) ;
557611
0 commit comments