Is it possible to calculate median of a list of numbers better than O(n log n)?

前端 未结 7 899
攒了一身酷
攒了一身酷 2021-02-14 04:30

I know that it is possible to calculate the mean of a list of numbers in O(n). But what about the median? Is there any better algorithm than sort (O(n log n)) and lookup middl

相关标签:
7条回答
  • 2021-02-14 05:02

    What you're talking about is a selection algorithm, where k = n/2. There is a method based on the same partitioning function used in quicksort which works. It is called, not surprisingly, quickselect. While it can, like quicksort, have a O(n2) worst case, this can be brought down to linear time using the proper pivot selection.

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