a[]={0, 1,2,3,4 };
B[]={ 1,3,5,7,9 };
Then their intersection is {1, 3}.
There are many methods to calculate the intersection of arrays, but the relative size of arrays will generally affect the efficiency of the algorithm, so it is necessary to determine the method according to the relative size of two arrays.
(1) In general, when two arrays have the same length, the following three methods can be adopted.
Method 1: Use bidirectional merge to traverse two arrays. (This name is very domineering and has martial arts tricks. )
Let two arrays be array 1[n 1] and array2[n2], and traverse the two arrays from the beginning with I and J respectively. In the process of traversal, if the array 1[i] of the current traversal position is equal to array2[j], then this number is the intersection of two arrays, which are recorded in any array 1 or array2, and array 1 and array2 traverse backwards. If the array 1 [i] >: Array2[j], you need to continue to traverse Array2. If array 1[i] is less than array2[j], you need to continue to traverse array 1 until an array has been traversed. Generally speaking, whoever falls behind will increase his subscript.
The code is as follows:
# contains "stdafx.h"
# include & ltstdio.h & gt
void mixed(int array 1[],int n 1,int array2[],int n2)
{
int i = 0,j = 0,k = 0;
And (I<n 1. & ampj & ltn2)
{
If (array 1[i] == array 2[j])
{
Array 1[k] = array1[i];
printf("%d\n ",array 1[k]);
i++;
j++;
k++;
}
else if(array 1[I]& gt; Array 2[j])
{
j++;
}
else if(array 1[I]& lt; Array 2[j])
{
i++;
}
}
}
void main()
{
int a[] = { 0, 1,2,3,4 };
int b[] = { 1,3,5,7,9 };
Mixing (a, 5, b, 5);
getchar();
}
The effect is as shown in the figure:
Method 2: Traverse the two arrays in turn, store the array elements in a hash table, and count the array elements at the same level. If it is 2, it is the intersection element of the two.
Method 3: Traverse any one of the two arrays, store the elements obtained by traversal in the hash table, then traverse the other array and query the established hash table at the same time. If it exists, it is an intersection element.
(2) When the lengths of two arrays are quite different, such as the length of array A is much greater than that of array B, the following methods can be adopted.
Method 1: Traverse the array with smaller length in turn, and method of bisection the array elements obtained by traversal in the long array. Specifically, two pointers to two arrays are set as elements, and the smaller number is searched in the other array. If it is found, there is an intersection, and the pointer of the target array points to the previous position. If you can't find it, you can also find a position so that the numbers in the target array after this position definitely don't exist in another array. Move the pointer of the target array directly to the previous position of this position, and search again until an array is empty. Because there may be duplicate numbers in two arrays, when the same number X is found in method of bisection, its subscript is I, so the lower bound of the next method of bisection becomes i+ 1 to avoid the repeated use of X.
Method 2: Use a method similar to method 1, but each search is based on the previous search, which can greatly reduce the length of the lookup table.
Method 3: The method is similar to method 2, but the way to traverse the array with smaller length is different. Starting from the head and tail of the array at the same time can further reduce the length of the lookup table.