简体   繁体   English

O(k)算法从最小堆中查找第k个最小元素

[英]O(k) algorithm to find kth min element from a min heap

o(klogn) solution is trivial to find the kth minimum.And here O(klogk) time algorithm to find kth smallest element from a binary heap is a O(klogk) solution. o(klogn)解决方案对于找到第k个最小值非常简单。这里的O(klogk)时间算法是从二进制堆中找到第k个最小元素的一种方法是O(klogk)解决方案。 But i was thinking of an algo which could be o(k) if correct . 但是我在想一种算法,如果正确的话可能是o(k)。 From the min heap,take the first k elements(first k nodes,traverse level wise ) and store them in an array .Now max heapify this array bottom-up which would take o(k) time.The root of this heap would be the required answer.Can anyone see a flaw in this algorithm? 从min堆中,获取前k个元素(前k个节点,遍历wise)并将其存储在数组中。现在max将此数组自下而上进行最大堆,这将花费o(k)时间。此堆的根是所需的答案。谁能看到该算法的缺陷?

Consider this example 考虑这个例子

        1
    2       11
  3   4   12  13

If you want to get the 3rd min element, it wouldn't be in the 3 first nodes. 如果要获取第3个min元素,则它将不在第3个第一个节点中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM