1. 程式人生 > >遞迴分治問題之找出兩個有序序列的中間值

遞迴分治問題之找出兩個有序序列的中間值

問題描述:

        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 n

th 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)