交集N算法

 

 

选用”sort集合一 + 遍历集合二到已排序的集合一种进行查找并计算“,做为Intersection N Algorithm的实现算法:

 

算法测试与权衡见: http://blog.csdn.net/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

*/

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值