R
robertwessel2
Odd, in order to reduce actual recursion (and do tail recursion
instead), and to keep locality of reference tight, I'd handle the
smaller part first were I to use an actual quicksort. Not that I'd
do that, being more of a fan of heapsort in situations where I want
an acceptable worst-case big-Oh for reasons such as availability.
The usual approach for the semi-recursive implementation is to recurse
on the smaller partition, and then loop (or allow tail recursion,
assuming your implementation language supports that) on the bigger
one. If you do a fully iterative version (managing your own stack),
you need to “push” the smaller partition last, so that you sort it
first. If you do it the other way, you can't bound your stack size
to log(N). A fully recursive implementation is stuck with order-N
worst case stack requirements (no matter which order you process the
partitions), unless you can guarantee tail recursion elimination.
Nope, reduces the constant, that's all. If the median is selected
from 3 random (i.e. unpredicatable to an attacker) elements, then
the worst case behaviour can not be forced, and you would average
to the theoretical average. But then you have to secure information
about your (P)RNG state.
And producing large quantities of good random numbers with low
overhead is much harder than the problem you wanted to solve in the
first place.