Page 506 - AP Computer Science A, 7th edition
P. 506
(C) Algorithm 2 will lead to more permutations of the deck than Algorithm 1.
(D) In terms of run time, Algorithm 2 is more efficient than Algorithm 1.
(E) If Algorithm 1 is repeated several times, it may return the deck to its original state.
7. The following shuffle method is used to shuffle the cards in the Deck class.
Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
Line 6:
Line 7:
Line 8:
Line 9:
Line 10:
Line 11:
The method does not work as intended. Which of the following changes should be made to correct the method?
public void shuffle() {
for (int k = NUMCARDS; k > 0; k--) {
int randPos = (int) (Math.random() ∗(k+1));
//swap randomly selected card with card at position k
Card temp = cards[k]; cards[k] = cards[randPos]; cards[randPos] = temp;
} }