6 ms·
Fisher-Yates is a better idea, of course, since it is pretty short and runs in linear time, but if someone insists on a sort-based one-linear this should work:
by omaranto 15y ago
Fisher-Yates is a better idea, of course, since it is pretty short and runs in linear time, but if someone insists on a sort-based one-linear this should work:
list.map(function (t) { return [Math.random(), t]; }).sort().map(function (t) { return t[1]; })
- tomstuart 15y agoFisher-Yates is also in-place, just like Array.prototype.sort.
- omaranto 15y agoArgh, I missed the edit time window: "one-linear", should be, of course, "one-liner". Fisher-Yates is O(n), but the sort makes this algorithm Omega(n log n).
- __rkaup__ 15y agoWhat's 'Omega'?
- throw_away 15y agoomega() is like the opposite of O(). it means "at least" rather than "at most". — http://en.wikipedia.org/wiki/Big_Oh_notation#Family_of_Bachmann.E2.80.93Landau_notations http://en.wikipedia.org/wiki/Big_Oh_notation#Family_of_Bachm...