如何求两个有序数组的第K个数

给定两个有序的数组,长度分别为m和n,求这两个数组中的第K个元素。

 问题分析:

 1. 把 A 平均分为前后两个部分,前部分有 x 个元素,后部分有 n-x 个元素(由于 A 是有序的,所以后一部分的所有元素都大于前一部分)。A[x] 为 A 的后一部分中的第一个元素。

 2. 同理把 B 也平均分成前后两个部分,前部分有 y 个元素,后部分有 m-y 个元素。B[y] 是 B 的后一部分中的第一个元素。

 3. 由于两个数组都是被平均分割的,所以可以近似地认为 x = n/2, y = m/2。这里不妨设 A[x] <= B[y](如果 A[x] > B[y] 处理过程和下面类似): 

 情况1:

 由于在 A 中,A[x] 前面有 x 个元素,在 B 中,B[y] 前面有 y 个元素,并且又有 A[x] <= B[y],那么,合并以后,A[x]前面原来那些元素必然也在B[y]前面。也就是说,B[y]前面至少会有 x + y 个元素,我们再规定如果 A, B 中有相同元素,则合并后 A 中的元素排在 B 前面,那么归并以后 A[x] 也会排在 B[y] 前面,于是,合并之后 B[y] 至少有 x+y+1 个元素。 

 如果 k <= x+y+1,也就是说,合并后第 k 个元素必然落在 B[y] 前面。所以,原来在 B 数组中的后一部分(B[y]以及 B[y] 之后)那些元素都不可能包含我们要找到内容(第 k 大元素),所以我们可以把他们排除掉。这样就排除了 B 中一半的内容。 

 情况2:

 在 A 中,A[x] 及其后面有 n-x 个元素,除去 A[x] 之后有 n-x-1 个元素,B[y] 及其后面有 m-y 个元素。那么,由于 A[x] <= B[y],所以合并起来之后,B[y] 后面那些元素必然也在 A[x] 后面,则合并后 A[x] 后面至少有(n-x-1) + (m-y) = (n+m)-(x+y+1) 个元素。 

 如果 k > x+y+1,也就说,合并后第 k 大的元素必然落在 A[x] 后面。所以,原来在 A 数组中,第一部分(A[x]之前)以及 A[x] 都不可能包含我们要找的元素,所以我们可以把他们排除掉。这样就排除了 A 中一半的内容。 

 总结:

 综上所述,对于 k <= x+y+1 还是 k > x+y+1 我们都提出了解决的方案,并且每种方案都能把 A 或者 B 的规模减小一半。减小了一半之后,我们将其作为一个新的问题继续使用上面的算法处理,直到 A 或者 B 减小到足够小: 

  1. A 没有了,这样只需要找出 B 中第 k 大的元素,也就是 B[k]。
  2. B 没有了,同上结果就是 A[k]。
  3. #include <iostream>
    #include <algorithm>
    #include <ctime>
    //#define DEBUG
    
    #define MINV(a, b) ((a) <= (b) ? (a) : (b))
    #define MAXV(a, b) ((a) >= (b) ? (a) : (b))
    #define MAX_N 10000000
    #define MAX_VAL 10000
    using namespace std;
    
    int array_1[MAX_N] = {0, 12, 17, 19, 44, 47, 52, 52, 57, 90};
    int array_2[MAX_N] = {14, 20, 38, 47, 76, 77, 77, 82, 89, 89};
    int n, m, k;
    
    int getKth(int s1, int e1, int s2, int e2, int k)
    {
    	int mid_1 = (s1 + e1) / 2;
    	int mid_2 = (s2 + e2) / 2;
    	
    	int pLen_1 = mid_1 - s1 + 1;
    	int pLen_2 = mid_2 - s2 + 1;
    
    	if(s1 > e1) pLen_1 = 0;
    	if(s2 > e2) pLen_2 = 0;
    	int pSum = pLen_1 + pLen_2;
    
    	if(k < pSum)
    	{
    		
    		if(pLen_2 != 0 && (pLen_1 == 0 || array_1[mid_1] <= array_2[mid_2]))
    			return getKth(s1, e1, s2, mid_2 - 1, k);
    		else
    			return getKth(s1, mid_1 - 1, s2, e2, k);
    	}
    	else
    	{
    		if(k == pSum)
    		{
    			if(pLen_1 == 0) return array_2[mid_2];
    			else if(pLen_2 == 0) return array_1[mid_1];
    		}
    
    		if(pLen_1 != 0 && (pLen_2 == 0 || array_1[mid_1] <= array_2[mid_2]))
    			return getKth(mid_1 + 1, e1, s2, e2, k - pLen_1);
    		else 
    			return getKth(s1, e1, mid_2 + 1, e2, k - pLen_2);
    	}
    }
    
    int main()
    {
    	int i, j;
    	
    	// testing cases num: 1000
    	int caseNum = 1;
    	
    	while(caseNum--)
    	{
    		//getchar();
    		// create m, n, k
    		srand(time(NULL));
    		m = rand() % MAX_N + 1;
    		n = rand() % MAX_N + 1;
    		k = rand() % (m + n) + 1;
    		
    		// create array_1 and sort it
    		for(i = 0; i < m; i++)
    			array_1[i] = rand() % MAX_VAL;
    		sort(array_1, array_1 + m);
    		
    		// output array_1
    #ifdef DEBUG
    		cout<<"k:"<<k<<endl;
    		cout<<"array_1:"<<endl;
    		for(i = 0; i < m; i++)
    			cout<<array_1[i]<<" ";
    		cout<<endl;
    #endif		
    		// create array_2 and sort it
    		for(i = 0; i < n; i++)
    			array_2[i] = rand() % MAX_VAL;
    		sort(array_2, array_2 + n);
    		
    		// output array_2
    #ifdef DEBUG
    		cout<<"array_2:"<<endl;
    		for(i = 0; i < n; i++)
    			cout<<array_2[i]<<" ";
    		cout<<endl;
    #endif
    		// get the kth num
    		int kthValue = getKth(0, m - 1, 0, n - 1, k);
    #ifdef DEBUG
    		cout<<"The kth value by my algorithm: "<<kthValue<<endl;
    #endif
    		// verify the result
    		int idex = 0;
    		int realValue;
    		i = j = 0;
    		while(i < m || j < n)
    		{
    			if(j == n || (i != m && array_1[i] <= array_2[j]))
    			{
    				idex++;
    				if(idex == k)
    				{
    					realValue = array_1[i];
    					break;
    				}
    				i++;
    				continue;
    			}
    			if(i == m || (j != n && array_2[j] <= array_1[i]))
    			{
    				idex++;
    				if(idex == k)
    				{
    					realValue = array_2[j];
    					break;
    				}
    				j++;
    			}
    		}
    #ifdef DEBUG
    		cout<<"The real kth value is: "<<realValue<<endl;
    #endif
    		if(realValue == kthValue) cout<<"Result: Correct"<<endl;
    		else
    		{
    			cout<<"k:"<<k<<endl;
    			cout<<"array_1:"<<endl;
    			for(i = 0; i < m; i++)
    				cout<<array_1[i]<<" ";
    			cout<<endl;
    			cout<<"array_2:"<<endl;
    			for(i = 0; i < n; i++)
    				cout<<array_2[i]<<" ";
    			cout<<endl;
    			cout<<"The kth value by my algorithm: "<<kthValue<<endl;
    			cout<<"The real kth value is: "<<realValue<<endl;
    			cout<<"Resutl: Wrong"<<endl;
    
    			break;
    		}
    		
    #ifdef DEBUG
    		cout<<endl<<endl;
    #endif
    		
    		k++;	
    	}
    	return 0;	
    }
    
    
    
    
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值