查找数组的中值? [英] Finding the median value of an array?

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

问题描述

我想知道是否可以找到数组的中值?例如,假设我有一个大小为 9 的数组.能不能找到这个数组的中间槽?

I was wondering if it was possible to find the median value of an array? For example, suppose I have an array of size nine. Would it possible to find the middle slot of this array?

推荐答案

假设数组 x 已排序且长度为 n:

Assuming the array x is sorted and is of length n:

如果 n 为奇数,则中位数为 x[(n-1)/2].
如果 n 是偶数,则中位数是 ( x[n/2] + x[(n/2)-1] )/2.

If n is odd then the median is x[(n-1)/2].
If n is even than the median is ( x[n/2] + x[(n/2)-1] ) / 2.

这篇关于查找数组的中值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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