Skip to content

Commit 94eaf3c

Browse files
committed
Mergesort with simple stack
1 parent cacf5ed commit 94eaf3c

File tree

7 files changed

+205
-79
lines changed

7 files changed

+205
-79
lines changed

src/algorithms/controllers/msort_arr_td.js

Lines changed: 135 additions & 71 deletions
Large diffs are not rendered by default.

src/algorithms/controllers/msort_arr_tdCollapseChunkPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export function isRecursionExpanded() {
6565

6666
// eslint-disable-next-line
6767
const { bookmark, pseudocode, collapse } = algorithm;
68-
return collapse.msort_arr_td.sort.QuicksortFirst ||
69-
collapse.msort_arr_td.sort.QuicksortSecond;
68+
return collapse.msort_arr_td.sort.MergesortL ||
69+
collapse.msort_arr_td.sort.MergesortR;
7070
}
7171

7272
// Trigger refresh of display when code is expanded/collapsed.

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
.array_1d {
44
flex-shrink: 0;
55

6+
.bottom_caption {
7+
// added for msort_arr_td
8+
caption-side: bottom;
9+
text-align: center;
10+
margin-top: 15px;
11+
font-size: 13px;
12+
color: var(--array-2d-row-col-value-color);
13+
font-family: Menlo, "Liberation Mono", Courier, monospace;
14+
}
15+
16+
.captionmsort_arr_td {
17+
caption-side: bottom;
18+
// caption-side: top;
19+
text-align: right;
20+
margin-top: 70px;
21+
// margin-top: 0px;
22+
}
23+
24+
25+
626
.row {
727
margin-bottom: m#{i}n(8px, 1vh);
828
position: relative;
@@ -168,4 +188,4 @@
168188
font-size: 0.8em;
169189
}
170190
}
171-
}
191+
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class Array1DRenderer extends Array2DRenderer {
5454

5555
renderData() {
5656

57+
// listOfNumbers used for stack caption in msort_arr_td
5758
// eslint-disable-next-line
58-
const { data, algo, stack, stackDepth } = this.props.data;
59+
const { data, algo, stack, stackDepth, listOfNumbers } = this.props.data;
5960

6061
const arrayMagnitudeScaleValue = 20; // value to scale an array e.g. so that the maximum item is 150px tall
6162

@@ -83,7 +84,17 @@ class Array1DRenderer extends Array2DRenderer {
8384
if (!this.props.data.arrayItemMagnitudes) {
8485
scaleY = () => 0;
8586
}
87+
// wrap output in table like 2D array so we can have a caption (for
88+
// msort_arr_td) XXX fix indentation
8689
return (
90+
<table
91+
className={switchmode(mode())}
92+
style={{
93+
marginLeft: -this.centerX * 2,
94+
marginTop: -this.centerY * 2,
95+
transform: `scale(${this.zoom})`,
96+
}}
97+
>
8798
<motion.div animate={{ scale: this.zoom }} className={switchmode(mode())}>
8899
{/* Values */}
89100
{data.map((row, i) => (
@@ -201,14 +212,24 @@ class Array1DRenderer extends Array2DRenderer {
201212
)}
202213
</div>
203214
<div>
204-
{stack && stack.length > 0 ? (
215+
{// Quicksort stuff
216+
stack && stack.length > 0 ? (
205217
this.maxStackDepth = Math.max(this.maxStackDepth, stackDepth),
206218
stackRenderer(stack, data[0].length, stackDepth, this.maxStackDepth)
207219
) : (
208220
<div />
209221
)}
210222
</div>
211223
</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>
212233
);
213234
}
214235
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@
179179
caption-side: bottom;
180180
}
181181

182+
.captionmsort_arr_td {
183+
caption-side: bottom;
184+
}
185+
186+
.captionmsort_arr_td {
187+
color: var(--array-2d-row-col-value-color);
188+
}
189+
182190
.captionBFS {
183191
caption-side: bottom;
184192
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Array2DRenderer extends Renderer {
5151
}
5252

5353
renderData() {
54-
// For DFSrec listOfNumbers is actually a list of pairs of numbers,
54+
// For DFSrec+msort_arr_td listOfNumbers is actually a list of pairs of numbers,
5555
// or strings such as '(2,5)'
5656
const { data, algo, kth, listOfNumbers, motionOn, hideArrayAtIdx } =
5757
this.props.data;
@@ -150,7 +150,7 @@ class Array2DRenderer extends Renderer {
150150
}
151151
}
152152
// XXX should use style sheets for formatting stack for DFS
153-
// and DFSrec
153+
// and DFSrec etc
154154
// - currently messing about attempting to make it look
155155
// better and more stack-like using float (deprecated)
156156
return (
@@ -301,6 +301,14 @@ class Array2DRenderer extends Renderer {
301301
<div style={{float:"right"}}>Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp; </div>
302302
</caption>
303303
)}
304+
{algo === 'msort_arr_td' && (
305+
<caption
306+
className={algo === 'msort_arr_td' ? styles.captionmsort_arr_td : ''}
307+
kth-tag="msort_arr_td_caption"
308+
>
309+
<div style={{float:"right"}}>Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp; </div>
310+
</caption>
311+
)}
304312
{algo === 'BFS' && (
305313
<caption
306314
className={algo === 'BFS' ? styles.captionBFS : ''}

src/components/DataStructures/Array/Array2DTracer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ class Array2DTracer extends Tracer {
226226
this.kth = k;
227227
}
228228

229+
// caption for arrays (undefined means no caption, [] may result in a blank
230+
// caption, depending on the algorithm - there may be extra text)
229231
setList(array) {
230-
this.listOfNumbers = array.join(', ');
232+
if (array)
233+
this.listOfNumbers = array.join(', ');
234+
else
235+
this.listOfNumbers = undefined;
231236
}
232237

233238
getKth() {

0 commit comments

Comments
 (0)