Lesson 4.15: Sorting Algorithms

AP CSA Hub Unit 4 4.1 Lesson Ex 1 Ex 2 Quiz 4.2 Lesson Ex 1 Ex 2 Quiz 4.3 Lesson Ex 1 Ex 2 Quiz 4.4 Lesson Ex 1 Ex 2 Quiz 4.5 Lesson Ex 1 Ex 2 Quiz 4.6 Lesson Ex 1 Ex 2 Quiz 4.7 Lesson Ex 1 Ex 2 Quiz 4.8 Lesson Ex 1 Ex 2 Quiz 4.9 Lesson Ex 1 Ex 2 Quiz 4.10 Lesson Ex 1 Ex 2 Quiz 4.11 Lesson Ex 1 Ex 2 Quiz 4.12 Lesson Ex 1 Ex 2 Quiz 4.13 Lesson Ex 1 Ex 2 Quiz 4.14 Lesson Ex 1 Ex 2 Quiz 4.15 Lesson Ex 1 Ex 2 Quiz 4.16 Lesson Ex 1 Ex 2 Quiz 4.17 Lesson Ex 1 Ex 2 Quiz

Unit 4 · Lesson 4.15 · Sorting Algorithms

Lesson 4.15: Sorting Algorithms

🕒 40-50 min · 10 Practice Questions · Selection Sort · Insertion Sort · Tracing

What You'll Learn

  • 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?
A 2
B 4
C 6
D 8
C. Placing 8 shifts once, placing 7 shifts twice, placing 6 shifts three times: 1 + 2 + 3 = 6 shifts total.

Get in Touch

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.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]