奇/偶非工作完美混洗算法错误

时间:2019-02-22 03:20:44

标签: java shuffle

private static final int SHUFFLE_COUNT = 5;


private static int[] VALUES = {1, 2, 3, 4, 5, 6};  


//broken for when  SHUFFLECOUNT and VALUES are EVEN
public static void perfectShuffle(int[] values) {
    int[] values1 = Arrays.copyOf(values, values.length);
    int k = 0; 
    for(int j = 0; j <= values.length / 2; j++) {
        values[k] = values1[j];
        k = k + 2; 
    }
    k = 1; 
    for(int j = (values.length / 2) + 1; j < values.length; j++) {
        values[k] = values1[j];
        k = k + 2; 
    }

}

public void testPerfectShuffle() {
    System.out.println("Results of " + SHUFFLE_COUNT +
                             " consecutive perfect shuffles:");
    for (int j = 1; j <= SHUFFLE_COUNT; j++) {
        perfectShuffle(VALUES);
        System.out.println("  " + j + ":" + Arrays.toString(VALUES));
    }
    System.out.println();
}

我正在努力实现一种完美的随机播放算法,由于某种原因,当测试的值的数量为奇数(例如 private static int[] VALUES = {1,2,3,4,5}; pass )时,我的测试就会起作用,尽管遗憾的是,{ {1}}否。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

将循环条件设置为“更大”是有帮助的。它确实会洗牌,它甚至可以处理奇数数组。如此运作 用您的代码发现它(与数组的大小无关:5,7 ..第一个数字始终保持在其位置)。 带有奇数大小的数组的未修改代码的输出:

  1:[1, 4, 2, 5, 3]
  2:[1, 5, 4, 3, 2]
  3:[1, 3, 5, 2, 4]
  4:[1, 2, 3, 4, 5]
  5:[1, 4, 2, 5, 3]

下面的代码表现类似-因为设计一个完全不同的随机播放算法不是问题-是吗?

如果我弄错了逻辑,请在您的问题中提供一些有关改组在逻辑上应如何正确工作的信息-否则,我无从得知,除了应改组所有数字(数组边界除外)(可能会保留) ,以下代码可以做到。

(而且我将testMethod设为静态,因此我可以从main调用它-为我简化测试)。

public class PerfectShuffle {

    private static final int SHUFFLE_COUNT = 5;

    private static int[] VALUES = {1, 2, 3, 4, 5, 6};

//broken for when  SHUFFLECOUNT and VALUES are EVEN
    public static void perfectShuffle(int[] values) {
        int[] values1 = Arrays.copyOf(values, values.length);
        int k = 0;
        //take the next bigger int for length/2(round up) 
        for (int j = 0; j < (values.length -1) / 2 +1 ; j++) {
            values[k] = values1[j];
            k = k + 2;
        }
        k = 1;
        //take the next bigger int for length/2(round up)
        for (int j = (values.length -1)/ 2  + 1; j < values.length; j++) {
            values[k] = values1[j];
            k = k + 2;
        }

    }

    public static void testPerfectShuffle() {
        System.out.println("Results of " + SHUFFLE_COUNT
                + " consecutive perfect shuffles:");
        for (int j = 1; j <= SHUFFLE_COUNT; j++) {
            perfectShuffle(VALUES);
            System.out.println("  " + j + ":" + Arrays.toString(VALUES));
        }
        System.out.println();
    }
    public static void main(String ... args){
        testPerfectShuffle();
    }
}

样品运行:

Results of 5 consecutive perfect shuffles:
  1:[1, 4, 2, 5, 3, 6]
  2:[1, 5, 4, 3, 2, 6]
  3:[1, 3, 5, 2, 4, 6]
  4:[1, 2, 3, 4, 5, 6]
  5:[1, 4, 2, 5, 3, 6]

先进后退。两者都只能在偶数数组大小下正常工作(取决于“正确”的定义),但是它们不会因奇数而引发错误:

public static void inShuffle(int[] values) {
    int[] values1 = Arrays.copyOf(values, values.length);
    for (int j = 0; j < values.length / 2; j++) {
        values[j*2] = values1[j];
    }
    for (int j = 1; j < values.length / 2; j++) {
        values[j*2-1] = values1[(values.length / 2)+j-1];
    }
}

public static void outShuffle(int[] values) {
    int[] values1 = Arrays.copyOf(values, values.length);
    for (int j = 0; j < values.length / 2; j++) {
        values[j*2] = values1[(values.length / 2)+j];
    }
    for (int j = 0; j < values.length / 2; j++) {
        values[j*2+1] = values1[j];
    }
}