Re: In-place list manipulation
- From: "jehugaleahsa@xxxxxxxxx" <jehugaleahsa@xxxxxxxxx>
- Date: Fri, 23 Nov 2007 10:52:41 -0800 (PST)
On Nov 22, 6:30 am, "Aki Tuomi" <cmo...@xxxxxxxxxxx> wrote:
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?
Aki Tuomi
With linked lists, auxilary lists are inexpensive. Essentially, you
Splice elements from one list into the other. The splicing simply
changes pointers, so no data is actually moved. Simply splice into the
auxilary until it contains elements of both lists, then splice the
result into one of the originals.
It should require no additional memory. You will find it to be faster
than in-place, as well.
Otherwise, you have to maintain the end of the merged portion E,
starting before the the first item. When the other list's item comes
first, splice/insert at E, otherwise just move the end forward. Be
aware that this modifies both lists.
If you are dealing with contiguous memory, good luck! It will be slow.
.
- Follow-Ups:
- Re: In-place list manipulation
- From: Gene
- Re: In-place list manipulation
- References:
- In-place list manipulation
- From: Aki Tuomi
- In-place list manipulation
- Prev by Date: Non-recursive chop and clone for Binary Search Tree - Solutions
- Next by Date: Re: In-place list manipulation
- Previous by thread: Re: In-place list manipulation
- Next by thread: Re: In-place list manipulation
- Index(es):
Relevant Pages
|