简体   繁体   English

Java排列2

[英]Java permutations 2

I asked a question on helping me with this question about a week ago 大约一周前,我问了一个有关帮助我解决此问题的问题

Java permutations Java排列

, with a problem in the print permutation method. ,而打印排列方法存在问题。 I have tidied up my code and have a working example that now works although if 5 is in the 5th position in the array it doesn't print it. 我整理了一下代码,并有了一个可以运行的示例,尽管如果5在数组的第5位,则不会打印它。 Any help would be really appreciated. 任何帮助将非常感激。

 package permutation;

public class Permutation {

static int DEFAULT = 100;

public static void main(String[] args) {
    int n = DEFAULT;
    if (args.length > 0)
        n = Integer.parseInt(args[0]);
    int[] OA = new int[n];
    for (int i = 0; i < n; i++)
        OA[i] = i + 1;
    System.out.println("The original array is:");
    for (int i = 0; i < OA.length; i++)
        System.out.print(OA[i] + " ");
    System.out.println();
    System.out.println("A permutation of the original array is:");
    OA = generateRandomPermutation(n);
    printArray(OA);
    printPermutation(OA);
}

static int[] generateRandomPermutation(int n)// (a)
{
    int[] A = new int[n];
    for (int i = 0; i < n; i++)
        A[i] = i + 1;
    for (int i = 0; i < n; i++) {
        int r = (int) (Math.random() * (n));
        int swap = A[r];
        A[r] = A[i];
        A[i] = swap;
    }
    return A;
}

static void printArray(int A[]) {
    for (int i = 0; i < A.length; i++)
        System.out.print(A[i] + " ");
    System.out.println();
}

static void printPermutation(int[] p)

{
    int n = p.length-1;
    int j = 0;
    int m;
    int f = 0;

    System.out.print("(");
    while (f < n) {
        m = p[j];
        if (m == 0) {
            do
                f++;
            while (p[f] == 0 && f < n);
            j = f;
            if (f != n)
                System.out.print(")(");
        } 
        else {
            System.out.print(" " + m);
            p[j] = 0;
            j = m - 1;
        }
    }
    System.out.print(" )");
}
}

I'm not too crazy about 我不太疯

int n = p.length-1;

followed by 其次是

while (f < n) {

So if p is 5 units long, and f starts at 0, then the loop will be from 0 to 3. That would seem to exclude the last element in the array. 因此,如果p为5个单位长,并且f从0开始,则循环将从0到3。这似乎排除了数组中的最后一个元素。

You can use the shuffle method of the Collections class 您可以使用Collections类的shuffle方法

Integer[] arr = new Integer[] { 1, 2, 3, 4, 5 };
List<Integer> arrList = Arrays.asList(arr);
Collections.shuffle(arrList);
System.out.println(arrList);

I don't think swapping each element with a random other element will give a uniform distribution of permutations. 我不认为将每个元素与其他随机元素交换不会产生排列的均匀分布。 Better to select uniformly from the remaining values: 最好从其余值中统一选择:

Random rand = new Random();
ArrayList<Integer> remainingValues = new ArrayList<Integer>(n);
for(int i = 0; i < n; i++)
    remainingValues.add(i);
for(int i = 0; i < n; i++) {
    int next = rand.nextInt(remainingValues.size());
    result[i] = remainingValues.remove(next);
}

Note that if order of running-time is a concern, using an ArrayList in this capacity is n-squared time. 请注意,如果需要考虑运行时间的顺序,则以这种能力使用ArrayList的时间为n平方时间。 There are data-structures which could handle this task in n log n time but they are very non-trivial. 有一些数据结构可以在n log n次之内处理此任务,但是它们非常重要。

This does not answer the problem you have identified. 这不能解决您确定的问题。

Rather i think it identifies a mistake with your generateRandomPermutation(int n) proc. 而是我认为它标识了您的generateRandomPermutation(int n)proc的一个错误。

If you add a print out of the random numbers generated (as i did below) and run the proc a few times it allows us to check if all the elements in the ARRAY TO BE permed are being randomly selected. 如果您从生成的随机数中添加打印品(如我在下面所做的那样),然后运行proc几次,它可以让我们检查是否要随机选择阵列中的所有元素。

static int[] generateRandomPermutation(int n)
{
    int[] A = new int[n];
    for (int i = 0; i < n; i++)
        A[i] = i + 1;
     System.out.println("random nums generated are: ");
    for (int i = 0; i < n; i++) {
        int r = (int) (Math.random() * (n));
         System.out.print(r + " ");

Run the proc several times. 运行proc几次。 Do you see what i see? 你看见我看到的了吗?

Jerry. 杰瑞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM