Sorting Algorithms AP CSA
Sorting Algorithms in AP CSA: Complete Guide (2025-2026)
AP CSA tests two sorting algorithms: selection sort and insertion sort. Both are O(n²) and sort in place. You need to trace them step by step, spot broken swap code, and calculate comparison counts. This is Unit 4 content (Data Collections, 30–40% of the exam).
Table of Contents
Selection Sort
- Outer loop: i from 0 to arr.length − 2
- Inner loop: j from i+1 to arr.length − 1 — finds minimum index in unsorted portion
- Swap arr[i] with arr[minIdx]
- After pass i, arr[0..i] is in final position
- Always makes exactly n(n−1)/2 comparisons
Insertion Sort
- Outer loop: i from 1 to arr.length − 1
- Save key = arr[i]; shift elements greater than key rightward
- Place key in the gap
- After pass i, arr[0..i] is sorted
- Efficient on nearly-sorted data
Exam tip: trace questions ask “after k passes”—not after fully sorted. Always trace exactly k outer-loop iterations, not more.
Code Examples
Example 1: Selection Sort
public class SelectSort {
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
int tmp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = tmp;
}
}
public static void main(String[] args) {
int[] data = {64, 25, 12, 22};
selectionSort(data);
for (int v : data) System.out.print(v + " ");
}
}
Example 2: Insertion Sort
public class InsertSort {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
int[] data = {5, 3, 8, 1, 4};
insertionSort(data);
for (int v : data) System.out.print(v + " ");
}
}
Example 3: Tracing Pass-by-Pass
public class SortTrace {
public static void main(String[] args) {
int[] arr = {3, 1, 4, 2};
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
int tmp = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = tmp;
System.out.print("Pass " + (i+1) + ": ");
for (int v : arr) System.out.print(v + " ");
System.out.println();
}
}
}
Common Pitfalls
Save the original value FIRST: tmp=arr[i]; arr[i]=arr[minIdx]; arr[minIdx]=tmp;. Skipping tmp destroys the original value.
Both sorts: outer loop ends at arr.length - 1. The last element lands in place automatically.
After k passes, the first k elements are finalized. Pass k corresponds to outer loop iteration i = k−1.
Selection sort always makes n(n−1)/2 comparisons. It does NOT detect a sorted array.
Check for Understanding
{{6, 2, 8, 1, 4}}, what is the array? PREDICT.
{{5, 3, 8, 1}}, what is the array? PREDICT.
int minIdx = i;
for (int j = i+1; j < arr.length; j++)
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
int tmp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = arr[i]; // ← line X
I. Selection sort always makes the same number of comparisons regardless of input order.
II. Insertion sort is more efficient on nearly-sorted arrays than selection sort.
III. Both have O(n²) worst-case runtime.
{{9, 4, 2, 7}}, what is the array? PREDICT.
int tmp = arr[i]; arr[i] = arr[minIdx]; ________ = tmp;
Frequently Asked Questions
No. Only selection sort and insertion sort are in the 2025-2026 curriculum.
Both are O(n²) worst case. Insertion sort is faster on nearly-sorted arrays because it makes fewer shifts. Selection sort always runs the same number of comparisons.
Yes: for (int j = i-1; j >= 0 && arr[j] > key; j--) arr[j+1] = arr[j]; arr[j+1] = key;. Both while and for versions are correct.
Exactly k elements are in their final sorted positions at the front of the array after k passes.
Yes. The inner loop never shifts anything, so it runs in O(n) time on a fully sorted array (the best case).
1-on-1 Expert Support
Sorting traces trip up even strong students. A targeted session with an AP CSA expert locks in the patterns before exam day.
View Tutoring OptionsRelated Topics
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]