[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] sort

On Dec 29, 2004, at 5:56 AM, Alberto de Campo wrote:

while testing my statistics stuff...
it turns out that sort changes the array itself:

a = [3,2,1,4];
// a.sort[2].postln;	// e.g. find second largest item, or
a.median.postln;	// calls sort also..,
a.postln;		// and now, a is [1,2,3,4]!

I find this surprising, and would assume that sort should do .copy first.

do a copy yourself. in-place sorts are often desired.

also, (on large arrays) quicksort measures faster than mergeSort;
is there some other reason why it should be mergeSort,
or can I change sort to this:

no. you can call quickSort directly if you want that.
mergesort should be the default because mergesort is stable, quicksort is not. stable means that items that compare as equal remain in the same order as they were in the input.

stable sorts are important if you want to do two level sorts. i.e. sort on criteria A and then sort on criteria B. All items with an equal B will then be sorted by A. This will not work with Quicksort.