Answered step by step
Verified Expert Solution
Question
1 Approved Answer
case study deck of cards (java) B. Selection Shuffle Consider the following algorithm that starts with an array named cards that contains 52 cards and
case study deck of cards (java)
B. Selection Shuffle Consider the following algorithm that starts with an array named cards that contains 52 cards and creates an array named shuffled. We will call this algorithm the "selection shuffle." Initialize shuffled to contain 52 "empty" elements. Then for k = 0 to 51, - Repeatedly generate a random integer j between 0 and 51, inclusive until cards[i] contains a card (not marked as empty); - Copy cards[i] to shuffled[k]; - Set cards[i] to empty This approach finds a suitable card for the kth position of the deck. Unsuitable candidates are any cards that have already been placed in the deck. While this is a more promising approach than the perfect shuffle, its big defect is that it runs too slowly. Every time an empty element is selected, it has to loop again. To determine the last element of shuffled requires an average of 52 calls to the random number generator. A better version, the efficient selection shuffle," works as follows: For k = 51 down to 1, - Generate a random integer r between 0 and k, inclusive; - Exchange cards[k] and cards[r]. This has the same structure as selection sort: For k = 51 down to 1, - Find r, the position of the largest value among cards[0] through cards[k]; - Exchange cards(k) and cards[r]. The selection shuffle algorithm does not require to a loop to find the largest (or smallest) value to swap, so it works quickly. (Submission 3) Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started