有人说用归并算法,但是执行下来时间远远不止2s。
在下实在想不出还有什么好办法,希望各位能给个提示或者解法,谢谢。
下面是我的测试代码:
public class TestA {
public static void main(String[] args) {
long[] a = new long[50000000];
long num = 13000000000l;
for (int i = 0; i < a.length; i++) {
a[i] = (num + i);
}
long[] b = new long[10000000];
long num2 = 14000000000l;
for (int i = 0; i < b.length - 2; i++) {
b[i] = (num2 + i);
}
b[9999999] = 13000000000l;
b[9999998] = 13000000001l;
long[] c = new long[a.length+b.length];
long start = System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = 0; i < b.length; i++) {
c[i + a.length] = b[i];
}
System.out.println("start");
sort(c, 0, c.length-1);
long end = System.nanoTime();
System.out.println(System.currentTimeMillis() - start);
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
public static void sort(long[] data, int left, int right) {
if (left < right) {
// 找出中间索引
int center = (left + right) / 2;
// 对左边数组进行递归
sort(data, left, center);
// 对右边数组进行递归
sort(data, center + 1, right);
// 合并
merge(data, left, center, right);
}
}
public static void merge(long[] data, int left, int center, int right) {
long[] tmpArr = new long[data.length];
int mid = center + 1;
// third记录中间数组的索引
int third = left;
int tmp = left;
while (left <= center && mid <= right) {
// 从两个数组中取出最小的放入中间数组
if (data[left] <= data[mid]) {
if(data[left] == data[mid]){
System.out.println(data[left]);
}
tmpArr[third++] = data[left++];
} else {
tmpArr[third++] = data[mid++];
}
}
// 剩余部分依次放入中间数组
while (mid <= right) {
tmpArr[third++] = data[mid++];
}
while (left <= center) {
tmpArr[third++] = data[left++];
}
// 将中间数组中的内容复制回原数组
while (tmp <= right) {
data[tmp] = tmpArr[tmp++];
}
}
}
To provide an idea, what we are looking for is the same phone number in the two arrays. Then we build a dictionary tree from the phone numbers in the first array.
In the second time, just search it directly in the dictionary tree. The overall complexity is O(N * 11) = O(N). 11 digits per phone number. The total complexity is only O(N).
Reference Zhihu: https://zhuanlan.zhihu.com/p/...
Just found a method, the execution time is about 2s:
The main idea of this method is to sort first, then use the Arrays.binarySearch() method to perform a binary query and return the matching array subscript.
I modified the method of obtaining the data source and found that if a random data source is used, the time consumed is about 8s. The error time is mainly consumed in the sort() sorting method. The rules of the data source still affect the time complexity of the sorting algorithm.
The code is modified as follows:
public class TestB {
}
考虑bitmap, 参考https://github.com/RoaringBit...
RoaringBitmap aBM = new RoaringBitmap()
for (int i = 0; i < a.length; i++) {
}
...
RoaringBitmap interactionBM = RoaringBitmap.and(aBM, bBM)
for (int item: interactionBM) {
}
Using the above code, on my machine, it is 8s
http://tieba.baidu.com/p/3866...
C#, run locally, release, 611ms
redis SINTER (Returns all members of a set that is the intersection of all given sets.)
If this operation can only be performed in an array, there is no trick. At least the smaller array must be traversed, and even sorting is inevitable. And if the array contents can be exported to other data structures, it seems to violate the original intention of the question. Did the questioner want to test array operations?
Let’s take a simpler method, which only takes about 200ms on MBP. Ordinary Pentium G2020 only takes 250msUm, this algorithm is completely impossible.
In fact, algorithm is the key to this question.
I suggest you read the first chapter of Programming Pearls. Will provide good ideas.
First of all, the question must be refined, that is, does the mobile phone number have to be a Chinese mobile phone number only? Otherwise the number would be much higher.
Let me briefly explain how phone numbers are stored in Zhuji.
It only uses a binary number to store all mobile phone numbers. A binary digit can be very long. The length is equal to the largest possible phone number. For example, 13999999999, in fact, 13 can be omitted. Our number is a binary number with 999999999 digits.
On each digit, if there is this phone number, it is marked as 1, if not, it is marked as 0.
For the sake of simplicity, I will assume that the range of the mobile phone number is 0-9. We first prepare a 10-digit binary Number 0000000000.
Assume that the first array has 3 phone numbers, namely 1, 5, and 7. Then the binary number storing this array is 0010100010. The 1, 5, and 7 bits of this number are 1, and the other bits are 0. .
Suppose the second array has 6 phone numbers, namely 1, 2, 3, 4, 7, and 8. Then the binary number storing this array is 0110011110, the 1, 2, 3, 4, 7, and 8 digits of this number. The upper bit is 1 and the other bits are 0.
So how to find out the same phone number in these two arrays?
You only need to do the "bitwise AND" (AND) operation in bit operations. If both of the same bits are 1, it is still 1. As long as one of them is 0, it is 0.
0010100010 & 0110011110 = 0010000010
I found out immediately that it is a binary number with 1 in the 1st and 7th digits.