Re: Is Sort stable?
- To: mathgroup at smc.vnet.net
- Subject: [mg39780] Re: Is Sort stable?
- From: "J.L.Garrido" <garrido at ruth.upc.es>
- Date: Fri, 7 Mar 2003 03:31:12 -0500 (EST)
- References: <b41bv1$pea$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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]