4 ms·
You can use a randomized selection algorithm to find the median in linear time, and if you use the median as a pivot you will never get worst case n^2 behavior.
by gamegoblin 14y ago
You can use a randomized selection algorithm to find the median in linear time, and if you use the median as a pivot you will never get worst case n^2 behavior.
This is not used in practice because the probability of getting worst case behavior is extremely slim if you do some clever, and cheap, tricks.
- kolistivra 14y agoRandomized selection algoritm is actually O(N^2) in the worst case. Median of medians is the O(N) worst case selection algorithm.