Merge Sort

Merge sort is the recursive sorting algorithm in AP Computer Science A. It repeatedly splits an array in half, recursively sorts each half, then merges the two sorted halves back together, giving it O(n log n) time complexity in every case.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is Merge Sort?

Merge sort is a divide-and-conquer sorting algorithm, and it's the one recursive sort you're responsible for in AP Computer Science A. The idea is simple once it clicks. Sorting a big array is hard, but merging two already-sorted arrays is easy. You just compare the front elements of each half and pick the smaller one, over and over. So merge sort splits the array in half, recursively sorts each half (splitting again and again until each piece is a single element, which is sorted by definition), and then merges the sorted halves back up.

That recursive structure is exactly why it shows up in the recursion unit instead of with the iterative sorts. Selection sort and insertion sort grind through the array with nested loops and take O(n²) time. Merge sort cuts the problem in half at every level, which means there are only about log n levels of splitting, and each level does O(n) work during the merges. Multiply those together and you get O(n log n) in the best, average, and worst case. The tradeoff is that the standard merge step needs a temporary array, so merge sort uses extra memory that the in-place sorts don't.

Why Merge Sort matters in AP Computer Science A

In the AP CSA course, merge sort lives in Unit 10 (Recursion), alongside recursive binary search. It serves as the course's main example of a recursive algorithm that actually beats its iterative competition. The exam expects you to understand how merge sort works, trace its recursive calls and merge steps, and compare its efficiency to the iterative sorts from earlier units (selection sort and insertion sort, covered with arrays and ArrayLists). It also reinforces the bigger recursion skills the CED cares about, like identifying base cases, tracking what each recursive call does, and counting how many calls execute. If a multiple-choice question hands you an array mid-sort and asks what it looks like after one more pass or one more merge, knowing merge sort's split-then-merge rhythm is what gets you the point.

How Merge Sort connects across the course

Binary Search Algorithm (Units 7 & 10)

Binary search and merge sort are the two halves of the same big idea. Both cut the problem in half each step, which is where the log n comes from. Bonus connection: binary search only works on sorted data, so merge sort is one way to earn the right to use it.

Linear Search (Unit 7)

Linear search checks every element one by one, just like the O(n²) sorts compare everything against everything. The linear-vs-binary search comparison is the same story as the insertion-sort-vs-merge-sort comparison. Halving the problem beats brute force as n grows.

Quick Sort (Beyond AP CSA)

Quick sort is merge sort's divide-and-conquer cousin. It also splits and recurses, but it does the work before the split (partitioning around a pivot) instead of after (merging). It's not in the AP CSA CED, so don't expect it on the exam, but you'll meet it the moment you take a data structures course.

Is Merge Sort on the AP Computer Science A exam?

Merge sort is tested almost entirely through multiple choice, and the questions cluster around three jobs. First, efficiency. Expect stems like "which best describes the time complexity of merge sort" (answer: O(n log n)) or "what is the main advantage of merge sort over bubble sort or selection sort" (answer: far fewer comparisons on large arrays, guaranteed n log n behavior). Second, tracing. You might get a partially sorted array and need to identify what merge sort produces after a given step, or count recursive calls. Third, the mystery-method question, where you're shown recursive sorting code and asked what algorithm it implements or what it outputs. Watch out here, because a recursive method that shifts one element into place per call is recursive insertion sort, not merge sort. Merge sort always splits in half and merges. No released FRQ asks you to write merge sort from scratch, but the recursion FRQ skills it builds (base cases, call tracing) absolutely show up.

Merge Sort vs Selection Sort and Insertion Sort

All three sort arrays, but they're tested differently. Selection and insertion sort are the iterative, nested-loop sorts from the array units, and they run in O(n²) time. Merge sort is recursive, splits the array in half, and runs in O(n log n) in every case. If exam code has nested for loops, it's not merge sort. If it calls itself on smaller halves and combines results, it is. Also remember the cost: merge sort needs a temporary array for merging, while selection and insertion sort work in place.

Key things to remember about Merge Sort

  • Merge sort works in two phases: it recursively splits the array into halves until each piece has one element, then merges the sorted halves back together.

  • Its time complexity is O(n log n) in the best, average, and worst case, which makes it faster than selection sort, insertion sort, and bubble sort (all O(n²)) on large arrays.

  • Merge sort is the recursive sorting algorithm in AP CSA Unit 10; selection and insertion sort are the iterative ones from earlier units.

  • The base case is an array (or subarray) of one element, which is already sorted, and that's the key insight that lets the recursion stop.

  • The merge step is where the real work happens, and it requires a temporary array, so merge sort trades extra memory for speed.

  • On the exam, you need to trace merge sort and compare its efficiency, not write the full implementation from memory.

Frequently asked questions about Merge Sort

What is merge sort in AP Computer Science A?

Merge sort is the recursive divide-and-conquer sorting algorithm covered in Unit 10 of AP CSA. It splits an array in half, recursively sorts each half, then merges the sorted halves, achieving O(n log n) time complexity in all cases.

Do I have to write merge sort code on the AP CSA exam?

No. The exam tests merge sort through multiple-choice questions where you trace its behavior, identify it in mystery code, or compare its O(n log n) efficiency to O(n²) sorts. You won't be asked to write the full algorithm from scratch on an FRQ.

How is merge sort different from selection sort and insertion sort?

Merge sort is recursive and runs in O(n log n) time by halving the array, while selection and insertion sort are iterative nested-loop algorithms that run in O(n²). The tradeoff is that merge sort needs a temporary array for merging, while the other two sort in place.

Is merge sort always O(n log n)?

Yes. Unlike some sorts whose speed depends on the input, merge sort splits the array in half regardless of how the data is arranged, so its best, average, and worst cases are all O(n log n). That guaranteed performance is its main selling point on exam comparison questions.

Is quick sort on the AP Computer Science A exam?

No. Merge sort is the only recursive sorting algorithm in the AP CSA CED. Quick sort, heap sort, and radix sort are worth knowing for later courses, but they won't appear on the AP exam.