1
1
// Merge sort for arrays, top down
2
2
// Adapted code from Quicksort...
3
3
// XXX Could do with a good clean up!
4
- // Lots of crud, including abandonned attempt at QS-style stack display.
4
+ // Lots of crud, mostly abandonned attempt at QS-style stack display.
5
5
// Uses simple stack display like DFSrec; stack vanishes inside
6
6
// merge+copy because screen real-estate is limited and details of merge
7
7
// are independent of stack details anyway (may cause some surprise
8
8
// though)
9
9
10
10
import { msort_arr_td } from '../explanations' ;
11
11
12
- const is_qs_median_of_3 = false ;
13
- const run = run_msort ( is_qs_median_of_3 ) ;
12
+ const run = run_msort ( ) ;
14
13
15
14
export default {
16
15
explanation : msort_arr_td ,
@@ -19,7 +18,7 @@ export default {
19
18
} ;
20
19
21
20
22
- // Quicksort common code
21
+ // XXX (was) Quicksort common code
23
22
// Example of a recursive algorithm that could serve as a guide to
24
23
// implementing others. Some things to note:
25
24
// 1) A depth parameter is added to the recursive code and also passed
@@ -46,30 +45,28 @@ export default {
46
45
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
47
46
48
47
import {
49
- // XXX may not need isMergeExpanded? only needed if last chunk of
50
- // merge still as extra vars displayed
51
- isMergeExpanded ,
52
- isMergeCopyExpanded ,
53
- isRecursionExpanded ,
48
+ areExpanded ,
54
49
} from './collapseChunkPlugin' ;
55
50
56
- // MergeExpandedR true if Merge code is really/recursively expanded (ie,
57
- // visible in animation). Its possible for Merge to be expanded but the
58
- // outer MergeCopy not expanded.
59
- function isMergeExpandedR ( ) {
60
- return isMergeCopyExpanded ( ) && isMergeExpanded ( )
51
+ /////////////////////////////////////////////////////
52
+
53
+ // arrayB exists and is displayed only if MergeCopy is expanded
54
+ function isMergeCopyExpanded ( ) {
55
+ return areExpanded ( [ 'MergeCopy' ] ) ;
61
56
}
62
57
63
- // visualisation variable strings
64
- // For now we use a special case for i&j running off the left of the
65
- // array since we can't easily render them in the right place
66
- const VIS_VARIABLE_STRINGS = {
67
- i_left_index : 'i' ,
68
- j_right_index : 'j' ,
69
- i_eq_0 : 'i==0' ,
70
- j_eq_0 : 'j==0' ,
71
- pivot : 'pivot' ,
72
- } ;
58
+ // We don't strictly need isMergeExpanded: only needed if last chunk of
59
+ // merge still had extra vars displayed. Some code still needs
60
+ // isMergeCopyExpanded because it uses arrayB
61
+ function isMergeExpanded ( ) {
62
+ return areExpanded ( [ 'MergeCopy' , 'Merge' ] ) ; // MergeCopy contains Merge
63
+ }
64
+
65
+ // checks if either recursive call is expanded (otherwise stack is not
66
+ // displayed)
67
+ function isRecursionExpanded ( ) {
68
+ return areExpanded ( [ 'MergesortL' ] ) || areExpanded ( [ 'MergesortR' ] ) ;
69
+ }
73
70
74
71
// see stackFrameColour in index.js to find corresponding function mapping to css
75
72
const STACK_FRAME_COLOR = {
@@ -85,21 +82,6 @@ const STACK_FRAME_COLOR = {
85
82
// for simple DFS-like stack display
86
83
let simple_stack = [ ] ;
87
84
88
- /*
89
-
90
- (loc is line of code)
91
- bookmarks are loc identifiers into a REAL file
92
- REAL files are the pseudocode files
93
- (search \\B and find quicksort)
94
- keep up to date with this file
95
-
96
- MEDIAN3_ if loc is only in median of 3 quicksort
97
- SHARED_ if shared loc between MEDIAN3 and RIGHT_PIVOT
98
- RIGHT_P_ if an loc is only in the right pivot quicksort
99
-
100
- */
101
-
102
-
103
85
104
86
// ----------------------------------------------------------------------------------------------------------------------------
105
87
@@ -167,7 +149,7 @@ const unhighlightB = (vis, index, isPrimaryColor = true) => {
167
149
// ----------------------------------------------------------------------------------------------------------------------------
168
150
169
151
170
- // Nice to hide array B entirely if things are collapsed
152
+ // We hide array B entirely if things mergeCopy is collapsed
171
153
export function initVisualisers ( ) {
172
154
if ( isMergeCopyExpanded ( ) ) {
173
155
return {
@@ -202,7 +184,7 @@ export function initVisualisers() {
202
184
* @param {array } nodes array of numbers needs to be sorted
203
185
*/
204
186
205
- export function run_msort ( is_qs_median_of_3 ) {
187
+ export function run_msort ( ) {
206
188
207
189
return function run ( chunker , { nodes } ) {
208
190
// can't rename from nodes
@@ -329,11 +311,6 @@ export function run_msort(is_qs_median_of_3) {
329
311
derive_stack ( cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_pivot_index , cur_depth )
330
312
) ;
331
313
332
- // assignVarToA(vis, VIS_VARIABLE_STRINGS.i_left_index, cur_i);
333
- // assignVarToA(vis, VIS_VARIABLE_STRINGS.i_eq_0, cur_i_too_low);
334
- // assignVarToA(vis, VIS_VARIABLE_STRINGS.pivot, cur_pivot_index);
335
- // assignVarToA(vis, VIS_VARIABLE_STRINGS.j_right_index, cur_j);
336
- // assignVarToA(vis, VIS_VARIABLE_STRINGS.j_eq_0, cur_j_too_low);
337
314
} ;
338
315
339
316
@@ -356,7 +333,7 @@ export function run_msort(is_qs_median_of_3) {
356
333
// ----------------------------------------------------------------------------------------------------------------------------
357
334
358
335
function renderInMerge ( vis , a , b , cur_left , cur_ap1 , cur_ap2 , cur_bp , cur_max1 , cur_max2 , c_stk ) {
359
- if ( isMergeExpandedR ( ) ) {
336
+ if ( isMergeExpanded ( ) ) {
360
337
vis . array . set ( a , 'msort_arr_td' ) ;
361
338
// set_simple_stack(vis.array, c_stk);
362
339
assignVarToA ( vis , 'ap1' , cur_ap1 ) ;
@@ -519,32 +496,32 @@ c_stk) => {
519
496
for ( let i = cur_mid + 1 ; i <= cur_right ; i ++ ) {
520
497
unhighlight ( vis , i , false )
521
498
}
522
- if ( isMergeExpandedR ( ) ) {
499
+ if ( isMergeExpanded ( ) ) {
523
500
assignVarToA ( vis , 'left' , undefined ) ;
524
501
assignVarToA ( vis , 'ap1' , cur_left ) ;
525
502
highlight ( vis , cur_left , true ) ;
526
503
}
527
504
} , [ A , left , mid , right ] , depth ) ;
528
505
chunker . add ( 'max1' , ( vis , a , cur_left , cur_mid , cur_right ) => {
529
- if ( isMergeExpandedR ( ) ) {
506
+ if ( isMergeExpanded ( ) ) {
530
507
assignVarToA ( vis , 'mid' , undefined ) ;
531
508
assignVarToA ( vis , 'max1' , cur_mid ) ;
532
509
}
533
510
} , [ A , left , mid , right ] , depth ) ;
534
511
chunker . add ( 'ap2' , ( vis , a , cur_left , cur_mid , cur_right ) => {
535
- if ( isMergeExpandedR ( ) ) {
512
+ if ( isMergeExpanded ( ) ) {
536
513
assignVarToA ( vis , 'ap2' , cur_mid + 1 ) ;
537
514
highlight ( vis , cur_mid + 1 , true ) ;
538
515
}
539
516
} , [ A , left , mid , right ] , depth ) ;
540
517
chunker . add ( 'max2' , ( vis , a , cur_left , cur_mid , cur_right ) => {
541
- if ( isMergeExpandedR ( ) ) {
518
+ if ( isMergeExpanded ( ) ) {
542
519
assignVarToA ( vis , 'right' , undefined ) ;
543
520
assignVarToA ( vis , 'max2' , right ) ;
544
521
}
545
522
} , [ A , left , mid , right ] , depth ) ;
546
523
chunker . add ( 'bp' , ( vis , a , cur_left , cur_mid , cur_right ) => {
547
- if ( isMergeExpandedR ( ) ) {
524
+ if ( isMergeExpanded ( ) ) {
548
525
assignVarToB ( vis , 'bp' , left ) ;
549
526
}
550
527
} , [ A , left , mid , right ] , depth ) ;
@@ -573,7 +550,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
573
550
cur_bp , cur_max1 , cur_max2 , cur_stk , cur_left ) => {
574
551
renderInMerge ( vis , a , b , cur_left , cur_ap1 , cur_ap2 , cur_bp ,
575
552
cur_max1 , cur_max2 , cur_stk , cur_left ) ;
576
- if ( isMergeExpandedR ( ) ) {
553
+ if ( isMergeExpanded ( ) ) {
577
554
highlightB ( vis , cur_bp , false ) ;
578
555
}
579
556
} , [ A , B , ap1 , ap2 , bp , max1 , max2 , simple_stack , left ] , depth ) ;
@@ -582,7 +559,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
582
559
cur_max1 , cur_max2 , cur_stk , cur_left ) => {
583
560
renderInMerge ( vis , a , b , cur_left , cur_ap1 , cur_ap2 , cur_bp ,
584
561
cur_max1 , cur_max2 , cur_stk , cur_left ) ;
585
- if ( isMergeExpandedR ( ) ) {
562
+ if ( isMergeExpanded ( ) ) {
586
563
highlightB ( vis , cur_bp , false ) ;
587
564
}
588
565
} , [ A , B , ap1 , ap2 , bp , max1 , max2 , simple_stack , left ] , depth ) ;
@@ -599,7 +576,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
599
576
cur_bp , cur_max1 , cur_max2 , cur_stk , cur_left ) => {
600
577
renderInMerge ( vis , a , b , cur_left , cur_ap1 , cur_ap2 , cur_bp ,
601
578
cur_max1 , cur_max2 , cur_stk , cur_left ) ;
602
- if ( isMergeExpandedR ( ) ) {
579
+ if ( isMergeExpanded ( ) ) {
603
580
highlightB ( vis , cur_bp , false ) ;
604
581
}
605
582
} , [ A , B , ap1 , ap2 , bp , max1 , max2 , simple_stack , left ] , depth ) ;
@@ -608,7 +585,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
608
585
cur_max1 , cur_max2 , cur_stk , cur_left ) => {
609
586
renderInMerge ( vis , a , b , cur_left , cur_ap1 , cur_ap2 , cur_bp ,
610
587
cur_max1 , cur_max2 , cur_stk , cur_left ) ;
611
- if ( isMergeExpandedR ( ) ) {
588
+ if ( isMergeExpanded ( ) ) {
612
589
highlightB ( vis , cur_bp , false ) ;
613
590
}
614
591
} , [ A , B , ap1 , ap2 , bp , max1 , max2 , simple_stack , left ] , depth ) ;
@@ -629,7 +606,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
629
606
630
607
chunker . add ( 'CopyRest1' , ( vis , a , b , cur_left , cur_ap1 ,
631
608
cur_ap2 , cur_max1 , cur_max2 , cur_bp , c_stk ) => {
632
- if ( isMergeExpandedR ( ) ) {
609
+ if ( isMergeExpanded ( ) ) {
633
610
vis . array . set ( a , 'msort_arr_td' ) ;
634
611
// set_simple_stack(vis.array, c_stk);
635
612
// unhighlight(vis, cur_ap1, true);
@@ -666,7 +643,7 @@ cur_max2, cur_b, c_stk) => {
666
643
highlightB ( vis , i , false ) ;
667
644
}
668
645
}
669
- if ( isMergeExpandedR ( ) ) {
646
+ if ( isMergeExpanded ( ) ) {
670
647
if ( cur_ap2 < a . length ) {
671
648
unhighlight ( vis , cur_ap2 , true ) ;
672
649
assignVarToA ( vis , 'ap2' , undefined ) ;
@@ -693,7 +670,7 @@ cur_right, c_stk) => {
693
670
for ( let i = cur_left ; i <= cur_right ; i ++ ) {
694
671
highlight ( vis , i , false ) ;
695
672
}
696
- if ( isMergeExpandedR ( ) ) {
673
+ if ( isMergeExpanded ( ) ) {
697
674
assignVarToA ( vis , 'ap1' , undefined ) ;
698
675
assignVarToA ( vis , 'max1' , undefined ) ;
699
676
assignVarToA ( vis , 'ap2' , undefined ) ;
0 commit comments