Java中 shuffle 算法的使用_java

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]

tag.120ask.com/jingyan/8lwv3lbtctwmaoskne.html

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;
            ListIterator<Object> it = (ListIterator<Object>) list
                    .listIterator();
            while (it.hasNext()) {
                it.next();
                it.set(array[i++]);
            }
        } else {
            List<Object> rawList = (List<Object>) 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;
        List<Integer> numList = new ArrayList<Integer>();
        List<Integer> firstList = new ArrayList<Integer>();
        List<Integer> secondList = new ArrayList<Integer>();
        List<Integer> thirdList = new ArrayList<Integer>();
        List<Integer> fourthList = new ArrayList<Integer>();

        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;
            ListIterator<Integer> it = (ListIterator<Integer>) fourthList.listIterator();
            while (it.hasNext()) {
                it.next();
                it.set((Integer) array[i++]);
            }
        } else {
            List<Integer> rawList = (List<Integer>) 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();
    }

    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级别,大概如下:

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库效率较低时,可以考虑使用其他方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成迷宫可以使用深度优先搜索算法或者随机化Prim算法。以下是使用深度优先搜索算法生成迷宫的Java代码示例: ```java import java.util.Random; public class MazeGenerator { private int[][] maze; private int width; private int height; public MazeGenerator(int width, int height) { this.width = width; this.height = height; maze = new int[height][width]; } public int[][] generateMaze() { // Initialize maze with all walls for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { maze[i][j] = 1; } } Random rand = new Random(); int startX = rand.nextInt(width); int startY = rand.nextInt(height); // Create path maze[startY][startX] = 0; createPath(startX, startY); return maze; } private void createPath(int x, int y) { int[][] directions = {{0,-1},{0,1},{-1,0},{1,0}}; Random rand = new Random(); // Shuffle directions for (int i = 0; i < directions.length; i++) { int randIndex = rand.nextInt(directions.length); int[] temp = directions[i]; directions[i] = directions[randIndex]; directions[randIndex] = temp; } for (int i = 0; i < directions.length; i++) { int newX = x + directions[i][0] * 2; int newY = y + directions[i][1] * 2; if (newX >= 0 && newX < width && newY >= 0 && newY < height && maze[newY][newX] == 1) { maze[y + directions[i][1]][x + directions[i][0]] = 0; maze[newY][newX] = 0; createPath(newX, newY); } } } } ``` 该代码使用递归实现深度优先搜索算法来构建迷宫。使用`generateMaze()`方法生成迷宫,返回一个二维数组表示迷宫,其0表示通路,1表示墙壁。 对于求解算法,可以使用广度优先搜索算法或A*算法。以下是使用广度优先搜索算法解决迷宫问题的Java代码示例: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private int[][] maze; private int[][] visited; private int startX; private int startY; private int endX; private int endY; private int width; private int height; public MazeSolver(int[][] maze, int startX, int startY, int endX, int endY) { this.maze = maze; this.startX = startX; this.startY = startY; this.endX = endX; this.endY = endY; this.width = maze[0].length; this.height = maze.length; visited = new int[height][width]; } public int[][] solve() { Queue<int[]> queue = new LinkedList<>(); queue.add(new int[]{startX, startY}); visited[startY][startX] = 1; while (!queue.isEmpty()) { int[] current = queue.poll(); if (current[0] == endX && current[1] == endY) { // Reached end break; } int[][] directions = {{0,-1},{0,1},{-1,0},{1,0}}; for (int i = 0; i < directions.length; i++) { int newX = current[0] + directions[i][0]; int newY = current[1] + directions[i][1]; if (newX >= 0 && newX < width && newY >= 0 && newY < height && maze[newY][newX] == 0 && visited[newY][newX] == 0) { visited[newY][newX] = visited[current[1]][current[0]] + 1; queue.add(new int[]{newX, newY}); } } } return visited; } } ``` 该代码使用广度优先搜索算法寻找从起点到终点的最短路径,返回一个二维数组表示每个节点到起点的距离。其0表示未访问,-1表示墙壁,其他数字表示距离。最终路径可以通过反向遍历visited数组得到。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值