How do I find the median of numbers in linear time using heaps?

前端 未结 7 845
谎友^
谎友^ 2020-12-04 06:59

Wikipedia says:

Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest element can be done i

相关标签:
7条回答
  • 2020-12-04 07:23

    perhaps it wasnt around when the original question was asked, but now wiki has a link to the source, and here it is: http://ftp.cs.purdue.edu/research/technical_reports/1991/TR%2091-027.pdf

    specifically, go to page 17, and look at the description of RSEL4. They prove in theorem 3.2 that the time complexity of this k-th selection algorithm is O(k). so it would take you O(n) to build the heap, and an extra O(k) to find k-th smallest item.

    its not really as straight forward as some of the other answers have suggested

    0 讨论(0)
提交回复
热议问题