RE: Re: Friendly Challenge 2: sort by column

*To*: mathgroup at smc.vnet.net*Subject*: [mg35042] RE: [mg35029] Re: [mg35005] Friendly Challenge 2: sort by column*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>*Date*: Thu, 20 Jun 2002 23:54:40 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

> -----Original Message----- > From: gleam at flashmail.com [mailto:gleam at flashmail.com] To: mathgroup at smc.vnet.net > Sent: Thursday, June 20, 2002 8:14 AM > Subject: [mg35042] [mg35029] Re: [mg35005] Friendly Challenge 2: sort by column > > > > consequently I should add > > > > (5) > > > > a[[Ordering[a[[All, 3]]]]] > > Hartmut Wolf, thank you for participating in this challenge. > > Your #5 is one of the algorithms I'm using. It's the fastest I've > found for sorting by one column only, disregarding the rest. > > After moderate number of tests, not nearly up to your standards, but > enough to get a good start, I am prepared to observe that of your > functions that handle duplicates by using Rotate, your function #2 is > fastest. (Incidentally, this is the duplicate handling method I first > chose, and still prefer.) I have a function that, on my machine, is > usually faster than #2, sometimes considerably. I have a feeling you > can best it if you try. > > Examples: > > hw2[a_,p_]:=RotateRight[Sort[RotateLeft[a,{0,p-1}]],{0,p-1}] > > sort3[a_,p_]:= ??? (my function) > > In[284]:= > a = Table[Random[Integer, {0, 255}], {37}, {50}]; > First@Timing[Do[a~#~i, {i, 2, 50}]~Do~{70}] & /@ {sort3, hw2} > > Out[285]= > {1.54 Second, 1.86 Second} > > In[286]:= > a = Table[Random[], {500}, {300}]; > First@Timing[Do[a~#~i, {i, 2, 299, 11}]] & /@ {sort3, hw2} > > Out[287]= > {1.16 Second, 2.2 Second} > > Regards, > > Paul > > Paul, what do you mean "handling duplicates" exactly? With a={{77,27,57,53},{72,21,40,27},{96,76,87,27},{61,97,93,71},{43,80,21,87}}; and (ad=Join[Take[a,2],{ReplacePart[a[[2]],2,4]}, {ReplacePart[a[[2]],1,4]},Drop[a,2]])//MatrixForm then RotateRight[Sort[RotateLeft[ad,{0,2}]],{0,2}]//MatrixForm ad[[Ordering[ad[[All,3]]]]]//MatrixForm come out different. Both handle "duplicates", however, if you prefer a certain order of the duplicates, you have to specify it. Which columns, then, have the next subordinate sorting priority is it 4,...,1,2 or 1,2,4,... or should it be defined (a challenge)? e.g. ad[[Ordering[RotateLeft[ad,{0,2}]]]]//MatrixForm should give the same ordering as the double rotate solution, but is faster: hw5x[a_, p_] := a[[Ordering[RotateLeft[a, {0, 2}]]]] Timing[Do[r2 = Do[hw2[aa, i], {i, 2, 50}], {70}]] {3.585 Second, Null} Timing[Do[r5x = Do[hw5x[aa, i], {i, 2, 50}], {70}]] {2.183 Second, Null} r2 == r5x True But {1.54/1.86 , 2.183/3.585} {0.827957, 0.608926} is perhaps not enough evidence for 5x being faster than your (yet unknown) sort3. -- Hartmut