algorithm - 随机快速排序划分概率

标签 algorithm sorting quicksort

我正在阅读 Algorithms Illuminated: Part 1 ,问题 5.2 指出:

Let ɑ be some constant, independent of the input array length n, strictly between 0 and 1/2. What is the probability that, with a randomly chosen pivot element, the Partition subroutine produces a split in which the size of both the resulting subproblems is at least ɑ times the size of the original array?

答案选择是:

  1. ɑ
  2. 1 - ɑ
  3. 1 - 2ɑ
  4. 2 - 2ɑ

我不确定如何回答这个问题。有什么想法吗?

最佳答案

让数组中有 N 个元素。如果选取的主元是数组中最小的 [Nα] 元素之一,则左分区的大小将小于 。同样,如果选择的主元是数组中最大的 [Nα] 元素之一,则右分区的大小将小于

因此,您可以选择 N - 2 * [Nα] 个元素,使两个分区的大小都大于或等于 。由于该算法随机选择一个枢轴,因此所有元素被选中的概率均等。

因此,得到这样的 split 的概率是 1 - 2α + O(1/N)

关于algorithm - 随机快速排序划分概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312485/

相关文章:

c++ - 简单的 std::sort 不工作

Perl 对数组的引用数组进行排序

algorithm - Quick sort中解决最差时间复杂度是3的中位数吗?

python - 如何重新排列这样的列表(python)?

c# - 为什么我会在这里收到 IndexOutOfRangeException?

string - 蛮力字符串匹配概念

python - Python 中错误的输出顺序

java - 适合内存的顺序数据的 QuickSort 和 MergeSort 性能与磁盘上访问顺序数据的速度比较慢

c - C中使用多线程的快速排序中的并行排序

c++ - 在中序遍历中,如何从一个递归到下一个递归保存一个元素?