Bug 5514 – Erroneous documentation and lacking randomization for topN
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Mac OS X
Creation time
2011-02-01T07:57:00Z
Last change time
2015-06-09T05:15:19Z
Assigned to
andrei
Creator
magnus
Comments
Comment #0 by magnus — 2011-02-01T07:57:21Z
The topN function in std.algorithm is documented as having a running time of Ο(r.length), if stable. This should be an *expected* (or average-case) running time of O(r.length), with a worst-case running time of O(r.length^^2), based on the current implementation, which is the Randomized-Select algorithm.
Also, the implementation should probably use randomization (as in the standard formulation of the algorithm), rather than consistently picking the middle element. This will entail a slight overhead in picking the pivot, but this will (on average) only be done a logarithmic number of times, so the cost should be negligible. The gain from this is, of course, that consistent worst-case behavior in the face of certain inputs is avoided.
Comment #1 by github-bugzilla — 2013-02-24T16:49:26Z