Median

我们知道排序的开销是nlogn, 

证明如下:n 个节点的permutation是n!

binary tree的高度至少是 logn!

logn! >nlogn  [这儿实际上是log2n   ]  


现在我们要找到n个元素中的median,通过排序肯定是可以的,缺点是O(nlogn).

而我们的理想算法是接近线性的。应该是可以的,因为排序明显是做的有点过了。

我只需要找到median,我可不管你最后是不是排序的。

randomized divide-and-conquer algorithm for selection

for any number u, image splitting the list U into three categories:

  • elements smaller than u
  • those equal to u
  • elements greater than u

the search can instantly be narrowed down to one of these sublists.

                           | -        selection(SL,k)                           if (k < |SL|)

selection(S,k) =  |          v

                           | -        selection(SR,k-|SL|-|v|)

 

sum up:

Quicksort is a sorting algorithm that splits the array in exactly the same way as the

median algorithm. Its worst-case performance is O(n2), like that of median finding.

But it can be proved that its average case is O(nlogn).

Furthermore, empirically it outperforms other sorting algorithms.

This has made quicksort a favorite in many apps.

posted on 2012-02-18 22:29  grepp  阅读(278)  评论(0编辑  收藏  举报

导航