Skip to content

Commit 53bd572

Browse files
committed
Mergesort fixes
1 parent 5bb52b0 commit 53bd572

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

src/algorithms/controllers/msort_lista_td.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,8 @@ cur_right, c_stk) => {
837837

838838
// XXXXXXXXX
839839
Indices = ['i'];
840-
Heads = ['Head(i) (data)'];
841-
Tails = ['Tail(i) (next)'];
840+
Heads = ['i.head (data)'];
841+
Tails = ['i.tail (next)'];
842842
simple_stack = [];
843843

844844
for (let i = 1; i<entire_num_array.length; i++) {

src/algorithms/pseudocode/msort_lista_td.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Mergesort(L, len) \\B Main
3838
\\Code{
3939
split
4040
Mid <- mid point of L \\Ref scan
41-
R <- tail(Mid) // R starts after Mid
42-
tail(Mid) <- Null // truncate L after Mid \\B tail(Mid)<-Null
41+
R <- Mid.tail // R starts after Mid
42+
Mid.tail <- Null // truncate L after Mid \\B tail(Mid)<-Null
4343
\\Code}
4444
4545
\\Code{
@@ -49,7 +49,7 @@ scan
4949
\\Expl}
5050
for i = 1 to len/2 - 1 // while not at middle
5151
\\In{
52-
Mid <- tail(Mid) \\B MidNext
52+
Mid <- Mid.tail \\B MidNext
5353
\\Expl{ Skip to next element
5454
\\Expl}
5555
\\In}
@@ -81,17 +81,33 @@ MergesortR
8181
Merge
8282
Initialise M with minimum of L and R \\Ref initM
8383
\\Expl{ Set M to the input list with the smallest first element and
84-
skip over (delete) that element for that input list.
84+
skip over (delete) that element for that input list. At this point,
85+
we are only interested in the first element of M, M.head -
86+
conceptually it is a single element list. We could
87+
set M.tail to be the empty list, but it will be reset to another
88+
value later so this is not necessary.
8589
\\Expl}
8690
E <- M // E is the end element of M \\B E
91+
\\Expl{ With the normal representation of lists, M
92+
will be a pointer to a list cell and E will be a pointer to the same
93+
list cell. In the while loop below, the elements of M from its
94+
first element up to the element pointed to by E will be the elements
95+
of L and R that have been skipped over. The remaining elements of M
96+
can conceptually be ignored - we could set E.tail to be the empty
97+
list. The value of E.tail is not used in the whie loop and is reset
98+
after the while loop.
99+
\\Expl}
100+
\\Note{ Best color the elements of M up to E differently from the rest
101+
(which are elements of L or R as well).
102+
\\Note}
87103
while L != Null && R != Null \\B whileNotNull
88104
\\Expl{ Scan through L and R, appending elements to M. E is always the
89-
end element of M, and L and R are the remaining inputs that have
105+
(conceptual) end element of M, and L and R are the remaining inputs that have
90106
not yet been appended.
91107
\\Expl}
92108
\\In{
93109
append the smaller input element to M, advance pointers \\Ref CopySmaller
94-
\\Expl{ The smaller of head(L) and head(R) is appended to M.
110+
\\Expl{ The smaller of L.head and R.head is appended to M.
95111
\\Expl}
96112
\\In}
97113
append any remaining elements onto M \\Ref CopyRest
@@ -102,18 +118,18 @@ Merge
102118
103119
\\Code{
104120
initM
105-
if head(L) < head(R) \\B compareHeads
121+
if L.head < R.head \\B compareHeads
106122
\\In{
107123
M <- L \\B M<-L
108-
L <- tail(L) \\B L<-tail(L)
124+
L <- L.tail \\B L<-tail(L)
109125
\\Expl{ M will contain the first element of L so we skip L to
110126
its next element.
111127
\\Expl}
112128
\\In}
113129
else
114130
\\In{
115131
M <- R \\B M<-R
116-
R <- tail(R) \\B R<-tail(R)
132+
R <- R.tail \\B R<-tail(R)
117133
\\Expl{ M will contain the first element of R so we skip R to
118134
its next element.
119135
\\Expl}
@@ -122,29 +138,29 @@ initM
122138
123139
\\Code{
124140
CopySmaller
125-
if head(L) <= head(R) \\B findSmaller
141+
if L.head <= R.head \\B findSmaller
126142
\\In{
127-
tail(E) <- L // append L element to M
143+
E.tail <- L // append L element to M
128144
E <- L // E <- end element of M
129-
L <- tail(L) // skip element in L that has been appended \\B popL
145+
L <- L.tail // skip element in L that has been appended \\B popL
130146
\\In}
131147
else
132148
\\In{
133-
tail(E) <- R // append R element to M
149+
E.tail <- R // append R element to M
134150
E <- R // E <- end element of M
135-
R <- tail(R) // skip element in R that has been appended \\B popR
151+
R <- R.tail // skip element in R that has been appended \\B popR
136152
\\In}
137153
\\Code}
138154
139155
\\Code{
140156
CopyRest
141157
if L == Null
142158
\\In{
143-
tail(E) <- R // append extra R elements to M \\B appendR
159+
E.tail <- R // append extra R elements to M \\B appendR
144160
\\In}
145161
else
146162
\\In{
147-
tail(E) <- L // append extra L elements to M \\B appendL
163+
E.tail <- L // append extra L elements to M \\B appendL
148164
\\In}
149165
\\Code}
150166

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
.array_1d {
44
flex-shrink: 0;
55

6+
// default caption is at top; some code may use undefined styles for
7+
// captions - should fix XXX
8+
.top_caption {
9+
caption-side: top;
10+
text-align: center;
11+
margin-bottom: 5px;
12+
font-size: 18px;
13+
// color: var(--array-2d-row-col-value-color);
14+
// font-family: Menlo, "Liberation Mono", Courier, monospace;
15+
}
16+
617
.bottom_caption {
7-
// added for msort_arr_td
818
caption-side: bottom;
919
text-align: center;
1020
margin-top: 15px;
@@ -13,10 +23,11 @@
1323
font-family: Menlo, "Liberation Mono", Courier, monospace;
1424
}
1525

16-
.captionmsort_arr_td {
26+
// used for some merge sorts and recursive DFS
27+
.simple_stack_caption {
1728
caption-side: bottom;
1829
// caption-side: top;
19-
text-align: right;
30+
text-align: right; // so stack grows to the left
2031
margin-top: 70px;
2132
// margin-top: 0px;
2233
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Array1DRenderer extends Array2DRenderer {
9191
// wrap output in table like 2D array so we can have a caption (for
9292
// msort_arr_td) XXX fix indentation
9393
return (
94+
<div>
9495
<table
9596
className={switchmode(mode())}
9697
style={{
@@ -258,31 +259,28 @@ class Array1DRenderer extends Array2DRenderer {
258259
</div>
259260
</motion.div>
260261
</tbody>
261-
{ // XXX I've given up trying to avoid this warning...
262-
// "Whitespace text nodes cannot appear as a child of <table>. Make
263-
// sure you don't have any extra whitespace between tags on each
264-
// line of your source code." Similariy div inside tbody.
265-
algo === 'msort_arr_td' && listOfNumbers && (
262+
{ algo === 'msort_arr_td' && listOfNumbers && (
266263
<caption
267-
className={styles.captionmsort_arr_td}
264+
className={styles.simple_stack_caption}
268265
kth-tag="msort_arr_td_caption"
269-
> Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp; </caption>)
266+
> Call stack (left,right):&emsp; {listOfNumbers}&emsp;&emsp; </caption>)
270267
}
271268
{
272269
algo === 'msort_arr_bup' && listOfNumbers && (
273270
<caption
274-
className={styles.captionmsort_arr_bup}
271+
className={styles.top_caption}
275272
kth-tag="msort_arr_bup_caption"
276273
> &emsp; {listOfNumbers}&emsp;&emsp; </caption>)
277274
}
278275
{
279276
algo === 'msort_arr_nat' && listOfNumbers && (
280277
<caption
281-
className={styles.captionmsort_arr_nat}
278+
className={styles.top_caption}
282279
kth-tag="msort_arr_nat_caption"
283280
> &emsp; {listOfNumbers}&emsp;&emsp; </caption>)
284281
}
285282
</table>
283+
</div>
286284
);
287285
}
288286
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@
232232
color: var(--array-2d-row-col-value-color);
233233
}
234234

235-
// redefined in 1D array
235+
// Following not really used - these algorithms use 1D arrays and styles are
236+
// defined separately
236237
.captionmsort_arr_td {
237238
caption-side: bottom;
238239
text-align: right;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,6 @@ class Array2DRenderer extends Renderer {
441441
Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp;
442442
</caption>
443443
)}
444-
{algo === 'msort_arr_td' && (
445-
<caption
446-
className={algo === 'msort_arr_td' ? styles.captionmsort_arr_td : ''}
447-
kth-tag="msort_arr_td_caption"
448-
>
449-
Call stack (n,p):&emsp; {listOfNumbers}&emsp;&emsp;
450-
</caption>
451-
)}
452444
{algo === 'msort_lista_td' && listOfNumbers && (
453445
<caption
454446
className={algo === 'msort_lista_td' ? styles.captionmsort_lista_td : ''}

0 commit comments

Comments
 (0)