c – 如何实现nth_element?

在StackOverflow和其他地方有很多索赔,nth_element是O(n),它通常用Introselect实现: http://en.cppreference.com/w/cpp/algorithm/nth_element

我想知道如何实现这一点.我看着Wikipedia’s explanation of Introselect,只是让我更困惑. QSort和Median-Med的算法如何切换?

在这里发现了Introsort的文章http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.14.5196&rep=rep1&type=pdf但是说:

In this paper,we concentrate on the sorting problem and return to the selection problem only briefly in a later section.

我试图阅读STL本身来了解如何实现nth_element,但是这样做真的很快.

有人会告诉我如何实现Introselect的伪代码?或者甚至更好,实际的C代码除了STL当然是:)

解决方法

你问了两个问题,一个是题目

How is nth_element Implemented?

你已经回答过:

There are a lot of claims on StackOverflow and elsewhere that nth_element is O(n) and that it is typically implemented with Introselect.

我也可以通过查看我的stdlib实现来确认. (稍后再来)

而你不明白答案的那个人:

How can an algorithm switch between QSort and Median-of-Medians?

让我们看看我从我的stdlib中提取的伪代码

nth_element(first,nth,last)
{ 
  if (first == last || nth == last)
    return;

  introselect(first,last,log2(last - first) * 2);
}

introselect(first,depth_limit)
{
  while (last - first > 3)
  {
      if (depth_limit == 0)
      {
          // [NOTE by editor] This should be median-of-medians instead.
          // [NOTE by editor] See Azmisov's comment below
          heap_select(first,nth + 1,last);
          // Place the nth largest element in its final position.
          iter_swap(first,nth);
          return;
      }
      --depth_limit;
      cut = unguarded_partition_pivot(first,last);
      if (cut <= nth)
        first = cut;
      else
        last = cut;
  }
  insertion_sort(first,last);
}

没有详细了解引用的函数heap_select和unguarded_pa​​rtition_pivot,我们可以清楚地看到,nth_element给出了introselect 2 * log2(大小)细分步骤(在最佳情况下是quickselect所需的两倍),直到heap_select启动并解决了好.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...