简体   繁体   中英

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. But i was thinking of an algo which could be o(k) if correct . 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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