微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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启动并解决了好.

原文地址:https://www.jb51.cc/c/115075.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐