Implement selection sort, which repeatedly finds the smallest remaining element and swaps it into place.
Implement insertion sort, which repeatedly shifts each element left past larger elements already in place.
Trace both algorithms by hand and count their swaps or shifts.
Explain why both algorithms reach the same final sorted order despite doing different amounts of work.
Key Vocabulary
Term
Definition
selection sort
Repeatedly finds the smallest value in the unsorted remainder of the array and swaps it into the next position.
insertion sort
Repeatedly takes the next element and shifts it left past every larger element already in place.
swap
Exchanging the values at two array positions, using a temporary variable to hold one of them during the exchange.
work vs. result
Different sorting algorithms can do very different amounts of work, but a correct sort must always reach the identical final order.
Selection Sort
On each pass, selection sort scans the unsorted remainder for its smallest value, then swaps that value into the next position.
for (int i = 0; i < data.length - 1; i++) {
int small = i;
for (int j = i + 1; j < data.length; j++) {
if (data[j] < data[small]) {
small = j;
}
}
if (small != i) {
int keep = data[i];
data[i] = data[small];
data[small] = keep;
}
}
⚠️ A Self-Swap Is Not a Swap
When the smallest remaining value is already at position i, small equals i and nothing needs to move. Counting that as a swap anyway inflates the count without reflecting any real work.
Insertion Sort
On each pass, insertion sort takes the next element and slides it left, past every already-sorted element bigger than it, until it finds its place.
for (int i = 1; i < data.length; i++) {
int key = data[i];
int j = i - 1;
while (j >= 0 && data[j] > key) {
data[j + 1] = data[j];
j = j - 1;
}
data[j + 1] = key;
}
The final line, placing key at data[j + 1] once the while loop stops, is not itself a counted shift: it is where the element ends up, not a move of an existing element.
Same Order, Different Work
📌 Two Algorithms, One Correct Answer
Sorting has exactly one correct final order for a given input. Selection sort and insertion sort can do very different amounts of work to get there, but a correct implementation of either must land on the identical sorted array.
Practice Questions
MCQ 1
Selection sort runs on [5,1,4,2]. What does the array look like after the FIRST pass of the outer loop?
A [1,5,4,2]
B [5,1,4,2]
C [1,2,4,5]
D [2,1,4,5]
✓ A. The smallest value overall is 1, at index 1, so it swaps with index 0.
MCQ 2
What does insertion sort do with each new element as it moves through the array?
A Shifts it left past every larger element already in the sorted portion, then places it
B Finds the global minimum and swaps it to the front
C Appends it to the end without moving anything else
D Removes it entirely if it is out of order
✓ A. This is the core insertion sort move: shift, then place, one element at a time.
MCQ 3
A grader counts a selection sort "swap" only when small != i. Why?
A Counting swap operations regardless of outcome is required by the Java language
B A self-swap (small == i) changes nothing, so it is not real work and should not inflate the count
C This distinction does not actually matter for correctness
D It always produces a larger reported number
✓ B. An element already in the right place being "swapped with itself" leaves the array unchanged and should not count as an operation performed.
MCQ 4
If two different, correct sorting algorithms run on the SAME input array, what must be true of their final results?
A They may differ, since different algorithms are allowed different final orders
B This cannot be determined without actually running the code
C They differ whenever the input contains duplicate values
D They must be identical: sorting has exactly one correct final order for a given input
✓ D. The correct final order is a property of the data, not of which algorithm produced it.
MCQ 5
What happens when insertion sort processes an array that is ALREADY fully sorted?
A It crashes, since there is nothing to sort
B Every while-loop condition is immediately false, so no shifting occurs and the array is returned unchanged with zero shifts
C It performs its maximum possible number of shifts anyway
D It reverses the array
✓ B. On already-sorted data, no element is ever bigger than the one before it, so the while condition never triggers a shift.
Tier 3 · AP Mastery
Mastery: Sorting Algorithms
MCQ 6
Selection sort runs on [3,1,2]. How many real swaps (small != i) occur?
A 0
B 1
C 2
D 3
✓ C. Pass 1 swaps 1 into position 0 ([1,3,2]). Pass 2 swaps 2 into position 1 ([1,2,3]). Two real swaps.
MCQ 7
Insertion sort runs on [3,1,2]. How many shifts occur (each move of an existing element one position right, not the final key placement)?
A 0
B 1
C 2
D 4
✓ C. Placing 1 shifts 3 right once. Placing 2 shifts 3 right once more. Two shifts total.
MCQ 8
For the same input [3,1,2], both selection sort and insertion sort were traced above. What is true of their final arrays?
A They differ, because the two algorithms use different operations
B This cannot be determined from the traces alone
C Selection sort produces [1,2,3] but insertion sort produces [1,3,2]
D They are identical: [1,2,3], since both are correct sorts of the same input
✓ D. Different work, same correct result: both land on [1,2,3].
MCQ 9
Selection sort runs on [9,8,7,6] (reverse sorted, its worst case). How many real swaps occur?
A 2
B 1
C 3
D 4
✓ A. Pass 1 swaps 6 into position 0 ([6,8,7,9]). Pass 2 swaps 7 into position 1 ([6,7,8,9]). Pass 3 finds the smallest remaining value already in place, no swap. Two real swaps.
MCQ 10
Insertion sort runs on [9,8,7,6] (its worst case). How many shifts occur?
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
34.8% of Tanner’s CSP students score 5s. The national average is 9.6%.
I’m a Student
I’m a Teacher
✓Free AP CSP Big Ideas cheat sheet (PDF)
✓Daily practice questions covering all 5 Big Ideas
✓Create Task tips that actually work — from a real AP teacher
✓Free class codes with student progress tracking
✓3 full practice exams + Top 100 questions for your class
✓Create Task guidance and pseudocode reference sheets
Which AP CS exams are you prepping for?
✓
You’re in!
Your Big Ideas cheat sheet is on its way.
No thanks, I’ll figure it out myself
Avg student improvement: 2+ score levels | Real AP teacher, not just a tutor
AP Cybersecurity — National Launch 2026–27
Get Early Access to AP Cyber
AP Cyber launches nationally fall 2026. Get in early to help shape the course — start free with Unit 1 and the free teacher gradebook.
✓
You’re in — you’re on the AP Cyber early-access list!
Tanner will follow up personally within 48 hours. Your feedback will directly shape what gets built.
Step 1 of 4
Early Access — Limited Spots
Who are you?
Are you a teacher or a student?
I’m a Teacher
I’m a Student
Free to start — Unit 1 and the teacher gradebook are always free, no credit card.
Founding teachers unlock all 5 units and get direct input on what we build.
Not interested right now
Step 2 of 4
Your School
Tell us about your class
Other AP CS courses you teach
Your Situation
Tell us about yourself
Step 3 of 4
Classroom Needs
What does your classroom need? (select all that apply)
How You Study
What would help you most? (select all that apply)
Step 4 of 4
Almost Done
Where should we send your early-access details?
Free gradebook + Unit 1 | Your feedback shapes the course | Built by a real AP teacher