Skip to content

Commit fea3789

Browse files
committed
msort polishing, style sheets used more
1 parent 94eaf3c commit fea3789

File tree

6 files changed

+69
-71
lines changed

6 files changed

+69
-71
lines changed

src/algorithms/controllers/msort_arr_td.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Merge sort for arrays, top down
2-
// XXX Migrating code from Quicksort...
3-
// Lots of crud, including abandonned attempt at QS-style stack display
4-
// Other stack display missing som refresh XXXXXXX
2+
// Adapted code from Quicksort...
3+
// XXX Could do with a good clean up!
4+
// Lots of crud, including abandonned attempt at QS-style stack display.
5+
// Uses simple stack display like DFSrec; stack vanishes inside
6+
// merge+copy because screen real-estate is limited and details of merge
7+
// are independent of stack details anyway (may cause some surprise
8+
// though)
59

610
import { msort_arr_td } from '../explanations';
711

@@ -703,6 +707,9 @@ cur_right, c_stk) => {
703707
// chunk after recursive call, as above, after adjusting
704708
// stack frames/depth etc
705709
}
710+
// XXX should we delete 'else' and always go to the 'Done' line
711+
// even for non-trivial array segments? (might need to
712+
// generalise (un)highlight code below
706713
else
707714
{
708715
chunker.add('Done', (vis, a, cur_left, cur_right) => {

src/algorithms/parameters/msort_arr_td.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Adapted from Quicksort - could rename a few things
2+
13
/* eslint-disable no-unused-vars */
24
import React, { useState, useEffect } from 'react';
35
import FormControlLabel from '@mui/material/FormControlLabel';
@@ -41,9 +43,8 @@ function MergesortParam() {
4143

4244

4345

44-
// XXX best case definitely not needed; best skip choice of cases
45-
// - not sure quite how currently
46-
// function for choosing the type of pivot (median of three)
46+
// XXX best case definitely not needed; could skip choice of cases
47+
// function for choosing the type of input
4748
const handleChange = (e) => {
4849
switch (e.target.name) {
4950
case 'sortedAsc':
@@ -148,18 +149,6 @@ function MergesortParam() {
148149
label="Sorted (descending)"
149150
className="checkbox"
150151
/>
151-
{/* create a checkbox for Median of Three */}
152-
<FormControlLabel
153-
control={
154-
<BlueRadio
155-
checked={QSCase.bestCase}
156-
onChange={handleChange}
157-
name="bestCase"
158-
/>
159-
}
160-
label="Ideal"
161-
className="checkbox"
162-
/>
163152
{/* render success/error message */}
164153
{message}
165154
</>

src/algorithms/pseudocode/msort_arr_td.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Mergesort(A, left, right) \\B Main
1515
\\In{
1616
mid <- (left + right)/2 \\B mid
1717
sort first half, A[left]..A[mid] \\Ref MergesortL
18-
\\Expl{ Sort elements in the first half of the array segment
18+
\\Expl{ Sort elements in the first half of the array segment.
1919
\\Expl}
2020
\\Note{ This should be animated in one step if not expanded
2121
\\Note}
2222
sort second half, A[mid+1]..A[right] \\Ref MergesortR
23-
\\Expl{ Sort elements in the second half of the array segment
23+
\\Expl{ Sort elements in the second half of the array segment.
2424
\\Expl}
2525
\\Note{ This should be animated in one step if not expanded
2626
\\Note}
@@ -58,13 +58,17 @@ MergeCopy
5858
Merge(A, left, mid, right, B) \\Ref Merge
5959
\\Expl{ Takes two sorted array segments, A[left..mid] and A[mid+1..right],
6060
and merges them together to form a single sorted array segment
61-
in temporary array B[left..right]
61+
in temporary array B[left..right].
62+
The animation shows values being deleted from A since they
63+
are no longer needed (they are actually still there).
6264
\\Expl}
6365
Copy merged elements back to A \\B copyBA
6466
\\Expl{ Copy elements from B[left..right] back to A[left..right].
6567
Copying can be reduced by merging
6668
from A to B and from B to A in alternate levels of recursion -
6769
a slightly more tricky coding.
70+
The animation shows values being deleted from B since they
71+
are no longer needed (they are actually still there).
6872
\\Expl}
6973
\\Note{ Might be better to move above to overview.
7074
\\Note}
@@ -75,37 +79,39 @@ Merge
7579
ap1 <- left \\B ap1
7680
max1 <- mid \\B max1
7781
\\Expl{ ap1 scans through the segment A[left..mid], "pointing at" or
78-
indexing elements of this array segment we copy from
82+
indexing elements of this array segment we copy from.
7983
\\Expl}
8084
ap2 <- max1+1 \\B ap2
8185
max2 <- right \\B max2
8286
\\Expl{ ap2 scans through the segment A[mid+1..right], "pointing at" or
83-
indexing elements of this array segment we copy from
87+
indexing elements of this array segment we copy from.
8488
\\Expl}
8589
bp <- ap1 \\B bp
8690
\\Expl{ bp scans through the segment B[left..right], "pointing at" or
87-
indexing elements of this array segment we copy to
91+
indexing elements of this array segment we copy to.
8892
\\Expl}
8993
while both A segments still have elements to copy \\Ref MergeWhile
9094
\\Expl{ we scan through both A segments from left to right by
91-
incrementing ap1 and ap2, copying to B as we go
95+
incrementing ap1 and ap2, copying to B as we go.
96+
The animation shows values being deleted from A since they
97+
are no longer needed (they are actually still there).
9298
\\Expl}
9399
\\In{
94100
copy the smaller A element, increment its pointer and bp \\Ref CopySmaller
95-
\\Expl{ The smaller of A[ap1] and A[ap2] is copied to B[bp]
101+
\\Expl{ The smaller of A[ap1] and A[ap2] is copied to B[bp].
96102
\\Expl}
97103
\\In}
98104
copy any remaining elements from A to B \\Ref CopyRest
99105
\\Expl{ One of the A segments will have been completely copied;
100-
the other has uncopied elements
106+
the other has uncopied elements.
101107
\\Expl}
102108
\\Code}
103109
104110
\\Code{
105111
MergeWhile
106112
while ap1 <= max1 and ap2 <= max2 \\B MergeWhile
107113
\\Expl{ Elements up to max1/max2 must be copied; those before
108-
ap1/ap2 have been copied already
114+
ap1/ap2 have been copied already.
109115
\\Expl}
110116
\\Code}
111117
@@ -114,6 +120,9 @@ CopySmaller
114120
if A[ap1] < A[ap2] \\B findSmaller
115121
\\In{
116122
B[bp] <- A[ap1] \\B copyap1
123+
\\Expl{ The animation shows the value being deleted from A[ap1] since it
124+
is no longer needed (it is actually still there).
125+
\\Expl}
117126
ap1 <- ap1+1 \\B ap1++
118127
bp <- bp+1 \\B bp++
119128
\\Note{ Clearer to duplicate this in then and else branches(?)
@@ -122,6 +131,9 @@ CopySmaller
122131
else
123132
\\In{
124133
B[bp] <- A[ap2] \\B copyap2
134+
\\Expl{ The animation shows the value being deleted from A[ap2] since it
135+
is no longer needed (it is actually still there).
136+
\\Expl}
125137
ap2 <- ap2+1 \\B ap2++
126138
bp <- bp+1 \\B bp++_2
127139
\\In}
@@ -134,7 +146,10 @@ CopyRest
134146
\\Note}
135147
copy A[ap2..max2] to B[bp..] \\B CopyRest2
136148
\\Expl{ One of these copy steps will do nothing because one of the
137-
A segments will be empty
149+
A segments will be empty. If ap2 is not shown in the animation
150+
it is max2+1, off the end of the array.
151+
The animation shows values being deleted from A since they
152+
are no longer needed (they are actually still there).
138153
\\Expl}
139154
\\Code}
140155

src/components/DataStructures/Array/Array1DRenderer/index.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class Array1DRenderer extends Array2DRenderer {
9595
transform: `scale(${this.zoom})`,
9696
}}
9797
>
98+
<tbody>
9899
<motion.div animate={{ scale: this.zoom }} className={switchmode(mode())}>
99100
{/* Values */}
100101
{data.map((row, i) => (
@@ -107,18 +108,6 @@ class Array1DRenderer extends Array2DRenderer {
107108
justifyContent: 'center',
108109
}}
109110
>
110-
{/*
111-
Pivot Line Rendering:
112-
- This JSX code renders the visual line over the pivot element in 1D arrays, e.g., QuickSort.
113-
- The feature is currently disabled. To re-enable:
114-
1. Uncomment the following JSX.
115-
2. Uncomment the `.pivotLine` style in Array1DRenderer.module.scss.
116-
*/}
117-
118-
{/*
119-
{row.filter((col) => col.variables.includes('pivot')).map((col)=><div className={styles.pivotLine} style={{
120-
bottom: `max(var(--array-1d-minimum-height), ${this.toString(scaleY(col.value))}vh)`}}/>)}
121-
*/}
122111
{row.map((col) => (
123112
<motion.div
124113
layout
@@ -153,7 +142,7 @@ class Array1DRenderer extends Array2DRenderer {
153142
))}
154143

155144
<div>
156-
{/* Indexes */}
145+
{/* Indexes XXX maybe avoid for arrayB in Merge sort? */}
157146
<div
158147
className={styles.row}
159148
style={{ display: 'flex', justifyContent: 'space-between' }}
@@ -221,15 +210,16 @@ class Array1DRenderer extends Array2DRenderer {
221210
)}
222211
</div>
223212
</motion.div>
224-
{algo === 'msort_arr_td' && listOfNumbers && (
225-
<caption
226-
className={algo === 'msort_arr_td' ? styles.captionmsort_arr_td : ''}
227-
kth-tag="msort_arr_td_caption"
228-
>
229-
<div style={{float:"right"}}>Call stack (left,right):&emsp; {listOfNumbers}&emsp;&emsp; </div>
230-
</caption>
231-
)}
232-
</table>
213+
</tbody>
214+
{algo === 'msort_arr_td' && listOfNumbers && (
215+
<caption
216+
className={algo === 'msort_arr_td' ? styles.captionmsort_arr_td : ''}
217+
kth-tag="msort_arr_td_caption"
218+
>
219+
<div style={{float:"right"}}>Call stack (left,right):&emsp; {listOfNumbers}&emsp;&emsp; </div>
220+
</caption>
221+
)}
222+
</table>
233223
);
234224
}
235225
}

src/components/DataStructures/Array/Array2DRenderer/Array2DRenderer.module.scss

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,31 @@
171171
}
172172
}
173173
}
174-
.captionDFS {
175-
caption-side: bottom;
176-
}
177174

178-
.captionDFSrec {
179-
caption-side: bottom;
180-
}
181-
182-
.captionmsort_arr_td {
175+
.captionDFS {
183176
caption-side: bottom;
184-
}
185-
186-
.captionmsort_arr_td {
177+
text-align: right;
178+
margin-top: -12px; // need to squeeze a bit
187179
color: var(--array-2d-row-col-value-color);
188-
}
180+
}
189181

190182
.captionBFS {
191183
caption-side: bottom;
192-
}
193-
194-
.captionDFS {
184+
text-align: center;
185+
margin-top: -12px; // need to squeeze a bit
195186
color: var(--array-2d-row-col-value-color);
196187
}
197188

198189
.captionDFSrec {
190+
caption-side: bottom;
191+
margin-top: -12px; // doesn't hurt to squeeze a bit like above
192+
text-align: right;
199193
color: var(--array-2d-row-col-value-color);
200194
}
201195

202-
.captionBFS {
196+
// redefined in 1D array
197+
.captionmsort_arr_td {
198+
caption-side: bottom;
199+
text-align: right;
203200
color: var(--array-2d-row-col-value-color);
204-
}
201+
}

src/components/DataStructures/Array/Array2DRenderer/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,23 @@ class Array2DRenderer extends Renderer {
290290
className={algo === 'DFS' ? styles.captionDFS : ''}
291291
kth-tag="dfs_caption"
292292
>
293-
<div style={{float:"right"}}>Nodes (stack):&emsp; {listOfNumbers}&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; </div>
293+
Nodes (stack):&emsp; {listOfNumbers}&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
294294
</caption>
295295
)}
296296
{algo === 'DFSrec' && (
297297
<caption
298298
className={algo === 'DFSrec' ? styles.captionDFSrec : ''}
299299
kth-tag="dfsrec_caption"
300300
>
301-
<div style={{float:"right"}}>Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp; </div>
301+
Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp;
302302
</caption>
303303
)}
304304
{algo === 'msort_arr_td' && (
305305
<caption
306306
className={algo === 'msort_arr_td' ? styles.captionmsort_arr_td : ''}
307307
kth-tag="msort_arr_td_caption"
308308
>
309-
<div style={{float:"right"}}>Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp; </div>
309+
Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp;
310310
</caption>
311311
)}
312312
{algo === 'BFS' && (

0 commit comments

Comments
 (0)