comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison <dennison@telepath.com>
Subject: Re: list strawman
Date: Fri, 11 Jan 2002 17:03:54 GMT
Date: 2002-01-11T17:03:54+00:00	[thread overview]
Message-ID: <3C3F1A0F.2000706@telepath.com> (raw)
In-Reply-To: 3c3eb272$0$282$626a54ce@news.free.fr

Jean-Marc Bourguet wrote:

> dennison@telepath.com (Ted Dennison) writes:
> 
> 
>>...OK it looks like the work involved in doing *all* the splits using
>>linear searches is (n/2 + 2(n/4) + 4(n/8) ....) where n>1. Since there
>>is one of these factors for each "level", and there are logn levels,
>>that should work out to roughly (n/2)logn, or O(n logn). So you are
>>right that it is O(N log N) worst case. However, you are wrong about
>>the best case. With a linked-list, that is *also* O(N log N).


>    procedure Split(Head: Node_Ptr; L1, L2: out Node_Ptr) is
>       Current : Node_Ptr := Head;
>       Last    : Node_Ptr := Null;
>       Other   : Node_Ptr := Null;
>    begin -- Split
>       L1 := null
>       L2 := null;
>       while Current /= null loop
>          declare
>             Next : Node_Ptr := Current.Next;
>          begin
>             Append(Current, Last, Other, L1, L2);
>             Current := Next;
>          end;
>       end loop;
>       Last.Next := null;
>       if Other /= null then
>          Other.Next := null
>       end if;            
>    end Split;
> Each split and Merge phase are O(n).  If the list is sorted, only


That's right, each call to those routines is O(n). However, for a 
mergesort you don't do just one split or merge. At the top level you do 
1, and the next level you do 2, at the next 4, and so on.

It doesn't look like the algorithm you presented does this, but then it 
doesn't look like a sort either. It only calls split once, and then 
merge. Its actually a O(n) algorithm. Too bad it doesn't actually sort, 
or you'd be up for some kind of prize. :-)

Now lets assume it *did* sort, by having split call itself recursively 
after the split if there are multiple elements, then perform a merge 
when its done. The way this modified Split algorithm works, you 
essentially do one full linear search through every list element at 
every recusion level that the list has. Since you divide the list in 
half at each level, there are log N division levels. Thus this algorithm 
you presented is O(n log n), best, worst and average case, just like I 
was saying. The algorithm I assumed for my previous mergesort split 
calculations was a bit quicker, in that I only searched through *half* 
the list at each level, but the time behavior is the same.






  reply	other threads:[~2002-01-11 17:03 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-06 20:55 list strawman Stephen Leake
2002-01-07 15:56 ` Ted Dennison
2002-01-07 15:57   ` Ted Dennison
2002-01-07 16:33   ` Stephen Leake
2002-01-07 16:37     ` Stephen Leake
2002-01-07 19:31       ` Ted Dennison
2002-01-07 19:26     ` Ted Dennison
2002-01-07 22:05       ` Stephen Leake
2002-01-07 22:51         ` Ted Dennison
2002-01-08  0:48           ` Steven Deller
2002-01-08 15:32             ` Ted Dennison
2002-01-08 15:43               ` Jean-Marc Bourguet
2002-01-08 17:07                 ` Ted Dennison
2002-01-08 17:21                   ` Jean-Marc Bourguet
2002-01-08 19:12                     ` Ted Dennison
2002-01-09  8:09                       ` Jean-Marc Bourguet
2002-01-09 18:37                         ` Ted Dennison
2002-01-11  9:37                           ` Jean-Marc Bourguet
2002-01-11 17:03                             ` Ted Dennison [this message]
2002-01-11 17:47                               ` Jeffrey Carter
2002-01-12 15:10                               ` Jean-Marc Bourguet
2002-01-13 10:18                                 ` Jean-Marc Bourguet
2002-01-14 16:02                                 ` Ted Dennison
2002-01-14 16:22                                   ` Jean-Marc Bourguet
2002-01-08 19:57                     ` Steven Deller
2002-01-08 19:54                 ` Steven Deller
2002-01-08 19:54               ` Steven Deller
2002-01-08 20:46                 ` Ted Dennison
2002-01-08 21:21                   ` Stephen Leake
2002-01-08 21:49                     ` Ted Dennison
2002-01-09  9:21                       ` Thomas Wolf
2002-01-09 15:20                         ` Ted Dennison
2002-01-09 15:53                           ` Stephen Leake
2002-01-09 21:21                             ` Ted Dennison
2002-01-09 17:42                         ` Mark Lundquist
2002-01-09 21:02                           ` Jeffrey Carter
2002-01-10  8:47                             ` Thomas Wolf
2002-01-11 17:38                               ` Jeffrey Carter
2002-01-11 21:52                                 ` Chad Robert Meiners
2002-01-12  5:45                                   ` Jeffrey Carter
2002-01-12 22:20                                     ` Chad R. Meiners
2002-01-13 17:03                                       ` Jeffrey Carter
2002-01-13 23:47                                         ` Chad R. Meiners
2002-01-14  1:32                                           ` Ted Dennison
2002-01-14  5:12                                           ` Jeffrey Carter
2002-01-14  5:12                                           ` Jeffrey Carter
2002-01-10 14:39                           ` Ted Dennison
2002-01-11  5:34                             ` Mark Biggar
2002-01-12 12:20                               ` Simon Wright
2002-01-14 14:53                                 ` Matthew Heaney
2002-01-16  5:56                                   ` Simon Wright
2002-01-18  9:15                           ` Overridability of _private_ predefined "=" [was Re: list strawman] Vincent Marciante
2002-01-19 16:58                             ` Vincent Marciante
2002-01-19 22:42                               ` Nick Roberts
2002-01-09  3:10                     ` list strawman Ted Dennison
2002-01-09 19:09                       ` Ted Dennison
2002-01-08 21:26               ` Georg Bauhaus
2002-01-08 22:13                 ` Ted Dennison
2002-01-09 20:52               ` Jeffrey Carter
2002-02-17 15:04 ` Florian Weimer
2002-02-17 15:05 ` Florian Weimer
2002-02-18  1:43   ` Stephen Leake
2002-02-18  8:57     ` Florian Weimer
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox