Randomness in built-in .sort

Ary Borenszweig ary at esperanto.org.ar
Tue Jan 6 11:06:44 PST 2009


Sean Kelly wrote:
> == Quote from Walter Bright (newshound1 at digitalmars.com)'s article
>> Bill Baxter wrote:
>>> It seems to me that the built-in .sort uses a randomized algorithm
>>> that leads to results that differ from run-to-run when there are
>>> elements with identical keys.
>> No, it doesn't use random sorts. The source code is included, so this
>> should be easy to verify.
> 
> I don't know if it helps, but the built-in sort uses a similar algorithm
> to the sort routine in tango.core.Array.  It picks a "median of 3" value
> for the pivot.  As Walter said, I don't believe any randomness is involved.
> 
> 
> Sean

I think Bill speaks about a stable sort. You can have an unstable sort 
algorithm without having explicity a random invocation. Note that he's 
saying "leads to results that differ from run-to-run when there are 
elements with identical keys".



More information about the Digitalmars-d mailing list