Java中 shuffle 算法的使用

2016-02-19 10:35 16 1 收藏

下面图老师小编要向大家介绍下Java中 shuffle 算法的使用,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

【 tulaoshi.com - 编程语言 】

Fisher–Yates shuffle 基本思想(Knuth shuffle ):

To shuffle an array a of n elements (indices 0..n-1):
  for i from n − 1 downto 1 do
       j ← random integer with 0 ≤ j ≤ i
       exchange a[j] and a[i]

JDK源代码如下:
代码如下:

/**
     * Moves every element of the List to a random new position in the list.
     *
     * @param list
     *            the List to shuffle
     *
     * @throws UnsupportedOperationException
     *             when replacing an element in the List is not supported
     */
    public static void shuffle(List? list) {
        shuffle(list, new Random());
    }

    /**
     * Moves every element of the List to a random new position in the list
     * using the specified random number generator.
     *
     * @param list
     *            the List to shuffle
     * @param random
     *            the random number generator
     *
     * @throws UnsupportedOperationException
     *             when replacing an element in the List is not supported
     */
    @SuppressWarnings("unchecked")
    public static void shuffle(List? list, Random random) {
        if (!(list instanceof RandomAccess)) {
            Object[] array = list.toArray();
            for (int i = array.length - 1; i 0; i--) {
                int index = random.nextInt(i + 1);
                if (index 0) {
                    index = -index;
                }
                Object temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }

            int i = 0;
            ListIteratorObject it = (ListIteratorObject) list
                    .listIterator();
            while (it.hasNext()) {
                it.next();
                it.set(array[i++]);
            }
        } else {
            ListObject rawList = (ListObject) list;
            for (int i = rawList.size() - 1; i 0; i--) {
                int index = random.nextInt(i + 1);
                if (index 0) {
                    index = -index;
                }
                rawList.set(index, rawList.set(i, rawList.get(index)));
            }
        }
    }

测试代码,为了确保每种情况的初始化一样,使用了多个容器:
代码如下:

public class javaShuffle {
    public static int temp = 0;
    public static long start;
    public static long end;

    public static void main(final String args[]) {

        Object changeTemp;
        ListInteger numList = new ArrayListInteger();
        ListInteger firstList = new ArrayListInteger();
        ListInteger secondList = new ArrayListInteger();
        ListInteger thirdList = new ArrayListInteger();
        ListInteger fourthList = new ArrayListInteger();

        for (int i = 1; i = 100000; i++) {
            numList.add(i);
            firstList.add(i);
            secondList.add(i);
            thirdList.add(i);
            fourthList.add(i);
        }

        // first shuffle,use changeTemp
        getStartTime();
        int randInt = 0;
        for (int i = 0, length = firstList.size(); i length; i++) {
            randInt = getRandom(i, firstList.size());
            changeTemp = firstList.get(i);
            firstList.set(i, firstList.get(randInt));
            firstList.set(randInt, javaShuffle.temp);
        }
        getEndTime("first shuffle run time ");

        // second shuffle,exchange list
        getStartTime();
        for (int i = 0, length = secondList.size(); i length; i++) {
            randInt = getRandom(i, secondList.size());
            secondList.set(i, secondList.set(randInt, secondList.get(i)));
        }
        getEndTime("second shuffle run time");

        // third shuffle, change generate random int
        getStartTime();
        Object[] tempArray = thirdList.toArray();
        Random rand = new Random();
        int j = 0;
        for (int i = tempArray.length - 1; i 0; i--) {
            j = rand.nextInt(i + 1);
            thirdList.set(i, thirdList.set(j, thirdList.get(i)));
        }

        getEndTime("third shuffle run time ");

        // fourth shuffle, simulate java shuffle
        getStartTime();
        Random random = new Random();
        if (!(fourthList instanceof RandomAccess)) {
            Object[] array = fourthList.toArray();
            for (int i = array.length - 1; i 0; i--) {
                int index = random.nextInt(i + 1);
                if (index 0) {
                    index = -index;
                }
                Object temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }

            int i = 0;
            ListIteratorInteger it = (ListIteratorInteger) fourthList.listIterator();
            while (it.hasNext()) {
                it.next();
                it.set((Integer) array[i++]);
            }
        } else {
            ListInteger rawList = (ListInteger) fourthList;
            for (int i = rawList.size() - 1; i 0; i--) {
                int index = random.nextInt(i + 1);
                if (index 0) {
                    index = -index;
                }
                rawList.set(index, rawList.set(i, rawList.get(index)));
            }
        }
        getEndTime("fourth shuffle run time");

        // java shuffle
        getStartTime();
        Collections.shuffle(numList);
        getEndTime("java shuffle run time  ");
    }

    public static void swap(int a, int b) {
        javaShuffle.temp = a;
        a = b;
        b = javaShuffle.temp;
    }

    public static int getRandom(final int low, final int high) {
        return (int) (Math.random() * (high - low) + low);
    }

    public static void getStartTime() {
        javaShuffle.start = System.nanoTime();
    }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

    public static void getEndTime(final String s) {
        javaShuffle.end = System.nanoTime();
        System.out.println(s + ": " + (javaShuffle.end - javaShuffle.start) + "ns");
    }

}

如果数值较小,例如100000级别,则输出大概是:

first shuffle run time : 85029499ns
second shuffle run time: 80909474ns
third shuffle run time : 71543926ns
fourth shuffle run time: 76520595ns
java shuffle run time  : 61027643ns

first shuffle run time : 82326239ns
second shuffle run time: 78575611ns
third shuffle run time : 95009632ns
fourth shuffle run time: 105946897ns
java shuffle run time  : 90849302ns

first shuffle run time : 84539840ns
second shuffle run time: 85965575ns
third shuffle run time : 101814998ns
fourth shuffle run time: 113309672ns
java shuffle run time  : 35089693ns

first shuffle run time : 87679863ns
second shuffle run time: 79991814ns
third shuffle run time : 73720515ns
fourth shuffle run time: 78353061ns
java shuffle run time  : 64146465ns

first shuffle run time : 84314386ns
second shuffle run time: 80074803ns
third shuffle run time : 74001283ns
fourth shuffle run time: 79931321ns
java shuffle run time  : 86427540ns

first shuffle run time : 84315523ns
second shuffle run time: 81468386ns
third shuffle run time : 75052284ns
fourth shuffle run time: 79461407ns
java shuffle run time  : 66607729ns

多次运行结果可能都不一样,但是基本java自带 shuffle速度最快,第三种方式次之。而第一种方法耗时最久。

如果是10000000级别,大概如下:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

first shuffle run time : 2115703288ns
second shuffle run time: 3114045871ns
third shuffle run time : 4664426798ns
fourth shuffle run time: 2962686695ns
java shuffle run time  : 3246883026ns first shuffle run time : 2165398466ns
second shuffle run time: 3129558913ns
third shuffle run time : 4147859664ns
fourth shuffle run time: 2911849942ns
java shuffle run time  : 4311703487ns first shuffle run time : 2227462247ns
second shuffle run time: 3279548770ns
third shuffle run time : 4704344954ns
fourth shuffle run time: 2942635980ns
java shuffle run time  : 3933172427ns first shuffle run time : 2200158789ns
second shuffle run time: 3172666791ns
third shuffle run time : 4715631517ns
fourth shuffle run time: 2950817535ns
java shuffle run time  : 3387417676ns first shuffle run time : 2201124449ns
second shuffle run time: 3203823874ns
third shuffle run time : 4179926278ns
fourth shuffle run time: 2913690411ns
java shuffle run time  : 3571313813ns first shuffle run time : 2163053190ns
second shuffle run time: 3073889926ns
third shuffle run time : 4493831518ns
fourth shuffle run time: 2852713887ns
java shuffle run time  : 3773602415ns

可以看出,第一种方法速度最快,而第四种最慢。java自带 shuffle速度也不理想。

在进行大数据处理的时候,如果使用java库效率较低时,可以考虑使用其他方式。

来源:https://www.tulaoshi.com/n/20160219/1595161.html

延伸阅读
枚举特点 1.用enum定义枚举类默认继承了java.lang.Enum类而不是继承了Object类。其中java.lang.Enum类实现了java.lang.Serializable和java.lang.Comparable两个接口 2.枚举类的构造函数只能使用private访问修饰符,如果省略了其构造器的访问控制符,则默认使用private修饰; 3.枚举类的所有实例必须在枚举类中显式列出,否则这个枚举类将...
冒泡排序的算法分析与改进 交换排序的基本思想是:两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。 应用交换排序基本思想的主要排序方法有:冒泡排序和快速排序。 代码如下: public class BubbleSort implements SortUtil.Sort{ public void sort(int[] data) { int temp; for(int i=0...
标签: Java JAVA基础
摘要:本文介绍了在JAVA环境下如何实现IDEA对称加密算法。由于电子商务和电子政务的普及,安全加密技术在其中应用非常广泛,对安全加密技术的要求也很高。目前在JAVA环境下实现IDEA加密具有很多的优势,因为JAVA是基于面向对象的编程语言,并且由于它的平台无关性能被大量应用于Internet的开发。 关键字:IDEA(Internation ...
内部类访问规则 •内部类可以直接访问外部类中的成员,包括私有。访问格式:外部类名.this •外部类要访问内部类必须创建内部类对象。 •内部类在成员位置上,可以被成员修饰符修饰。 代码如下: public class InnerClassDemo1 {      public static void main(String[] args){    &nbs...
final可以修饰类 ,成员变量,局部变量和方法。 1.final修饰成员变量 1.final成员变量的初始化 对于final修饰的变量,系统不会默认初始化为0 fina变量初始化方式: 在定义的时候初始化 final变量可以在初始化块中初始化,不可以在静态初始化块中初始化。 静态final变量可以在静态初始化块中初始化,不可以在初始化块中初始化。 fina变...

经验教程

566

收藏

9
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部