Search for the character Z using the binary search algorithm on the following array of characters: A D H J L N P R Z For each iteration of binary search use the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point index of the array, and (d) the number of character-to-character comparisons made during the search at lines 8 and 10 of the algorithm at the back of the exam.
public static int binarySearch(char[] a, char target) {
int left = 0; //first index of array
int right = a.length - 1; //last index of array
while (left <= right) {
int middle = (left + right) / 2;
if ( a[middle] == target) {
return middle;
} else if (target < a[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return -1; //target not found
}
Iteration Left Right Middle Number of Comparisons
1
2
3
4

Solved
Show answers

Ask an AI advisor a question