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

Re: [sc-dev] sort



Hi Alberto,

> 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.
>

I think doing sorting in place is much more efficient since you don't need to
allocate more memory.  Maybe the method 'median' (or any others) should create
their own copy of the array instead of 'sort'.

> 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:
>
> 	sort { arg function;
> 		if (function.isNil) { function = { arg a, b; a <= b }; };
> 		^this.copy.quickSort(function)
> 	}
>

I don't think mergesort has anything over quicksort other than a simpler
algorithm.

http://www.cs.ubc.ca/spider/harrison/Java/FastQSortAlgorithm.java.html

Lance