MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Median

Function Median

math/median.go:14–30  ·  view source on GitHub ↗
(values []T)

Source from the content-addressed store, hash-verified

12)
13
14func Median[T constraints.Number](values []T) float64 {
15
16 sort.Bubble(values)
17
18 l := len(values)
19
20 switch {
21 case l == 0:
22 return 0
23
24 case l%2 == 0:
25 return float64((values[l/2-1] + values[l/2]) / 2)
26
27 default:
28 return float64(values[l/2])
29 }
30}

Callers 1

TestMedianFunction · 0.92

Calls 1

BubbleFunction · 0.92

Tested by 1

TestMedianFunction · 0.74