multiversx_price_aggregator_sc/
median.rsmultiversx_sc::imports!();
pub fn calculate<M: ManagedTypeApi>(
list: &mut [BigUint<M>],
) -> Result<Option<BigUint<M>>, StaticSCError> {
if list.is_empty() {
return Result::Ok(None);
}
list.sort_unstable();
let len = list.len();
let middle_index = len / 2;
if len % 2 == 0 {
let median1 = list.get(middle_index - 1).ok_or("median1 invalid index")?;
let median2 = list.get(middle_index).ok_or("median2 invalid index")?;
Result::Ok(Some((median1.clone() + median2.clone()) / 2u64))
} else {
let median = list.get(middle_index).ok_or("median invalid index")?;
Result::Ok(Some(median.clone()))
}
}