Re: In-place list manipulation
- From: Gene <gene.ressler@xxxxxxxxx>
- Date: Thu, 22 Nov 2007 12:42:38 -0800 (PST)
On Nov 22, 8:30 am, "Aki Tuomi" <cmo...@xxxxxxxxxxx> wrote:
When doing mergesort in place, I have ran into a following problem:On Nov 22, 8:30 am, "Aki Tuomi" <cmo...@xxxxxxxxxxx> wrote:
How to perform in-place interleave during merge without need of auxilliary
list or array?
I have two sets of indexes: a.start, a.stop and b.start and b.stop. These
contain start and stop indexes in the list where the data is. My question
is, how do I do merge the two into the list in-place?
Example:
List = {1,3,2,5,4,6,7,8,9}
a.start = 1
a.stop = 2
b.start = 3
b.stop = 4
a = {3,2}
b = {5,4}
merge(a,b,List)
List = {1,2,3,4,5,6,7,8,9}
I have solved this now by copying the two segments into temporary arrays and
then copy data back from there, but it does not really feel right. Any
ideas?
When doing mergesort in place, I have ran into a following problem:
How to perform in-place interleave during merge without need of auxilliary
list or array?
I have two sets of indexes: a.start, a.stop and b.start and b.stop. These
contain start and stop indexes in the list where the data is. My question
is, how do I do merge the two into the list in-place?
Example:
List = {1,3,2,5,4,6,7,8,9}
a.start = 1
a.stop = 2
b.start = 3
b.stop = 4
a = {3,2}
b = {5,4}
merge(a,b,List)
List = {1,2,3,4,5,6,7,8,9}
I have solved this now by copying the two segments into temporary arrays and
then copy data back from there, but it does not really feel right. Any
ideas?
As Richard said, in-place merge is very complicated except for the
case of linked lists, where the extra space is essentially allocated
in advance as the link fields. I've never seen in-place merging for
arrays implemented in production.
There _is_ a nice little optimization that many implementations seem
to miss. You just begin a merge step by copying out the lower half of
the array to be merged in advance and merge into the target. Ada is
below. If you don't know Ada, blah'First returns the lowest index of
the array blah. Indexing is with parens (as in FORTRAN). The
expression blah(lo .. hi) slices the array blah. List_Type below is a
"typedef" for an array of any ordered type. (digression: Ada is a
lovely language.)
procedure Sort(List : in out List_Type) is
Mid : Positive;
begin
if List'Length > 1 then
Mid := (List'First + List'Last) / 2;
Sort(List(List'First .. Mid));
Sort(List(Mid + 1 .. List'Last));
-- Merge.
declare
Temp : List_Type := List(List'First .. Mid);
P1 : Positive := List'First;
P2 : Positive := Mid + 1;
I : Positive := List'First;
begin
while P1 <= Mid loop
if P2 > List'Last or else Temp(P1) < List(P2) then
List(I) := Temp(P1);
P1 := P1 + 1;
else
List(I) := List(P2);
P2 := P2 + 1;
end if;
I := I + 1;
end loop;
end;
end if;
end Sort;
.
- References:
- In-place list manipulation
- From: Aki Tuomi
- In-place list manipulation
- Prev by Date: Re: mathematical combination
- Next by Date: x windows
- Previous by thread: Re: In-place list manipulation
- Next by thread: Re: In-place list manipulation
- Index(es):
Relevant Pages
|