I am trying to write a shuffle
method for a card game program. The method works by dividing the deck in half, then interleaving the cards from alternate halves. The issue is, the method simply keeps the array as is. Nothing is being shuffled.
/**
* Apply a "perfect shuffle" to the argument.
* The perfect shuffle algorithm splits the deck in half, then interleaves
* the cards in one half with the cards in the other.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void perfectShuffle(int[] values) {
int[] shuffled = new int[values.length];
int k = 0;
for (int j = 0; j < (values.length + 1) / 2; j++){
shuffled[k] = values[j];
k += 2;
}
k = 1;
for (int j = (values.length + 1) / 2; j < values.length; j++){
shuffled[k] = values[j];
k += 2;
}
values = shuffled;
}
Where's the bug?