Can your team beat its own algorithm?
Sort a shuffled deck as quickly and accurately as possible. Then redesign your strategy and try again. The cards are physical. The thinking is computer science.
Understand the problem
Your team receives one shuffled deck. Your job is to produce one correctly ordered deck, grouped by suit and ordered within each suit.
Target order
A 2 3 4 5 6 7 8 9 10 J Q K
Group all four suits. Choose a consistent suit order and keep every suit in ascending rank order.
Finish signal
- Combine the sorted cards into one deck.
- Turn the deck face down.
- Take both hands off the cards.
- Every teammate raises both hands.
Round 1: Solve first, explain later
Shuffle the deck. Do not stop for a long planning session. When your teacher says Go, solve the problem.
Teacher can use this timer, or a projected classroom stopwatch.
Sort
Move from a shuffled deck to the required order.
Signal
Deck face down. Hands up. Nobody touching cards.
Verify
A fast result only counts if the order is correct.
Round 2: Design before you execute
Shuffle again. This time your team gets one minute to plan without touching the cards. Decide how your algorithm will divide the work.
Roles
Who is responsible for each part of the problem?
Data layout
Where will suits, ranks, and partial results go?
Merge
How will separate results become one final deck?
Check
How will you verify correctness before signaling?
From cards to Java objects
In Java, each physical card can be represented as an object. Its visible properties become attributes, and operations on cards and decks become methods.
Object: Card
Attributes
Every card has data that describes its state.
suitrankThe physical card also has many details that our program may not need, such as artwork, wear, texture, or manufacturer. A class models only the attributes that matter to the problem.
The deck becomes an ArrayList
A deck is not one value. It is a collection of many Card objects. That makes it a natural bridge to arrays and ArrayList.
Collection of objects
Questions to notice
- How do we visit every card?
- How do we compare two cards?
- How do we move or swap cards?
- What information determines which card comes first?
- How do we know when the collection is sorted?
Methods describe what the system can do
shuffle()
Changes the order of the deck.
compareTo()
Defines how one card compares with another.
sort()
Applies an algorithm that rearranges the collection into the required order.
Your team invented a sorting algorithm
Different groups may reach the same correct deck using different processes. In CSA, we can describe and compare those strategies more precisely.
Selection sort thinking
Repeatedly find the next smallest card and place it into its final position.
Insertion sort thinking
Build a sorted region, inserting each new card into the correct place.
Divide and combine
Split the deck into smaller collections, sort those parts, then combine them. Human teams often discover this naturally.
Make your thinking visible
1. What changed?
Why was Round 2 faster, slower, or about the same?
2. What was your algorithm?
Describe your team's Round 2 strategy as a sequence of steps.
3. Objects and attributes
If a card were a Java object, which attributes would your program need?
4. Arrays and methods
How is a deck like an array or ArrayList? What methods would a Deck class need?
One challenge, four CSA ideas
Objects
Each card is one instance with its own state.
Attributes
Suit and rank describe the data we need.
Arrays
The deck is an ordered collection of Card objects.
Algorithms
Methods organize the steps that inspect, compare, move, and sort those objects.