【算法】乱序检查

乱序检查。通过实验检查表中的乱序代码是否能够产生预期的效果。编写一个程序

ShuffleTest,接受命令行参数M和N,将大小为M的数组打乱N次且在每次打乱之前都将数组

重新初始化为a[i] = i。打印一个MxM的表格,对于所有的列j,行i表示的是i在打乱后

落到j的位置的次数。数组中的所有元素的值都应该接近于N/M。

乱序代码:(StdRandom来自http://algs4.cs.princeton.edu/code/stdlib.jar

    /**
     * Rearrange the elements of a double array in random order.
     */
    public static void shuffle(double[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {
            int r = i + uniform(N-i);     // between i and N-1
            double temp = a[i];
            a[i] = a[r];
            a[r] = temp;
        }
    }

实验代码:

package com.beyond.algs4.experiment;

import com.beyond.algs4.lib.StdIn;
import com.beyond.algs4.lib.StdOut;
import com.beyond.algs4.lib.StdRandom;

public class ShuffleTest {

    public static class RandomArray {
        private int M = 0;
        private int[] a = null;

        public int[] getA() {
            return a;
        }

        public void setA(int[] a) {
            this.a = a;
        }

        public RandomArray(int M) {
            this.M = M;
            this.a = new int[M];
            for (int i = 0; i < M; i++) {
                a[i] = i;
            }
        }
    }
    
    public static class RandomChecker {
        private double[][] matrix = null;
        public double[][] getMatrix() {
            return matrix;
        }


        private int M = 0;
        private int N = 0;
        
        public RandomChecker(int M, int N) {
            this.M = M;
            this.N = N;
            matrix = new double[M][];
            for (int i = 0; i < matrix.length; i++) {
                matrix[i] = new double[M];
            }
        }
        
        public void execute() {
            for (int n = 0; n < this.N; n++) {
                RandomArray c = new RandomArray(M);
                int[] a = c.getA();
                StdRandom.shuffle(a);
                for (int k = 0; k < a.length; k++) {
                    int i = a[k];
                    int j = k;
                    matrix[i][j] += 1;
                }
                
            }
        }
        
        public void print() {
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < M; j++) {
                    StdOut.print(matrix[i][j] + "\t");
                }
                StdOut.print("\n");
            }
        }
    }
    
    public static class BadRandomChecker {
        private double[][] matrix = null;
        public double[][] getMatrix() {
            return matrix;
        }


        private int M = 0;
        private int N = 0;
        
        public BadRandomChecker(int M, int N) {
            this.M = M;
            this.N = N;
            matrix = new double[M][];
            for (int i = 0; i < matrix.length; i++) {
                matrix[i] = new double[M];
            }
        }
        
        public void execute() {
            for (int n = 0; n < this.N; n++) {
                RandomArray c = new RandomArray(M);
                int[] a = c.getA();
                shuffle(a);
                for (int k = 0; k < a.length; k++) {
                    int i = a[k];
                    int j = k;
                    matrix[i][j] += 1;
                }
                
            }
        }
        
        public void print() {
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < M; j++) {
                    StdOut.print(matrix[i][j] + "\t");
                }
                StdOut.print("\n");
            }
        }
        
        public static void shuffle(int[] a) {
            int N = a.length;
            for (int i = 0; i < N; i++) {
                int r = StdRandom.uniform(N-i);     // between 0 and N-1
                int temp = a[i];
                a[i] = a[r];
                a[r] = temp;
            }
        }
    }

    public static void main(String[] args) {
        StdOut.println("Please input M: ");
        int M = StdIn.readInt();
        StdOut.println("Please input N: ");
        int N = StdIn.readInt();
        StdOut.println("Random Check: ");
        RandomChecker rc = new RandomChecker(M, N);
        rc.execute();
        rc.print();
        
        StdOut.println("Bad Random: ");
        BadRandomChecker br = new BadRandomChecker(M, N);
        br.execute();
        br.print();
    }
}

 

实验结果:

 Please input M:
10
Please input N:
1000

i\j12345678910
110810695102988510410111289
210210296991138488105102109
310199849210310496118100103
410710411288961041019884106
576104111102971131019311093
61071039112196123861038783
7981039498941091168998101
8977991102949710599107129
99810410211693107105979088
10106961248011674989711099

 

Please input M:
5
Please input N:
100

i\j12345
11317222028
22715251914
31523222317
41926181720
52619132121

结果分析:
当M<N时,数组中的所有元素的值都应该接近于N/M。但当M>N时,无法得出结论。

糟糕的打乱。假设在我们的乱序代码中你选择的是一个0到N-1而非i到N-1之间的随机整数。

证明得到的结果并非均匀的分布在N!种可能性之间。用乱序检查的测试检验这个版本。

实验结果:

Please input M:
10
Please input N:
1000

i\j12345678910
1951011318910383114969395
20107112100116128118111100108
30629811913411814613613255
40541001181361801361399542
504782154146188187977227
6032751612281101471366447
705413921343776021115152
801482522348665023260130
903956153233313817433
10905058141711131611

Please input M:
5
Please input N:
100

i\j12345
11818231724
2022293217
3015314014
404410838
5821737

 

参考资料:

算法 第四版  谢路云 译 Algorithms Fourth Edition [美] Robert Sedgewick, Kevin Wayne著

http://algs4.cs.princeton.edu/home/

源码下载链接:

 http://pan.baidu.com/s/1pJ0xicF

转载于:https://www.cnblogs.com/richaaaard/p/4576969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值