10.2 Recursive Searching
As discussed in previous chapters, searching and sorting data is an integral part of data storage and management in Java. Searching algorithms, such as Insertion and Selection sort, rely on the use of iterative control structures, like for loops and while loops, to traverse data structures and find a desired value:
Just as for loops and while loops can be used for traversals, recursion too can be applied to searching algorithms.
One search method that relies on the use of recursion is binary search.
A binary search finds the middle of an array and tests that point. If the target is greater than the midpoint value, then the search eliminates the lower half. Likewise, if the target is less than the midpoint value, the top half can be eliminated. This process is then repeated, finding a new midpoint each time. It’s important to note that the array must be sorted for a binary search to work properly.
Here is this search in action:
Notice how each iteration eliminates half of the values. While this process is slightly more efficient for smaller arrays, it is significantly more efficient for larger arrays. A linear search of 10,000 records could have a worst case scenario of 10,000 comparisons. With binary search, the worst case is only 15 comparisons to find the value.
When comparing Linear Search to Binary Search, there are a few significant differences. First, binary searches are much more efficient than linear searches. Second, linear searches will work on any array, but binary searches only work on a sorted array. Finally, the algorithm for binary searches is a little more complex than our linear search.
Linear | Binary |
---|---|
Not efficient | Very efficient |
Works on sorted or unsorted arrays. | Only works on sorted arrays. |
Easy to code | Slightly more complex to code |
Here is a non-recursive implementation of binary search:
Notice in the loop how the midpoint is calculated, then tested before updating the beginning and ending points each time based on the results. Initially, the array is passed to the method, where the values begin
and end
are set to the first index and the last index in the array. The while loop then checks that begin is less than end, and calculates the midpoint of the array. The method then checks if the target value is greater than, less than, or equal to the midpoint. If the target is greater than the midpoint, then the method removes the begin to midpoint values from the search. As noted earlier, this only works if the values are sorted before being searched. The method then returns to the beginning of the while loop, and finds a new midpoint between the new begin value and the end. It does so until the target has been found, or the array has been fully halved and searched.
This method can also be written using recursion. Since recursion attempts to create a simpler version of a problem each time a recursive method is called, binary search lends itself well to its use, as the array is reduced in size each time, all the way to a single value at times. Here is the implementation of binary search with recursion:
The biggest change at the start of this code is that the beginning and ending values are being passed into the method. From there, the midpoint is calculated the same way as before.
The base case is where the value is found. In the first iteration, the base case is skipped, and the recursive case is called. The recursive call passes the original array, the target value, but then updates the starting point as it makes the call. Once the value is found, the index value of the target is then returned back through each of the recursive return statements until it eventually gets back to the original call statement.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 128 elements?
How many times does a binary search need to execute to find its value? Recall from our lesson that the number of iterations is roughly a log base 2 relationship.
In this exercise, you are going to calculate the maximum iterations and the actual iterations needed to find a random value in arrays of size 500, 5000, 50k, and 500k.
You are given helper methods to calculate the maximum iterations, generate the random array, and do the binary search. You are also given a counter variable that increments each time your recursive binary method is called.
You will need to come up with the remainder of the code.