Page 56 - AP Computer Science A, 7th edition
P. 56
(E) It converts the elements of arr to base–10.
Questions 27 and 28 refer to the search method in the Searcher
class below.
public class Searcher {
private int[] arr;
/∗ ∗ Constructor. Initializes arr with ∗/
public Searcher()
{ /∗ implementation not shown ∗/ }
integers.
/∗ ∗ Precondition: arr[first]...arr[last] sorted in ascending order.
∗ Postcondition: Returns index of key in arr. If key not in arr,
∗ returns –1.
∗/
public int search(int first, int last, int key) {
}
int mid;
while (first <= last) {
mid = (first + last) / 2; if (arr[mid] == key) search
//found key, exit
return mid;
else if (arr[mid] < key) //key to right of arr[mid]
first = mid + 1; else
left of arr[mid] last = mid – 1;
}
return –1; in list
//key to
//key not