Insertion Sort in AP CSA: Shift, Insert, Trace (2026)

Insertion Sort in AP CSA: Shift, Insert, Trace

Insertion sort is the second sorting algorithm on the 2026 AP CSA exam. Unlike selection sort, it shifts elements rather than swapping. Understanding the shift-and-insert pattern is essential for tracing questions and distinguishing it from selection sort.

⏰ The AP CSA Exam is May 15, 2026 — Get the 4-Week Cram Kit →

🔧 How Insertion Sort Works

Think of sorting a hand of playing cards. You pick up each card and slide it into the correct position among the cards you have already sorted.

  1. Save: Store the current element in a temporary variable
  2. Shift: Move all larger sorted elements one position to the right
  3. Insert: Place the saved element into the gap
🎓 AP Exam Tip

The exam loves to ask: “After 3 passes of insertion sort, what is the array?” Remember — the sorted portion grows from the LEFT, but those elements are only sorted relative to each other, not necessarily in their final positions.

📋 Complete Trace Example

Sort the array {7, 3, 5, 1, 4} using insertion sort:

Pass Current Shifts Array After
1 3 7 shifts right [3, 7, 5, 1, 4]
2 5 7 shifts right [3, 5, 7, 1, 4]
3 1 7, 5, 3 all shift right [1, 3, 5, 7, 4]
4 4 7, 5 shift right [1, 3, 4, 5, 7]

Key observation: on pass 3, the value 1 caused THREE shifts. Insertion sort can move many elements in a single pass.

💻 The Code (AP Exam Version)

public static void sort(int[] data)
{
    for (int j = 1; j < data.length; j++)
    {
        int temp = data[j];
        int pos = j;
        while (pos > 0 && data[pos - 1] > temp)
        {
            data[pos] = data[pos - 1];
            pos--;
        }
        data[pos] = temp;
    }
}
⚠ Watch Out! The outer loop starts at index 1, not 0. The element at index 0 is considered “already sorted” as a group of one.

⚖ Selection Sort vs Insertion Sort

Feature Selection Sort Insertion Sort
Mechanism Find min, swap Shift right, insert
Comparisons Always n(n-1)/2 Varies (fewer if nearly sorted)
After k passes First k are FINAL positions First k+1 are sorted relative to each other
Best case Same as worst case Nearly sorted: very fast
Code clue temp + swap while loop + shift

❌ Common Pitfalls

Pitfall 1: Confusing “Sorted” with “Final Position”

After pass 2 of insertion sort on {7, 3, 5, 1, 4}, the array is [3, 5, 7, 1, 4]. The first 3 elements are sorted relative to each other, but 3 is NOT in its final position (1 will eventually go before it). In selection sort, the first k elements ARE in final positions.

Pitfall 2: Forgetting the Short-Circuit in the While Loop

The condition pos > 0 && data[pos-1] > temp has two parts. If pos reaches 0, the element goes at the front. If data[pos-1] is not greater than temp, the element stays where it is. Both conditions must be checked.

Pitfall 3: Counting Shifts as Swaps

Insertion sort does NOT swap. It shifts elements one position and then inserts once. This is more efficient than swapping because it uses one assignment per shift instead of three.

✏ Practice Questions

Score: 0 / 8
1. After two passes of insertion sort on {9, 4, 6, 2, 8}, what is the array?
2. On which input does insertion sort perform the fewest total shifts?
3. How many shifts occur on pass 3 (j=3) when sorting {8, 6, 7, 2, 5}?
4. Which of the following is true about insertion sort but NOT selection sort?
I. The number of comparisons depends on the initial order.
II. After k passes, the first k+1 elements are sorted relative to each other.
III. Elements are placed in their final position after each pass.
5. A student writes insertion sort but uses >= instead of > in the while condition: data[pos-1] >= temp. What effect does this have?
6. Consider the code segment below. What algorithm does it implement?
for (int j = 1; j < arr.length; j++)
{
    int val = arr[j];
    int k = j;
    while (k > 0 && arr[k - 1] > val)
    {
        arr[k] = arr[k - 1];
        k--;
    }
    arr[k] = val;
}
7. After insertion sort completes on {5, 3, 5, 2, 5}, what is the order of the three 5s relative to their original positions?
8. What is the total number of element shifts when insertion sort processes {5, 4, 3, 2, 1}?

❓ Frequently Asked Questions

How does insertion sort differ from selection sort?

Selection sort finds the minimum and swaps it into place. Insertion sort takes the next unsorted element and shifts sorted elements to the right until the correct position opens up, then inserts it there.

Is insertion sort faster than selection sort?

On nearly-sorted data, insertion sort is significantly faster because few shifts are needed. On reverse-sorted data, both have similar performance. Selection sort always does the same work regardless of order.

How do I recognize insertion sort in AP CSA code?

Look for a pattern where elements are shifted one position to the right inside a while loop, and then a single value is inserted into the gap. Selection sort uses a find-minimum-then-swap pattern instead.

What does the array look like during insertion sort?

At any point during insertion sort, the left portion of the array is sorted relative to itself, but elements may not be in their final positions. This differs from selection sort, where the left portion contains the actual smallest values.

About the Author: Tanner Crow has 11+ years of AP Computer Science teaching experience, 1,845+ verified tutoring hours, and a 5.0 rating. His students score 5s at over double the national rate. Book a tutoring session →

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]