How to calculate the median of an array?

后端 未结 14 1709
北海茫月
北海茫月 2020-12-05 06:10

I\'m trying to calculate the total, mean and median of an array thats populated by input received by a textfield. I\'ve managed to work out the total and the mean, I just ca

14条回答
  •  既然无缘
    2020-12-05 06:50

    I was looking at the same statistics problems. The approach you are thinking it is good and it will work. (Answer to the sorting has been given)

    But in case you are interested in algorithm performance, I think there are a couple of algorithms that have better performance than just sorting the array, one (QuickSelect) is indicated by @bruce-feist's answer and is very well explained.

    [Java implementation: https://discuss.leetcode.com/topic/14611/java-quick-select ]

    But there is a variation of this algorithm named median of medians, you can find a good explanation on this link: http://austinrochford.com/posts/2013-10-28-median-of-medians.html

    Java implementation of this: - https://stackoverflow.com/a/27719796/957979

提交回复
热议问题