Recursive Searching
and Sorting
Master binary search and merge sort through the lens of recursion: breaking big problems into smaller, self-similar subproblems.
What is Recursion?
A recursive method is one that calls itself to solve a smaller instance of the same problem. Every recursive solution has two critical parts:
Why Recursion for Search and Sort?
Many searching and sorting algorithms naturally follow a divide-and-conquer strategy: split the problem, solve each piece recursively, then combine the results.
The Call Stack in Action
Each recursive call creates a new stack frame. When a base case returns, frames unwind in reverse order. Understanding this is key to tracing recursive algorithms.
Recursive Binary Search
Given a sorted array, binary search finds a target by repeatedly halving the search range. Its time complexity is \( O(\log n) \).
Binary Search: Step by Step
Searching for 23 in a sorted array of 8 elements:
A sorted collection makes discarded search ranges meaningful
Sorted order makes it possible to discard an entire search range after one comparison. An unsorted collection does not support the same elimination.Binary search with Java indices
Search this sorted nine-value array for 23. The tested midpoint values are 17, 31 and 23; Java indices 0 through 8 lead to a return value of 5. Each failed comparison excludes its tested midpoint.Count comparisons as the list grows
For inclusive midpoint binary search on n ≥ 1 items, the maximum number of probes is floor(log₂ n) + 1. The horizontal axis uses a log₂ scale; the count measures search probes after sorting.Recursive Merge Sort
Merge sort splits the array into halves, recursively sorts each half, then merges them into sorted order. Time complexity: \( O(n \log n) \).
Merge Sort: Split and Merge
The Merge Helper Method
The merge method combines two already-sorted halves into one sorted sequence using a temporary array.
Binary vs Linear Search
| Binary Search | Linear Search | |
|---|---|---|
| Precondition | Array must be sorted | No requirement |
| Time Complexity | \( O(\log n) \) | \( O(n) \) |
| Strategy | Divide and conquer | Check every element |
| Recursive? | Naturally recursive | Typically iterative |
| Best for | Large sorted datasets | Small or unsorted data |
Merge Sort vs Selection Sort
| Merge Sort | Selection Sort | |
|---|---|---|
| Time (worst) | \( O(n \log n) \) | \( O(n^2) \) |
| Time (best) | \( O(n \log n) \) | \( O(n^2) \) |
| Space | \( O(n) \) extra (temp array) | \( O(1) \) in-place |
| Strategy | Divide and conquer | Find minimum, swap |
| Recursive? | Yes | No (iterative) |
How to Apply Binary Search
Example: Trace Binary Search
Array: {1, 4, 7, 10, 15, 20, 25}. Find 15.
Example: Trace Merge Sort
Sort {5, 2, 8, 1} using merge sort.
Example: Merge in Detail
Merge left = {3, 9, 27} and right = {10, 38, 43} step by step.
Common Mistakes
Question 1 of 5
What is the time complexity of recursive binary search on a sorted array of \( n \) elements?
Question 2 of 5
In the recursive binary search method, what happens when \( \text{lo} > \text{hi} \)?
Question 3 of 5
What is the base case for merge sort?
Question 4 of 5
Consider the array {2, 5, 8, 12, 15, 18, 23}. How many total calls to binarySearch are needed to find 18?
Question 5 of 5
What is the space complexity of merge sort and why?