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

前端 未结 5 988
忘掉有多难
忘掉有多难 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:16

    In case of randomized QuickSort, since the pivot element is randomly chosen, we can expect the split of the input array to be reasonably well balanced on average - as opposed to the case of 1 and (n-1) split in a non randomized version of the algorithm. This helps in preventing the worst-case behavior of QuickSort which occurs in unbalanced partitioning.

    Hence, the average case running time of the randomized version of QuickSort is O(nlogn) and not O(n^2);

提交回复
热议问题