10.2.8 Maximum Iterations CodeHS Answers
10.2.8 Maximum Iterations CodeHS Answers
The most common answer to the 10.2.8 Maximum Iterations Java Codehs
answer is:
import java.util.*;
// selected value.
binaryRec(generateArrayOfLength(100), 2, 0, 99);
System.out.println();
count = 0;
binaryRec(generateArrayOfLength(1000), 2, 0, 999);
System.out.println();
count = 0;
binaryRec(generateArrayOfLength(10000), 2, 0, 9999);
System.out.println();
count = 0;
binaryRec(generateArrayOfLength(100000), 2, 0, 99999);
public static int binaryRec(int[] array, int target, int begin, int end) {
{
int mid = (begin + end) / 2;
count ++;
// Base Case
if (target == array[mid]) {
return mid;
Arrays.sort(arr);
return arr;
This Java code implements a Binary Search Test, which includes methods for
performing a binary search recursively (binaryRec), generating a sorted array of a
given length (generateArrayOfLength), and calculating the maximum number of
iterations required to perform a binary search on an array of a given size
(binaryMax).
The program runs tests for arrays of different sizes (100, 1000, 10000, 100000) and
prints the maximum iterations and actual iterations required to find a target value
using binary search.
The target value 2 is used consistently for simplicity, but since the arrays are
randomly generated, the actual presence and position of 2 in each array can vary,
affecting the actual iterations count.
It’s worth noting that the arrays are filled with random numbers and sorted, and the
target for the binary search is set to 2. Given the randomness, the “Actual
iterations” might vary or the target might not be present at all in some runs, which
is a suitable demonstration of binary search behavior in various scenarios.
This Java code is well-structured for educational purposes. It illustrates how binary
search works, how to implement it recursively, and how to assess its efficiency in
terms of iterations.