O(n) algorithm to find the median of a collection of numbers

后端 未结 3 1919
[愿得一人]
[愿得一人] 2020-11-27 14:59

Problem: input is a (not necessarily sorted) sequence S = k1, k2, ..., kn of n arbitrary numbers. Consider the collection C of n² numbers of the form min{ki,kj}, for 1 <=

3条回答
  •  旧时难觅i
    2020-11-27 15:29

    Wikipedia has a good article on Selection algorithms. If you are using C++, the STL includes a nth_element() algorithm with linear time on average.

提交回复
热议问题