算法导论第九章9.3.6----k分位数

来源:互联网 发布:java练手小项目 编辑:程序博客网 时间:2024/05/22 06:39

转自:http://dreamkorea.tistory.com/attachment/jk37.pdf

9.3-6
The kth quantiles of an n-element set are the k-1 order statistics that divide the
sorted set into k equal-sized sets (to within1). Give an O(nlgk)-time algorithm
to list the kth quantiles of a set.

对一个含有n个元素的集合来说,所谓k分位数,就是能把已经排序的集合分成k个大小相等的集合的k-1个顺序统计量。飞出一个能列出某一集合的k分位数的O(nlgk)时间的算法。


The k quantiles of an n-element sorted array A are:

已排序的有n个元素的数组A的k分位数是


We have as inputs an unsorted array A of distinct keys, an integer k, and an
empty array Q of length k − 1 and we want to find the kth quantiles of A.

函数的输入是一个未排序的数组A,整数k,空数组Q,用来存放k分位数的长度为k-1的数组。


The output Q of the recursive algorithm QUANTILES contains the kth quantiles of A.

递归算法QUANTILES的输出Q包含A的k分位数。

The algorithm first finds the key that turns out to be the lower median of Q, and then partitions the input array about this key. 

算法首先找到Q的低中位数,之后以这个数将数组A分成2部分。

So we have two smaller arrays that each contain at most (k − 1)/2 of the k − 1 order statistics of the original array A. 

因此我们得到两个小数组,每个数组包含至多(k-1)/2个原数组A的k-1个顺序统计量。

At each level of the recursion the number of order statistics in the array is at most half the number of the previous array.

在递归算法的每一层,数组中的顺序统计量个数,最多包含递归输入数组顺序统计量的一半。
Consider a recurrsion tree for this algorithm. At the top level we need to find k − 1 order statistics, and it costs O(n) to find one.

考虑这个算法的递归树。在最底层,我们需要找到k-1个顺序统计量,找到一个的时间复杂度为O(n)。

 The root has two children, one contains at most b(k − 1)/2c order statistics, and the other d(k − 1)/2e order statistics. The sum of the costs for these two nodes is O(n).

roo有2个孩子,每个最多有个顺序统计量,另一个有个顺序统计量。找到这两几个节点的时间复杂度总和是O(n).

At depth i we find 2^i order statistics. 

在深度为i处,找到2^i个顺序统计量。

The sum of the costs of all nodes at depth i is O(n), for  0 ≤ i ≤ lg(k − 1), because the total number of elelements at any depth is n.

在深度为i( 0 ≤ i ≤ lg(k − 1))处的所有节点的时间复杂度总和是O(n),因为任何深度的节点总数是n。

The depth of the tree is d = lg(k − 1). Hence, the worstcase running time of QAUNTILES is Θ(n lg k).

树的深度是 d = lg(k-1)。因此,最坏情况的运行时间是Θ(n lg k)。

0 0
原创粉丝点击