计算中位数在C# [英] Calculate median in c#

查看:193
本文介绍了计算中位数在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写函数,将接受小数数组,它会找到的中位数。

有没有在.net数学库中的函数?

解决方案
  

有没有在.net数学库中的函数?

没有。

这并不难,虽然自己编写。天真算法排序阵列和拾取中间(或两个中间的平均值)的元素。但是,这种算法是为O(n log n)的,而其可能的解决 O(N)时间这个问题。你想看看选择算法得到这样的算法。

I need to write function that will accept array of decimals and it will find the median.

Is there a function in the .net Math library?

解决方案

Is there a function in the .net Math library?

No.

It's not hard to write your own though. The naive algorithm sorts the array and picks the middle (or the average of the two middle) elements. However, this algorithm is O(n log n) while its possible to solve this problem in O(n) time. You want to look at selection algorithms to get such an algorithm.

这篇关于计算中位数在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆