MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: split a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40624] Re: [mg40566] Re: split a list
  • From: Dr Bob <majort at cox-internet.com>
  • Date: Fri, 11 Apr 2003 02:03:46 -0400 (EDT)
  • References: <b70boj$883$1@smc.vnet.net> <b70nvi$9ji$1@smc.vnet.net> <200304100739.DAA24217@smc.vnet.net>
  • Reply-to: majort at cox-internet.com
  • Sender: owner-wri-mathgroup at wolfram.com

Orestis,

There's a bug in that.  The first argument of Position should be 'srt', not 
'r', so I don't know how you managed to time the code.

Also, your code returns two sorted lists, but the original request was to 
keep elements in original order in each sublist.

After fixing the bug, and realizing the answers don't quite match, your 
solution IS really fast, though -- almost as fast as Hartmut's compiled 
code.

orestis[r_, m_] := Module[{srt = Sort[Join[r, {m}]], pos},
    pos = Position[srt, m, 1, 1][[1, 1]];
    {Take[srt, pos - 1], Take[srt, {pos + 1, -1}]}]

test = trial@5
treat3 @@ test
hartmut @@ test
orestis @@ test

{{0.140517,0.940518,0.611916,0.853237,0.45228},0.111577}

{{},{0.140517,0.940518,0.611916,0.853237,0.45228}}
{{},{0.140517,0.940518,0.611916,0.853237,0.45228}}
{{},{0.140517,0.45228,0.611916,0.853237,0.940518}}

test = trial@1500000;
Timing[kuska @@ test;]
Timing[treat @@ test;]
Timing[treat2 @@ test;]
Timing[hartmut @@ test;]
Timing[kuska @@ test;]
Timing[treat3 @@ test;]
Timing[orestis @@ test;]

{13.032 Second,Null}
{13.047 Second,Null}
{12.875 Second,Null}
{2.594 Second,Null}
{13.312 Second,Null}
{13.109 Second,Null}
{3.672 Second,Null}

Bobby

On Thu, 10 Apr 2003 03:39:36 -0400 (EDT), Orestis Vantzos 
<atelesforos at hotmail.com> wrote:

> A faster function can be build based on the fact that Sort is
> extremely fast even for rather large lists of numbers:
>
> sortSplit[r_,m_]:=Module[{srt,pos},
> srt=Sort[r~Join~{m}];
> pos=Position[r,m,1,1][[1,1]];
> {Take[srt,pos-1], Take[srt, {pos+1,-1}]}]
>
> First, add m into the list and sort it.
> Now find the position of the first occurance of m and cut the
> (sorted)list there.
> On the left are all the numbers smallest than m and on the right the
> greater/equal. We throw away the element at the position of the cut;
> it is the m we added at the first step.
>
> This function is >3 times faster than Jens' function on my system.
>
> Orestis
>
> Jens-Peer Kuska <kuska at informatik.uni-leipzig.de> wrote in message 
> news:<b70nvi$9ji$1 at smc.vnet.net>...
>> Hi,
>>
>> lst = Table[Random[], {10000}];
>>
>> (* your version *)
>>
>> SplitAt[r_, m_] := Module[{tmp, u, v, i},
>> u = v = {};
>> For[i = 1, i <= Length[r], i++, tmp = r[[i]];
>> If[tmp > m, AppendTo[u, tmp], AppendTo[v, tmp]];];
>> {v, u}
>> ]
>>
>> (* a faster one *)
>>
>> SplitAt1[r_, m_] := {Select[r, # < m &], Select[r, # >= m &]}
>>
>>
>> Timing[SplitAt[lst, 0.5];]
>>
>> {24.22 Second, Null}
>>
>
>> and
>>
>> Timing[SplitAt1[lst, 0.5];]
>>
>> {0.6 Second, Null}
>>
>> Regards
>> Jens
>>
>> Roberto Brambilla wrote:
>> > > Hi,
>> > > I have a list (very long, thousands, and unsorted) of numbers 
>> r={n1,n2...}
>> > and for a given a number m  I want to split it in the two sublists
>> > (unsorted, same order) u={elements<m], v={elements>m}.
>> > Now I use this *old-style*  method:
>> > > u = v = {};
>> > For[i = 1, i <= Length[r], i++,
>> >   tmp = r[[i]];
>> >   If[tmp > m , AppendTo[u, tmp], AppendTo[v, tmp]];
>> >   ]
>> > > Any suggestion for a more efficient (and elegant) method?
>> > Also oneliners are well accepted.
>> > > Many thanks, Roberto
>> > > Roberto Brambilla
>> > CESI
>> > Via Rubattino 54
>> > 20134 Milano
>> > tel +39.02.2125.5875
>> > fax +39.02.2125.5492
>> > rlbrambilla at cesi.it
>
>



-- 
majort at cox-internet.com
Bobby R. Treat



  • Prev by Date: Re: '#1' raised to a power in result of a Solve[] call?
  • Next by Date: Re: RE: split a list
  • Previous by thread: Re: Re: split a list
  • Next by thread: Re: split a list