This code is supposed to perform a "perfect shuffle" on a deck of cards. It represents the division of a deck into two decks and then the subsequent interleaving of them.
When I pass in an array and then print out the elements in the returned array, the elements are evidently shuffled. However, as the array in the perfectShuffle method is an alias of whatever array I pass into it, the original array back in the main method should also be "shuffled".
However, when I print out the array after performing the perfect shuffle method on it, the elements are not shuffled. When I check the same returned array, it is shuffled... I'm not sure why it isn't working. Could someone explain this to me?
public class Shuffler {
private static final int SHUFFLE_COUNT = 1;
private static final int VALUE_COUNT = 9;
public static void main(String[] args) {
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive perfect shuffles:");
int[] values1 = new int[VALUE_COUNT];
for (int i = 0; i < values1.length; i++) {
values1[i] = i;
}
perfectShuffle(values1);
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++) {
System.out.print(" " + values1[k]);
}
System.out.println();
}
System.out.println();
}
public static int[] perfectShuffle(int[] values) {
int[] shuffle = new int[values.length];
int k=0;
for(int j=0; j<(values.length+1)/2; j++)
{
shuffle[k]=values[j];
k+=2;
}
k=1;
for(int j=(values.length+1)/2; j<values.length; j++)
{
shuffle[k]=values[j];
k+=2;
}
values=shuffle;
return values;
}
}