选用”sort集合一 + 遍历集合二到已排序的集合一种进行查找并计算“,做为Intersection N Algorithm的实现算法:
算法测试与权衡见: http://blog.youkuaiyun.com/yang_net/archive/2010/10/21/5956428.aspx
/**
*
*/
package algorithm.intersection;
import java.util.Arrays;
/**
* intersection n algorithm
*
* @author yangwm Oct 21, 2010 9:35:14 PM
*/
public class IntersectionNAlgorithm {
/**
* sort + intersection + n
*
* @param left
* @param right
* @return
*/
public static int[] intersection(int[] left, int[] right, int n) {
/*
* if left size greater than right size then swap of address
* if n greater than left size, set left size to n
*/
if (left.length > right.length) {
int[] swap = left;
left = right;
right = swap;
}
if (n > left.length) {
n = left.length;
}
//System.out.println(left.length + ", " + right.length + ", " + n);
/*
* allocation maximum space for result
*/
int[] result = null;
if (left.length < right.length) {
result = new int[left.length];
} else {
result = new int[right.length];
}
/*
* sort
*/
Arrays.sort(left);
/*
* intersection calculate
*/
int rightIdx = 0;
int resultIdx = 0;
while (rightIdx < right.length && resultIdx < n) {
int rightValue = right[rightIdx];
rightIdx++;
int resultValue = Arrays.binarySearch(left, rightValue);
if (resultValue > -1) {
result[resultIdx] = rightValue;
resultIdx++;
}
}
//System.out.println(left.length + ", " + rightIdx + ", " + resultIdx);
/*
* actual size of result
*/
return Arrays.copyOf(result, resultIdx);
}
/**
* @param args
*/
public static void main(String[] args) {
int[] left = { 5, 9, 2, 8, 11 };//{ 5, 9, 2, 8, 11, 7, 18, 10, 12, 3 };
int[] right = { 2, 7, 6, 8, 9, 18, 11, 5, 3, 20 };
int n = 3;
System.out.println("------IntersectionNAlgorithm Array intersection-------");
/*
* intersection and test print
*/
long begin = System.nanoTime();
int[] result = intersection(left, right, n);
long end = System.nanoTime();
System.out.println("cosume: " + (end - begin) + " ns");
System.out.println(Arrays.toString(result));
}
}
/*
------IntersectionNAlgorithm Array intersection-------
cosume: 247518 ns
[2, 8, 9]
*/
/**
*
*/
package algorithm.intersection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* intersection n algorithm beanch
*
* @author yangwm Oct 21, 2010 9:50:18 PM
*/
public class IntersectionNAlgorithmBeanch {
/**
* @param args
*/
public static void main(String[] args) {
/*
*
*/
int leftTotal = 2000;
int rightTotal = 1000000;
int factor = (rightTotal / leftTotal) * (new Random().nextInt(5) + 1);
int n = 100;
/*
* test data
*/
List<Integer> leftList = new ArrayList<Integer>(leftTotal);
for (int i = 0; i < leftTotal; i++) {
leftList.add(i * factor);
}
Collections.shuffle(leftList);
List<Integer> rightList = new ArrayList<Integer>(rightTotal);
for (int i = 0; i < rightTotal; i++) {
rightList.add(i);
}
Collections.shuffle(rightList);
System.out.println("test begin leftTotal: " + leftTotal + ", rightTotal: " + rightTotal + ", factor: " + factor + ", n: " + n);
System.out.println("test data leftList.size(): " + leftList.size() + ", rightList.size(): " + rightList.size());
//-----------------------------------------------------------------
int[] leftArray = new int[leftList.size()];
for (int i = 0; i < leftArray.length; i++) {
leftArray[i] = leftList.get(i);
}
int[] rightArray = new int[rightList.size()];
for (int i = 0; i < rightArray.length; i++) {
rightArray[i] = rightList.get(i);
}
System.out.println("-------IntersectionNAlgorithm Array intersection-------");
/*
* intersection and test print
*/
long begin = System.currentTimeMillis();
int[] resultArray = IntersectionNAlgorithm.intersection(leftArray, rightArray, n);
long end = System.currentTimeMillis();
System.out.println("cosume: " + (end - begin) + " ms");
System.out.println("resultArray.length: " + resultArray.length);
}
}
/*
vm args: -server -Xms256m -Xmx256m -XX:PermSize=64m
test begin leftTotal: 2000, rightTotal: 1000000, factor: 1000, n: 100
test data leftList.size(): 2000, rightList.size(): 1000000
-------IntersectionNAlgorithm Array intersection-------
cosume: 15 ms
resultArray.length: 100
*/