Skip to main content
Logo image

Section 12.4 Recursive searching and sorting

In the first two lessons of this chapter , we learned about searching and sorting algorithms that using iteration (loops) to search or sort arrays and ArrayLists. And as we saw in the previous lesson, recursion can also be used to traverse Strings, arrays, and ArrayLists but is even more useful for traversing tree structures.
In this lesson, we will take a look at how we can use recursion to implement binary searching and a recursive sorting algorithm called merge-sort algorithm.

Subsection 12.4.2 Merge sort

In SectionΒ 12.2Β Sorting algorithms, we looked at two sorting algorithms, selection sort and insertion sort. In this lesson, we will look at a third sorting algorithm, merge sort, which uses recursion. In this case recursion makes the algorithm more efficient than either of the other two algorithms we’ve looked it.
Similar to how the recursive binary search worked by searching smaller and smaller sub-ranges of the original array, merge sort repeatedly divides ranges covering part of the array into smaller sub-ranges until each range is just one element wide and then recursively merges adjacent ranges back together in sorted order.
For example, after four adjacent one-element ranges are merged into two adjacent two-element ranges, those two-element ranges are merged into one four-element range, and so on until on the very last step, the sorted first half of the array is merged with the sorted second half of the array, completing the sort.
Merge sort typically uses a second array the same size as the original array to hold the values while merging and then copies the sorted values back into the original array.
Here is a folk dance video that shows the merge sort process.
And here is a short video that describes how merge sort works.
And here is an implementation of merge sort. Note that the only public method here is sort but it is written in terms of two helper methods: sortRange and merge. The sortRange method is actually the recursive method. Since recursive methods often need some extra arguments to specify what sub-problem each recursive call is working on, it’s common to write a non-recursive method that calls a recursive method with the right initial arguments to kick things off. Here sort calls sortRange with the range covering the whole array.
You can see this executing using this visualization or the CodeLens button above.
To understand merge sort it can be useful to trace through the steps using parentheses or curly braces to show how the array is divided into sub-arrays and then merged. For example, here is a trace of of sorting the array {86, 3, 43, 5} from the code above.
  1. Split 1: { { 86, 3 } , { 43, 5 } }
  2. Split 2: { { { 86 }, { 3 } } , { { 43 }, { 5 }} }
  3. Merge 1: { { 3, 86 }, { 5, 43 } }
  4. Merge 2: { 3, 5, 43, 86 }

Activity 12.4.4.

Under what condition will a merge sort execute faster?
  • If the data is already sorted in ascending order
  • This won’t really affect the execution time for merge sort.
  • If the data is already sorted in descending order
  • This won’t really affect the execution time for merge sort.
  • It will always take the same amount of time to execute
  • It will take about the same time regardless of the data.

Activity 12.4.5.

Which sort should be the fastest most of the time?
  • selection sort
  • Merge sort is always faster than selection sort.
  • insertion sort
  • Merge sort is usually faster than insertion sort.
  • merge sort
  • Merge sort is always faster than selection sort and usually faster than insertion sort.

Subsection 12.4.3 Tracing Challenge: Recursive Search and Sort

Working in pairs, practice the recursive binary search and merge sort algorithms with a deck of cards or pieces of paper with numbers or names on them. Here’s a video that shows merge sort with cards.
Work in pairs to do the following tracing problems.

Project 12.4.6.

Trace through mergeSort(array) where array = {5, 2, 20, 22, 17, 15, 8, 10} writing down each split and merge.

Subsection 12.4.4 Summary

  • (AP 4.17.A.1) Recursion can be used to traverse String objects, arrays, and ArrayList objects.
  • (AP 4.17.B.1) Data must be in sorted order to use the binary search algorithm. Binary search starts at the middle of a sorted array or ArrayList and eliminates half of the array or ArrayList in each recursive call until the desired value is found or all elements have been eliminated.
  • (AP 4.17.B.2) Binary search is typically more efficient than linear search.
  • (AP 4.17.B.3) The binary search algorithm can be written either iteratively or recursively.
  • (AP 4.17.C.1) Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.
  • (AP 4.17.C.2) Merge sort repeatedly divides an array into smaller sub-arrays until each sub-array is one element and then recursively merges the sorted sub-arrays back together in sorted order to form the final sorted array.

Subsection 12.4.5 Summary and Review Exercises

This lesson is the last lesson in Unit 4! Congratulations! Now, it’s time to practice before the exam.
You can now do the following review lessons about recursion at the end of the unit and College Board Progress Check for Unit 4 Part 3 in the AP Classroom. The rest of this unit consists of the reviews for each section of the unit.
You have attempted of activities on this page.