找回密码
 欢迎注册
查看: 11982|回复: 6

[讨论] 快速求两有序数组合并后的中位数

[复制链接]
发表于 2009-2-20 16:12:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
先解释一下“中位数”的概念:
  把n个数据按大小顺序排列,处于最中间位置的一个数据(或)叫做这组数据的中位数(median)。
中位数仅与数据排列位置有关,当一组数据从小到大排列后,最中间的数据为中位数(偶数个数据取最中间两个的平均数)。

现在的问题是:给出两个已排序的数组A和B,要求快速得到它们合并后的中位数?
(更一般的,如何快速得到两有序数组合并排序后的任意某个位置上的数据?)

大家有空时讨论讨论。
这是我自编的,也许在某个时候某个领域会有用。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-20 16:25:00 | 显示全部楼层
好像很简单的样子
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-20 17:46:02 | 显示全部楼层
csdn里面好像讨论过的
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-20 18:35:22 | 显示全部楼层
from CSDN:

Say the two arrays are sorted and increasing, namely A and B.
It is easy to find the median of each array in O(1) time.
Assume the median of array A is m and the median of array B is n.
Then,
1' If m=n, then clearly the median after merging is also m, the algorithm holds.
2' If m <n, then reserve the half of sequence A in which all numbers are greater than
  m, also reserve the half of sequence B in which all numbers are smaller than n.
  Run the algorithm on the two new arrays.
3' If m>n, then reserve the half of sequence A in which all numbers are smaller than
  m, also reserve the half of sequence B in which all numbers are larger than n.
  Run the algorithm on the two new arrays.

Time complexity: O(logn)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-20 21:21:11 | 显示全部楼层
归并排序就是这个问题的变形吧
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-21 11:53:34 | 显示全部楼层
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-2-23 02:43:48 | 显示全部楼层
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-5-4 08:06 , Processed in 0.049920 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表