算法5:求两个已排序数组的交集和并集

算法5:求两个已排序数组的交集和并集

问题描述

求两个已排序数据的交集和并集,要求时间复杂度为O(m+n).

解题思路

A数组和B数组,A数组大小为m,B数组大小为n。 1、查找B数组的每个成员是否在A数组中,时间复杂度为O(mn) 2、由于A和B数组都是有序数组,使用二分法查找B数组的每个成员是否在A数组中,时间复杂度为O(n*lgm)。如果n比m大,则查找A数组的成员是否在B数组中,时间复杂度为O(m*lgn)。 3、使用hash表,将A数组的值使用hash表保存,B中的值判断是否存在A中,由于hash表的查找时间复杂度为O(1),所以该算法的时间复杂度为O(n)。但是此方法只适合m比较小的情况,如果A数组比较大,hash表容易产生collision的情况,hash表的查找平均速度将不再是O(1)。 4、使用两个指针分别指向数组A和数组B,指向数据小的指针往前继续移动,保存两个指针指向相同数据的值,直到两个指针都指向数组末尾,该算法的时间复杂度为O(m+n)。

交集就是保存两个指针指向相同的值,并集就是保存两个指针指向不同的值,并且保存一份指向相同的值

C++代码

//获取两个排序数组的交集 void GetIntersectionSet(int ABuffer[],int ALength, int BBuffer[],int BLength,vector<int>& intersectionSet) { int pointerA = 0; int pointerB = 0; while(pointerA < ALength && pointerB < BLength) { if(ABuffer[pointerA] < BBuffer[pointerB]) { pointerA++; } else if(BBuffer[pointerB] < ABuffer[pointerA]) { pointerB++; } else { intersectionSet.push_back(ABuffer[pointerA]); pointerA++; pointerB++; } } } //获取两个排序数组的并集 void GetUnionSet(int ABuffer[],int ALength, int BBuffer[],int BLength,vector<int>& unionSet) { int pointerA = 0; int pointerB = 0; while(pointerA < ALength && pointerB < BLength) { if(ABuffer[pointerA] < BBuffer[pointerB]) { unionSet.push_back(ABuffer[pointerA]); pointerA++; } else if(BBuffer[pointerB] < ABuffer[pointerA]) { unionSet.push_back(BBuffer[pointerB]); pointerB++; } else { unionSet.push_back(ABuffer[pointerA]); pointerA++; pointerB++; } } if(pointerA < ALength) { for(int i = pointerA; i < ALength;i++) { unionSet.push_back(ABuffer[i]); } } if(pointerB < BLength) { for(int i = pointerB; i < BLength;i++) { unionSet.push_back(BBuffer[i]); } } }

测试代码

int _tmain(int argc, _TCHAR* argv[]) { //定义排序数组A和B const int length = 6; int ABuffer[length] = {0}; int BBuffer[length] = {0}; cout<<"please input orderly A Buffer:"<<endl; for(int i = 0; i < length; i++) { cin>>ABuffer[i]; } cout<<"please input orderly B Buffer:"<<endl; for(int i = 0; i < length; i++) { cin>>BBuffer[i]; } //定义交集集合和并集集合 vector<int> intersectionSet; intersectionSet.clear(); vector<int> unionSet; unionSet.clear(); GetIntersectionSet(ABuffer,length,BBuffer,length,intersectionSet); GetUnionSet(ABuffer,length,BBuffer,length,unionSet); //输出交集和并集 cout<<"the intersection set of orderly A and orderly B as follows:"<<endl; vector<int>::iterator itA; for(itA = intersectionSet.begin(); itA != intersectionSet.end();itA++) { cout<<*itA<<" "; } cout<<endl; cout<<"the union set of orderly A and orderly B as follows:"<<endl; vector<int>::iterator itB; for(itB = unionSet.begin(); itB != unionSet.end();itB++) { cout<<*itB<<" "; } cout<<endl; return 0; }