[英]Odd/Even Non-Working Perfect Shuffle Algorithm Error
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};
,我的测试就会工作private static int[] VALUES = {1,2,3,4,5};
通过,尽管可悲的是, private static int[] VALUES = {1,2,3,4,5,6}
没有通过。 任何帮助表示赞赏!
将循环条件固定为“更大”。 它确实会洗牌,它甚至可以处理奇数数组。 这样就行了。 用您的代码发现它(与数组的大小无关: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]
FWIW混战。 两者都只能在偶数数组大小下正常工作(取决于“正确”的定义),但它们不会因奇数而引发错误:
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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.