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.
📄 On This Page
🔧 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.
- Save: Store the current element in a temporary variable
- Shift: Move all larger sorted elements one position to the right
- Insert: Place the saved element into the gap
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;
}
}
⚖ 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
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.
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.
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
{9, 4, 6, 2, 8}, what is the array?{8, 6, 7, 2, 5}?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.
>= instead of > in the while condition: data[pos-1] >= temp. What effect does this have?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;
}
{5, 3, 5, 2, 5}, what is the order of the three 5s relative to their original positions?{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.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]