writes:
>> but `pos2` and `pos3` *change* throughout the merge loop. What you're
>> comparing them to is supposed to stay fixed.
>
> where?
Here's the code you had:
~~~~ {.cpp}
for(i = 0; i < length; i++)
{
if(pos1 < pos2 && ( pos2 == pos3 || max(in[pos1], in[pos2]) == in[pos1]))
{
space[i] = in[pos1];
pos1++;
}
else
{
space[i] = in[pos2];
pos2++;
}
}
~~~~
Obviously, `pos2` is changing throughout that loop, because it says
`pos2++`. (This has nothing to do with the recursion.) In the `if`, what
you're comparing `pos1` to should stay the same.
Here it is with my bug fix:
~~~~ {.cpp}
for(i = 0; i < length; i++)
{
if(pos1 < left+mpt && ( pos2 == right || max(in[pos1], in[pos2]) == in[pos1]))
{
space[i] = in[pos1];
pos1++;
}
else{
space[i] = in[pos2];
pos2++;
}
}
~~~~
The `pos1` and `pos2` are indeed "cursors" working through the two
lists. Both of them move. The `pos1` gets to the end of its list when it
equals `left+mpt`. The `pos2` gets to the end of its list when it equals
`right`.
CL
--==-=-=
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
1.0, user-scalable=3Dyes">
Ruben Safir ruben.safir-at-my.liu=
.edu writes:
but pos2
and pos3
change throughout t=
he merge loop. What you=E2=80=99re comparing them to is supposed to stay fi=
xed.
where?
Here=E2=80=99s the code you had:
ceCode cpp">for(i =3D 0=
; i < length; i++)
{
if(pos1 < pos2 && ( pos2 =3D=3D pos=
3 || max(in[pos1], in[pos2]) =3D=3D in[pos1]))
{
space[i] =3D in[pos1];
pos1++;
}
else
{
space[i] =3D in[pos2];
pos2++;
}
}
Obviously, pos2
is changing throughout that loop, because i=
t says pos2++
. (This has nothing to do with the recursion.) In=
the if
, what you=E2=80=99re comparing pos1
to sh=
ould stay the same.
Here it is with my bug fix:
ceCode cpp">for(i =3D 0=
; i < length; i++)
{
if(pos1 < left+mpt && ( pos2 =3D=3D=
right || max(in[pos1], in[pos2]) =3D=3D in[pos1]))
{
space[i] =3D in[pos1];
pos1++;
}
else{
space[i] =3D in[pos2];
pos2++;
}
}
The pos1
and pos2
are indeed =E2=80=9Ccursors=
=E2=80=9D working through the two lists. Both of them move. The pos1<=
/code> gets to the end of its list when it equals left+mpt
. Th=
e pos2
gets to the end of its list when it equals right<=
/code>.
CL
--==-=-=--
--=-=-=--
--===-=-=
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQEcBAEBCAAGBQJYJcnIAAoJEGuLsz1PMbCLrFIIAMLe6/U0Xi6uQK+u2p1m/iLX
EosPfn3dI3sqDuOg6XYIAJhm9+cp6Ab1/swMCQJgpVBPKHWLfWe4wwSKR06FxqMQ
qW0eK7v8PqftCi17imOdP7sa6VPQS9s7mn3XfyFZ4qh5LO1L7zqZXOSWldrEAZps
9kweHKn2Y4OZX42RzGcZGJ0j+srdAz4VAMQy8ISHoxLf7O3RmMTIF4lKBw8SByDL
D/rw7Lpa/7GzkB/pq2/xzloFpK3mMLXjpq7Ymi3mm1g+/bkO6WmpJcRWzPXpioJ+
KNWnpiZX6bxXLPl4poOVLw8TV6Xda4R8O7bB+ttFzk9DgOf8/UpxTjCPW5pqnPY=
=DsOP
-----END PGP SIGNATURE-----
--===-=-=--
--===============1467537476==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Learn mailing list
Learn-at-nylxs.com
http://lists.mrbrklyn.com/mailman/listinfo/learn
--===============1467537476==--