<<

A Deck of Cards

• A deck consists of 52 cards Algorithms for Cards • A card has a – Suit: spades, clubs, hearts, or diamonds – Value: 2, 3, 4, ..., 10, Jack, Queen, King, or Ace ♠ ♣ ♦ ♥ • The algorithms we discuss can be made to work with any number of cards

Algorithms Come First Some Card Concerns

• We'll look at some common card • We'll consider three things manipulations and then simulate them – How can we sort cards in order from • This will help us design classes for smallest to largest (or from largest to representing cards smallest)? – How can you a deck of cards? • Rule of thumb: first work out your – How can you shuffle a deck of cards? algorithms by hand, and then use your understanding of them to design good • Each of these questions lead to programs interesting computer science questions

Sorting Your Hand Sorting: Method 1

• Deal 7 cards 4♣ T♠ Q♦ 4♥ 9♦ J♦ A♦ • Put the smallest – Ignore suits (for card in your hand 4♣ T♠ Q♦ 4♥ 9♦ J♦ A♦ now!) face-up on the table – Aces are high • Repeat this until all • Put them into sorted your cards are on order, lowest to 4♣ 4♥ 9♦ T♠ J♦ Q♦ A♦ the table ♣ ♥ ♦ ♠ ♦ ♦ ♦ highest, left to right or • Pick up your cards, 4 4 9 T J Q A • Try it! How do you 4♥ 4♣ 9♦ T♠ J♦ Q♦ A♦ they're now sorted! do it? Sorting: Method 2 Two Sorting Methods

• Divide your hand into • Method 1 is called selection sort ♣ ♠ ♦ ♥ ♦ ♦ ♦ a sorted and 4 T Q 4 9 J A • Method 2 is called insertion sort unsorted part • Put the smallest card • Both of these algorithms are good for from the unsorted sorting small numbers of objects part onto the end of – If you're sorting thousands or millions of the sorted part 4♣ 4♥ 9♦ T♠ J♦ Q♦ A♦ things, these methods are too slow • Repeat until all your – There are faster ways to sort: quicksort, cards are sorted mergesort, , etc.

Selection Sort in Detail Sorting with Suits

• Use two piles, one called sorted, the • When two cards have the same value, other called unsorted we can order them if we specify an • Initially, everything is in unsorted order for the suits • Pick the smallest card from unsorted, • There are 24 different ways to order the and put it face up on top of sorted suits • Repeat the previous step until • Pick one, such as ♠ < ♥ < ♦ < ♣ like unsorted is empty --- then everything – This means: 7♠ < 7♥ < 7♦ < 7♣ is in sorted order in sorted!

Cutting Cards by Hand Cutting Cards • Not many people can pick up exactly 26 • The standard cut cards from a deck all the time! – Put the deck on the table – Although it can be done, with practice! – Pick up the top half of the deck and place it • After a standard cut, the previous top to the right and bottom cards are now together in – Pick up the bottom half and put it on top of the middle of the pack the other half – Cheaters & magicians can exploit this fact! • This is easy to do by hand, but tricky to • There are fancier cuts, e.g. divide the do efficiently with a computer! deck into 3 parts, and rearrange them What Does Cutting Do? What Does Cutting Do?

1 2 3 4 5 6 7 8 9 10 11 12 13 • Think for a moment how cards are 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ T♥ J♥ Q♥ K♥ A♥ rearranged in a standard cut • How does the deck change? • Let's look at a simplified example using only the hearts 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ T♥ J♥ Q♥ K♥ A♥ 7♥ 8♥ 9♥ T♥ J♥ Q♥ K♥ A♥ 2♥ 3♥ 4♥ 5♥ 6♥ 1 2 3 4 5 6 7 8 9 10 11 12 13

A Neat Connection Weird Cuts: A Slow Cut

• Cutting a deck of cards is the same as Put the bottom card of the deck on the top doing cut and paste in a word Repeat the previous action 26 or so times processor! • People can't do this very quickly, but – Replace cards with letters computers can – Imagine highlighting the left part – Computers don't move physical cards, only – Then "drag" it to the end of the right part representations of them

Weird Cuts: Another Slow Cut Weird Cuts: A Flip Cut

From the top of the deck, deal 26 or so cards – Hold the deck in your right hand face-up onto the table – Pick up the top half in your left hand Turn over the pile on the table – Turn the pile in your left hand upside down Place the pile on the table on top of the pile – Turn the pile in your right hand upside in your hand down • Admittedly, these two cuts won't – Put the pile in your left on the pile in your impress your buddies (unless right they are CS students!) – Turn the deck upside down (so it's now right-side up) Different Views of the Same Shuffling Cards Thing • There are many ways to shuffle cards • Cutting a deck of cards is the same as – Milk shuffle: repeatedly remove the top and – Doing cut and paste in a word processor bottom cards – Rotating a sequence in a circular fashion – Riffle shuffle: cut the deck into two piles, • This is what the slow cuts did directly and interleave the cards – Swapping unequal segments of a • A perfect riffle shuffle is when you cut the deck sequence into two piles of 26, and perfectly alternate cards from each pile • This is what the flip cut did directly • Also known as a faro shuffle, or a weave shuffle

Shuffling Cards Random Shuffle

• We will consider two sorts of shuffles: • We want to put the deck into a –A random shuffle, where the deck is completely random order randomly permuted – We want to randomly permute the deck – The standard riffle shuffle, where the • This means that each card has the deck is cut into two approximately equal same chance of being in any position halves, and the deck is re-formed by alternately adding cards from each half • Ideally, we want to do this quickly, and without using extra memory

52 Pick Up Faro Shuffles

• Here's one way to generate a random • To do a faro shuffle permutation by hand: – Make two piles of exactly 26 cards each – Throw the deck in the air so the cards fly – Alternate choosing a card from each pile, everywhere placing the selected card on a new pile – Then pick them up, in no particular order – If the top card remains the top card, it's • Card players don't usually shuffle cards called an out shuffle; otherwise it's an in this way, but computers often do shuffle • 8 out shuffles in a row returns a deck to it's original order! Imperfect Riffle Shuffles Which Shuffle to Choose?

– Make two piles of roughly 26 cards each • If you are writing a , you – Alternate choosing clumps of, say, 0 to 3 probably want cards from each pile, placing the clump on – Perfect riffle shuffles don't mix up the cards the new pile very well • We can program a computer to do • Thus, either a random permutation or either perfect or imperfect riffle shuffles an imperfect riffle shuffle will do • How can you create a random permutation of cards?

Random Permutations: Random Permutations: Method 1 Method 2 • This simple method works very well: – Hold the deck in your hand For i = 1 to 52 do – Select a random card and place it face Choose R to be a random number from 1 to 52 down on a pile on the table Swap card i with card R – Repeat the previous step until you no End for longer hold any cards in your hand • It's fast, doesn't use extra memory, and • This is simple, but it requires twice as is easy to understand and program much memory as the previous method – Hard to do by hand, though! – That's okay for only 52 cards

Pseudocode for Method 2 Pseudocode for Method 2

• Call the pile of cards in your hand unprocessed contains all the cards unprocessed processed is empty • Call the pile of cards on the table While not unprocessed.empty() do processed Remove a randomly chosen card C from • Initially, all the cards are in unprocessed unprocessed, and processed is Put C on top of processed empty End while Representing a Permutation Permutations of ints

• Our goal: generate random • This puts 1 to 52 into a vector: permutations of cards vector p; // processed • But let's look at the bare essentials first Vector u; // unprocessed • Let's work with a vector instead for(int i=1; i<=52; ++i) of cards u.push_back(i); – Imagine writing the numbers 1 to 52 on • We can swap u[i] with u[j] using your cards swap(u[i],u[j]) • Later, you can use a Card class

Method 2 in C++ Method 2 Modified

•The vectors u and p have just been • We can rewrite this method to so it only correctly initialized, so here's the loop: uses one vector while (u.size() != 0) { • The trick is to notice that the number of int r = 1 + (rand() % 52); cards in processed and p.push_back(u[r]); unprocessed is always 52 swap(u[r],u[u.size() - 1]); u.pop_back(); // remove u's last • We can partition one 52-element vector // element into two parts, one for processed, the } // while other for unprocessed

The following slides Red numbers are show how to randomly selected randomly permute a vector ... swap

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

Algorithm: move randomly selected numbers from unprocessed the unprocessed part into the processed part, stopping when the unprocessed part is empty ... swapped swap

6 2 3 4 5 1 7 8 9 6 2 3 4 5 1 7 8 9 processed unprocessed processed unprocessed

swap swapped

6 4 3 2 5 1 7 8 9 6 4 8 2 5 1 7 3 9

processed unprocessed processed unprocessed

swap with itself swapped with itself

6 4 8 2 5 1 7 3 9 6 4 8 2 5 1 7 3 9

processed unprocessed processed unprocessed swap swapped

6 4 8 2 5 1 7 3 9 6 4 8 2 3 1 7 5 9 processed unprocessed processed unprocessed

swap swapped

6 4 8 2 3 1 7 5 9 6 4 8 2 3 7 1 5 9

processed unprocessed processed unprocessed

swap swapped

6 4 8 2 3 7 1 5 9 6 4 8 2 3 7 9 5 1

processed unprocessed processed unprocessed swap swap

6 4 8 2 3 7 9 5 1 6 4 8 2 3 7 9 1 5

processed unprocessed processed unprocessed

6 4 8 2 3 7 9 1 5

processed