Re: easiest way to sort a list?
- To: mathgroup at smc.vnet.net
- To: mathgroup at smc.vnet.net
- Subject: [mg18357] Re: easiest way to sort a list?
- From: Colin Rose <colin at tri.org.au>
- Date: Wed, 30 Jun 1999 14:13:25 -0400
- Sender: owner-wri-mathgroup at wolfram.com
UnsortedUnion[lis_]:= Part[lis, Sort[Map[Position[lis, #][[1]] &, Union[lis]] // Flatten]] OrderedUnion[li_]:=Block[{i}, i[n_]:=(i[n]=Sequence[];n); i /@ li] Andrzej Kozlowski wrote: > Although in your example UnsortedUnion comes just ahead of OrderedUnion, > at the other extreme we get: > list1=Range[1000]; > OrderedUnion[list1];//Timing > {0.15 Second, Null} > UnsortedUnion[list1];//Timing > {1.91667 Second, Null} Actually, in v4, for this example, UnsortedUnion is still faster than OrderedUnion: In[4]:= OrderedUnion[list1]; // Timing Out[4]= {0.116667 Second, Null} In[5]:= UnsortedUnion[list1]; // Timing Out[5]= {0.0666667 Second, Null} Perhaps you are using an old version of Mathematica, in which Sort is much slower. Of course, the essence of your point is correct. That is, there are two types of large problems: * When the number of distinct elements is large: - here OrderedUnion is MUCH faster. * When the list is large, but the number of distinct elements is small. - here OrderedUnion is not faster. As an aside, in both cases, OrderedUnion may have memory problems. For instance, a list of length 500000 will 'quit' most people's kernels. Cheers Colin Colin Rose tr(I) - Theoretical Research Institute __________________________________________ colin at tri.org.au http://www.tri.org.au/