【转】有序数组求中位数问题

1、有两个已排好序的数组A和B,长度均为n,找出这两个数组合并后的中间元素,要求时间代价为O(logn)。
2、假设两个有序数组长度不等,同样的求出中位数。
一:解析: 这个题目看起来非常简单。第一题的话: 假设数组长度为n, 那么我就把数组1和数组2直接合并,然后再直接找到中间元素。对于这样的方案,第一题和第二题就没有什么区别了。这样的话时间复杂度就是O(n)。通常在这样的情况下,那些要求比较高的面试官就会循循善诱道:“你还有更好的办法吗?” 如果比线性更高效,直接能想到的就是对数了O(log(n)),这个时间复杂度在这里可能吗? 当然还是可能的。
算法导论上面的分析是这样的:
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)
下面,我们来画个图,分析一下这个思路:

我们先来分析看看: 想到对数的效率,首先想到的就是二分查找,对于这个题目二分查找的意义在哪里呢?
我们找到了A[n/2] 和 B[n/2]来比较,
1、如果他们相等,那样的话,我们的搜索结束了,因为答案已经找到了A[n/2]就肯定是排序后的中位数了。
2、如果我们发现B[n/2] > A[n/2],说明什么,这个数字应该在 A[n/2]->A[n]这个序列里面, 或者在 B[1]-B[n/2]这里面。 或者,这里的或者是很重要的, 我们可以说,我们已经成功的把问题变成了在排序完成的数组A[n/2]-A[n]和B[0]-B[n/2]里面找到合并以后的中位数, 显然递归是个不错的选择了。
3、如果B[n/2] < A[n/2]呢?显然就是在A[0]-A[n/2]和B[n/2]-B[n]里面寻找了。

在继续想, 这个递归什么时候收敛呢?当然一个case就是相等的值出现, 如果不出现等到这个n==1的时候也就结束了。
照着这样的思路, 我们比较容易写出如下的代码, 当然边界的值需要自己思量一下(递归代码如下):

  1. // 两个长度相等的有序数组寻找中位数  
  2. int Find_Media_Equal_Length(int a[] , int b[] , int length)  
  3. {  
  4.     if(length == 1)  
  5.     {  
  6.         return a[0] > b[0] ? b[0] : a[0];  
  7.     }  
  8.     int mid = (length-1)/2;   //奇数就取中间的,偶数则去坐标小的  
  9.     if(a[mid] == b[mid])  
  10.         return a[mid];  
  11.     else if(a[mid] < b[mid])  
  12.     {  
  13.         return Find_Media_Equal_Length(&a[length-mid-1] , &b[0] , mid+1);    //偶数则取剩下的length/2,奇数则取剩下的length/2+1  
  14.         //return Find_Media_Equal_Length(a+length-mid-1 , b , mid+1);  
  15.     }  
  16.     else  
  17.     {  
  18.         return Find_Media_Equal_Length(&a[0] , &b[length-mid-1] , mid+1);  
  19.         //return Find_Media_Equal_Length(a , b+length-mid-1 , mid+1);  
  20.     }  
  21. }  

非递归代码如下:

  1. // 非递归代码  
  2. int Find_Media_Equal_Length(int a[] , int b[] , int length)  
  3. {  
  4.     int mid;  
  5.     while(1)  
  6.     {  
  7.         if(length == 1)  
  8.         {  
  9.             return a[0] > b[0] ? b[0] : a[0];  
  10.         }  
  11.         mid = (length-1)/2;  
  12.         if(a[mid] == b[mid])  
  13.             return a[mid];  
  14.         else if(a[mid] < b[mid])  
  15.             a = a + length - mid - 1;    // a数组的后半部分  
  16.         else  
  17.             b = b + length - mid - 1;    // b数组的后半部分  
  18.         length = mid + 1;  
  19.     }  
  20. }  

二:马上有人说那不定长的怎么办呢?一样的,我们还是来画个图看看:

一样的, 我们还是把这个两个数组来比较一下,不失一般性,我们假定B数组比A数组长一点。A的长度为n, B的长度为m。比较A[n/2]和B[m/2] 时候。类似的,我们还是分成几种情况来讨论:
a、如果A[n/2] == B[m/2],那么很显然,我们的讨论结束了。A[n/2]就已经是中位数,这个和他们各自的长度是奇数或者偶数无关。
b、如果A[n/2]  <   B[m/2],那么,我们可以知道这个中位数肯定不在[A[0]---A[n/2])这个区间内,同时也不在[B[m/2]---B[m]]这个区间里面。这个时候,我们不能冲动地把[A[0]---A[n/2])和[B[m/2]---B[m]]全部扔掉。我们只需要把[B[m-n/2]---B[m]]和[A[0]---A[n/2])扔掉就可以了。(如图所示的红色线框),这样我们就把我们的问题成功转换成了如何在A[n/2]->A[n]这个长度为 n/2 的数组和 B[1]-B[m-n/2]这个长度为m-n/2的数组里面找中位数了,问题复杂度即可下降了。
c、只剩下A[n/2] > B[m/2],和b类似的,我们可以把A[n/2]->A[n]这块以及B[1]->B[n/2]这块扔掉了就行,然后继续递归。
我们也可以写出如下的代码:

  1. // 两个长度不相等的有序数组寻找中位数  
  2. int Find_Media_Random_Length(int a[] , int lengtha , int b[] , int lengthb)  
  3. {  
  4.     int mida = lengtha/2;  
  5.     int midb = lengthb/2;  
  6.     int l = (mida <= midb) ? mida : midb;  
  7.     if(lengtha == 1)  
  8.     {  
  9.         if(lengthb % 2 == 0)  
  10.         {  
  11.             if(a[0] >= b[midb])  
  12.                 return b[midb];  
  13.             else if(a[0] <= b[midb-1])  
  14.                 return b[midb-1];  
  15.             return a[0];  
  16.         }  
  17.         else  
  18.             return b[midb];  
  19.     }  
  20.     else if(lengthb == 1)  
  21.     {  
  22.         if(lengtha % 2 == 0)  
  23.         {  
  24.             if(b[0] >= a[mida])  
  25.                 return a[mida];  
  26.             else if(b[0] <= a[mida-1])  
  27.                 return a[mida-1];  
  28.             return b[0];  
  29.         }  
  30.         else  
  31.             return a[mida];  
  32.     }  
  33.     if(a[mida] == b[midb])  
  34.         return a[mida];  
  35.     else if(a[mida] < b[midb])  
  36.         return Find_Media_Random_Length(&a[mida] , lengtha-l , &b[0] , lengthb-l);  
  37.     else  
  38.         return Find_Media_Random_Length(&a[0] , lengtha-l , &b[midb] , lengthb-l);  
  39. }  

举例如下:
A:1、2、8、9、10
B:1、2、3、4、11
第一次:
A:1、2、{8}、9、10
B:1、2、{3}、4、11
因为8>3,所以第二次:
A:1、{2}、8
B:3、{4}、11
因为2<4,所以第三次:
A:{2}、8
B:{3}、4
因为2<3,所以第四次:
A:{8}
B:{3}

再举一个简单的例子:
A: 1 3 5 7 9
B: 2 4 6 8 10
结果我们都知道是5(下中位数)。
第一步:取5 和 6比较,发现5<6,则在 7 9 2 4中寻找吗?
偶数的情况类似:
A: 1 3 5 7 9 11
B: 2 4 6 8 10 12
第一步:取5 和 6比较,发现5<6,则在 7 9 11 2 4中寻找吗?
个人认为取一半的时候一定需要包含用于比较的两个中位数(无论奇偶)。
就是说上面的两个例子第一步之后:
在A:5 7 9    B:2 4 6中继续找
在A:5 7 9 11    B:2 4 6中继续找
但这样导致新的问题是:新的数组A和B数字个数不一致了!
办法是: 在递归的过程中,当数组中的元素是偶数时,在一个数组中取上中位数,在另一个数组中取下中位数,并且在整个过程中保持不变。在哪个数组中去上中位数,就一直在那个数组中取上中位数,反之亦然。奇数时的情形依旧。
这也就解释了为什么代码中a[mid]<b[mid] 的时候,a数组的开始位置会是: a[length-mid-1]

原文地址:https://www.cnblogs.com/slysky/p/2752852.html