go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/mathutil/median.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package mathutil
     9  
    10  // Median returns the middle value from a list of _unsorted_ values.
    11  func Median[T Operatable](values []T) T {
    12  	return MedianSorted(CopySort(values))
    13  }
    14  
    15  // MedianSorted returns the middle value from a list of _sorted_ values.
    16  func MedianSorted[T Operatable](values []T) (output T) {
    17  	if len(values) == 0 {
    18  		return
    19  	}
    20  	middle := len(values) >> 1
    21  	output = values[middle]
    22  	return
    23  }