Re: Is Sort stable?
- To: mathgroup at smc.vnet.net
- Subject: [mg39840] Re: Is Sort stable?
- From: "J.L.Garrido" <garrido at ruth.upc.es>
- Date: Sat, 8 Mar 2003 02:50:40 -0500 (EST)
- References: <b41bv1$pea$1@smc.vnet.net> <b49m8q$fag$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
En/Na "J.L.Garrido" ha escrit:
> En/Na Roland Nilsson ha escrit:
>
> > Hi,
> >
> > --Short form:
> > Is the sorting algorithm implemented by Sort[] stable?
> >
> > --Long form:
> > I'm doing a thingy where I need to take out subsets of data and look at them
> > individually. I have a labeled data set, with labels being a vector of 1,2
> > ... denoting class membership, and I've tried to pick out subset using
> > either e.g.
> >
> > Extract[data, Position[labels,1]]
> > => the data points in "class 1"
> >
> > or using Sort to sort data according to labels (so I get e.g. {1,1,1,1, ...
> > 2,2,2, ... 3,3,3}, and which is nice for plotting data sets. This work ok,
> > but it seems like Sort[] is rearranging the data points within classes
> > different from Extract[]. Does anyone know i Is the Sort[] algorithm stable?
> > Could there be something else lurking here?
> >
> > Regards,
> >
> > --
> > ---------------------------------------------
> > Roland Nilsson
> > IFM Computational Biology
> > Linköping University, Sweden
> > rolle at ifm.liu.se
>
> MySort[n_, data_] :=
> Sort[Sort[Select[data, First[#] == n &], #1[[2]] <= #2[[2]] &],
> Apply[Plus, Rest[#1]] <= Apply[Plus, Rest[#2]] &]
>
> data = Table[{Random[Integer, {1, 3}], Random[Integer, {1, 3}],
> Random[]}, {30}];
>
> Flatten[Map[MySort[#, data] &, Union[Map[First, data]]], 1]
MySort[n_, data_] :=
Sort[Sort[Select[data, First[#] == n &], #1[[2]] <= #2[[2]] &],
Apply[Plus, Rest[#1]] <= Apply[Plus, Rest[#2]] &]
If
data = Table[{Random[Integer, {1, 3}], Random[Integer, {1, 3}],
Random[]}, {30}];
data1 = Flatten[Map[MySort[#, data] &, Union[Map[First, data]]], 1]
and if
data = Table[{Random[Integer, {1, 3}], Random[Integer, {1, 3}],
Random[Integer,{1, 3}]}, {30}];
data2 = Sort[
Flatten[Map[MySort[#, data] &, Union[Map[First, data]]], 1], #1[[3]] <=
#2[[3]] &] // Sort