First-day of second semester AP Computer Science A challenge

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.

01

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

Heart suit symbolHearts
Diamond suit symbolDiamonds
Club suit symbolClubs
Spade suit symbolSpades

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

  1. Combine the sorted cards into one deck.
  2. Turn the deck face down.
  3. Take both hands off the cards.
  4. Every teammate raises both hands.
02

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.

00:00.00

Teacher can use this timer, or a projected classroom stopwatch.

1

Sort

Move from a shuffled deck to the required order.

2

Signal

Deck face down. Hands up. Nobody touching cards.

3

Verify

A fast result only counts if the order is correct.

03

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?

Think like a programmer: planning can reduce collisions, repeated work, missing data, and wasted motion.
04

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

public class Card { private String suit; private int rank; public Card(String suit, int rank) { this.suit = suit; this.rank = rank; } public String getSuit() { return suit; } public int getRank() { return rank; } }

Attributes

Every card has data that describes its state.

suitrank

The 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.

05

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

ArrayList<Card> deck = new ArrayList<>(); deck.add(new Card("Hearts", 1)); deck.add(new Card("Clubs", 13)); Card current = deck.get(0);

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?
06

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.

public int compareTo(Card other) { if (!suit.equals(other.suit)) { return suit.compareTo(other.suit); } return rank - other.rank; }
07

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.

The goal today is not to memorize sorting code. It is to notice that the same problem can have multiple correct algorithms with different tradeoffs.
08

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?

09

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.