How does random shuffling in quick sort help in increasing the efficiency of the code?

前端 未结 5 1004
忘掉有多难
忘掉有多难 2020-12-28 20:00

I was going through lecture videos by Robert Sedgwick on algorithms, and he explains that random shuffling ensures we don\'t get to encounter the worst case quadratic time s

5条回答
  •  醉话见心
    2020-12-28 20:04

    The assumption is that the worst case -- everything already sorted -- is frequent enough to be worth worrying about, and a shuffle is a black-magic least-effort sloppy way to avoid that case without having to admit that by improving that case you're moving the problem to another one, which happened to get randomly shuffled into sorted order. Hopefully that bad case is a much rarer situation, and even if it does come up the randomness means the problem can't easily be reproduced and blamed on this cheat.

    The concept of improving a common case at the expense of a rare one is fine. The randomness as an alternative to actually thinking about which cases will be more or less common is somewhat sloppy.

提交回复
热议问题