递归分治问题之找出两个有序序列的中间值

本文介绍了一种高效算法,用于从两个独立数据库中各包含n个数值的情况下,通过最少次数的查询来确定合并数据集的中位数。该算法采用分治策略,逐步缩小查询范围,最终在O(log n)查询次数内找到中位数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

        You are interested in analyzing some hard-to-obtain data from two separate databases. Each database contains n numerical values, so there are 2n values total and you may assume that no two values are the same. You’d like to determine the median of this set of 2n values, which we will define here to be the nth smallest value. However, the only way you can access these values is through queries to the databases. In a single query, you can specify a value k to one of the two databases, and the chosen database will return the kth smallest value that it contains. Since queries are expensive, you would like to compute the median using as few queries as possible.
   Give an algorithm that finds the median value using at most O(log n) queries.

解题思路:

    首先,我们比较这两个序列的中间值,设第一个序列D1,中间值m1;设第二个序列,D2,中间值m2,且两个序列长度一样均为n。当第一个序列的中值m1>m2时,则说明两个序列融合后的中值一定处在D1序列的D1[n/2...n-1]和D2序列的D2[0...n/2-1]。我们把D1[n/2...n-1]作为新的D1,把D2[0...n/2-1]作为新的D2。继续上述的判断运算。直至最后的D1和D2都只有1个元素,比较找到较大(或较小或平均值),即为两个序列结合后的中值。

pseudo-code:

    Merge for two datasets

    p1=p2=n/2;

    for(i=2 ... log2(n))//因为进行的是二元搜索,共进行的是log2(n)

      m1=query(D1,p1);

      m2=query(D2,p2);

      if(m1>m2)

       {

         p1=p1-n/2^i;

         p2=p2+n/2^i;

       }

      else

       {

         p1=p1+n/2^i;

         p2=p2-n/2^i;

       }

    return min(m1,m2);

时间复杂度:因为执行的递归分治方案,时间复杂度o(nlogn)



         

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值