30秒学会 Golang 片段 · 2020年2月2日

30秒学会 Golang 片段 – Median

Returns the median of a collection of numbers.

Find the midpoint and convert it to an integer, use sort.Float64s() to sort the given numbers.
Return the number at the midpoint if length is odd, otherwise the average of the two middle numbers.

代码实现

import (
    "math"
    "sort"
)

func Median(nums ...float64) float64 {
    m, n := int(math.Floor(float64(len(nums))/2.0)),
        nums[:]
    sort.Float64s(n)

    if len(nums)%2 == 0 {
        return (n[m] + n[m+1]) / 2.0
    }
    return n[m]
}

使用样例

Median(5.0, 6.0, 50.0, 1.0, -5.0) // 5.0